Skip to content

Commit aeb8a85

Browse files
authored
CLN: Move DeepChainMap to only location used (#51314)
1 parent 02a6088 commit aeb8a85

File tree

3 files changed

+39
-47
lines changed

3 files changed

+39
-47
lines changed

pandas/compat/chainmap.py

-37
This file was deleted.

pandas/core/computation/pytables.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33

44
import ast
55
from functools import partial
6-
from typing import (
7-
TYPE_CHECKING,
8-
Any,
9-
)
6+
from typing import Any
107

118
import numpy as np
129

@@ -36,9 +33,6 @@
3633
pprint_thing_encoded,
3734
)
3835

39-
if TYPE_CHECKING:
40-
from pandas.compat.chainmap import DeepChainMap
41-
4236

4337
class PyTablesScope(_scope.Scope):
4438
__slots__ = ("queryables",)
@@ -567,7 +561,7 @@ def __init__(
567561
self._visitor = None
568562

569563
# capture the environment if needed
570-
local_dict: DeepChainMap[Any, Any] | None = None
564+
local_dict: _scope.DeepChainMap[Any, Any] | None = None
571565

572566
if isinstance(where, PyTablesExpr):
573567
local_dict = where.env.scope

pandas/core/computation/scope.py

+37-2
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,51 @@
1010
import pprint
1111
import struct
1212
import sys
13+
from typing import (
14+
ChainMap,
15+
TypeVar,
16+
)
1317

1418
import numpy as np
1519

1620
from pandas._libs.tslibs import Timestamp
17-
from pandas.compat.chainmap import DeepChainMap
1821
from pandas.errors import UndefinedVariableError
1922

23+
_KT = TypeVar("_KT")
24+
_VT = TypeVar("_VT")
25+
26+
27+
# https://docs.python.org/3/library/collections.html#chainmap-examples-and-recipes
28+
class DeepChainMap(ChainMap[_KT, _VT]):
29+
"""
30+
Variant of ChainMap that allows direct updates to inner scopes.
31+
32+
Only works when all passed mapping are mutable.
33+
"""
34+
35+
def __setitem__(self, key: _KT, value: _VT) -> None:
36+
for mapping in self.maps:
37+
if key in mapping:
38+
mapping[key] = value
39+
return
40+
self.maps[0][key] = value
41+
42+
def __delitem__(self, key: _KT) -> None:
43+
"""
44+
Raises
45+
------
46+
KeyError
47+
If `key` doesn't exist.
48+
"""
49+
for mapping in self.maps:
50+
if key in mapping:
51+
del mapping[key]
52+
return
53+
raise KeyError(key)
54+
2055

2156
def ensure_scope(
22-
level: int, global_dict=None, local_dict=None, resolvers=(), target=None, **kwargs
57+
level: int, global_dict=None, local_dict=None, resolvers=(), target=None
2358
) -> Scope:
2459
"""Ensure that we are grabbing the correct scope."""
2560
return Scope(

0 commit comments

Comments
 (0)