Skip to content

CLN: Move DeepChainMap to only location used #51314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions pandas/compat/chainmap.py

This file was deleted.

10 changes: 2 additions & 8 deletions pandas/core/computation/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@

import ast
from functools import partial
from typing import (
TYPE_CHECKING,
Any,
)
from typing import Any

import numpy as np

Expand Down Expand Up @@ -36,9 +33,6 @@
pprint_thing_encoded,
)

if TYPE_CHECKING:
from pandas.compat.chainmap import DeepChainMap


class PyTablesScope(_scope.Scope):
__slots__ = ("queryables",)
Expand Down Expand Up @@ -567,7 +561,7 @@ def __init__(
self._visitor = None

# capture the environment if needed
local_dict: DeepChainMap[Any, Any] | None = None
local_dict: _scope.DeepChainMap[Any, Any] | None = None

if isinstance(where, PyTablesExpr):
local_dict = where.env.scope
Expand Down
39 changes: 37 additions & 2 deletions pandas/core/computation/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,51 @@
import pprint
import struct
import sys
from typing import (
ChainMap,
TypeVar,
)

import numpy as np

from pandas._libs.tslibs import Timestamp
from pandas.compat.chainmap import DeepChainMap
from pandas.errors import UndefinedVariableError

_KT = TypeVar("_KT")
_VT = TypeVar("_VT")


# https://docs.python.org/3/library/collections.html#chainmap-examples-and-recipes
class DeepChainMap(ChainMap[_KT, _VT]):
"""
Variant of ChainMap that allows direct updates to inner scopes.

Only works when all passed mapping are mutable.
"""

def __setitem__(self, key: _KT, value: _VT) -> None:
for mapping in self.maps:
if key in mapping:
mapping[key] = value
return
self.maps[0][key] = value

def __delitem__(self, key: _KT) -> None:
"""
Raises
------
KeyError
If `key` doesn't exist.
"""
for mapping in self.maps:
if key in mapping:
del mapping[key]
return
raise KeyError(key)


def ensure_scope(
level: int, global_dict=None, local_dict=None, resolvers=(), target=None, **kwargs
level: int, global_dict=None, local_dict=None, resolvers=(), target=None
) -> Scope:
"""Ensure that we are grabbing the correct scope."""
return Scope(
Expand Down