diff --git a/pandas/compat/chainmap.py b/pandas/compat/chainmap.py deleted file mode 100644 index 5bec8e5fa1913..0000000000000 --- a/pandas/compat/chainmap.py +++ /dev/null @@ -1,37 +0,0 @@ -from __future__ import annotations - -from typing import ( - ChainMap, - TypeVar, -) - -_KT = TypeVar("_KT") -_VT = TypeVar("_VT") - - -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) diff --git a/pandas/core/computation/pytables.py b/pandas/core/computation/pytables.py index f7d1314b12da2..ba3ff2e137857 100644 --- a/pandas/core/computation/pytables.py +++ b/pandas/core/computation/pytables.py @@ -3,10 +3,7 @@ import ast from functools import partial -from typing import ( - TYPE_CHECKING, - Any, -) +from typing import Any import numpy as np @@ -36,9 +33,6 @@ pprint_thing_encoded, ) -if TYPE_CHECKING: - from pandas.compat.chainmap import DeepChainMap - class PyTablesScope(_scope.Scope): __slots__ = ("queryables",) @@ -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 diff --git a/pandas/core/computation/scope.py b/pandas/core/computation/scope.py index 6d070540de26a..0b9ba84cae511 100644 --- a/pandas/core/computation/scope.py +++ b/pandas/core/computation/scope.py @@ -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(