Skip to content

Commit 6bc7e0c

Browse files
committed
fix cache_readonly
1 parent 822db7a commit 6bc7e0c

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

pandas/core/dtypes/dtypes.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class PandasExtensionDtype(ExtensionDtype):
8181
base: DtypeObj | None = None
8282
isbuiltin = 0
8383
isnative = 0
84-
_cache: dict[str_type, PandasExtensionDtype] = {}
84+
_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}
8585

8686
def __str__(self) -> str_type:
8787
"""
@@ -105,7 +105,7 @@ def __getstate__(self) -> dict[str_type, Any]:
105105
@classmethod
106106
def reset_cache(cls) -> None:
107107
""" clear the cache """
108-
cls._cache = {}
108+
cls._cache_dtypes = {}
109109

110110

111111
class CategoricalDtypeType(type):
@@ -177,7 +177,7 @@ class CategoricalDtype(PandasExtensionDtype, ExtensionDtype):
177177
str = "|O08"
178178
base = np.dtype("O")
179179
_metadata = ("categories", "ordered")
180-
_cache: dict[str_type, PandasExtensionDtype] = {}
180+
_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}
181181

182182
def __init__(self, categories=None, ordered: Ordered = False):
183183
self._finalize(categories, ordered, fastpath=False)
@@ -671,7 +671,7 @@ class DatetimeTZDtype(PandasExtensionDtype):
671671
na_value = NaT
672672
_metadata = ("unit", "tz")
673673
_match = re.compile(r"(datetime64|M8)\[(?P<unit>.+), (?P<tz>.+)\]")
674-
_cache: dict[str_type, PandasExtensionDtype] = {}
674+
_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}
675675

676676
def __init__(self, unit: str_type | DatetimeTZDtype = "ns", tz=None):
677677
if isinstance(unit, DatetimeTZDtype):
@@ -837,7 +837,7 @@ class PeriodDtype(dtypes.PeriodDtypeBase, PandasExtensionDtype):
837837
num = 102
838838
_metadata = ("freq",)
839839
_match = re.compile(r"(P|p)eriod\[(?P<freq>.+)\]")
840-
_cache: dict[str_type, PandasExtensionDtype] = {}
840+
_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}
841841

842842
def __new__(cls, freq=None):
843843
"""
@@ -859,12 +859,12 @@ def __new__(cls, freq=None):
859859
freq = cls._parse_dtype_strict(freq)
860860

861861
try:
862-
return cls._cache[freq.freqstr]
862+
return cls._cache_dtypes[freq.freqstr]
863863
except KeyError:
864864
dtype_code = freq._period_dtype_code
865865
u = dtypes.PeriodDtypeBase.__new__(cls, dtype_code)
866866
u._freq = freq
867-
cls._cache[freq.freqstr] = u
867+
cls._cache_dtypes[freq.freqstr] = u
868868
return u
869869

870870
def __reduce__(self):
@@ -1042,7 +1042,7 @@ class IntervalDtype(PandasExtensionDtype):
10421042
_match = re.compile(
10431043
r"(I|i)nterval\[(?P<subtype>[^,]+)(, (?P<closed>(right|left|both|neither)))?\]"
10441044
)
1045-
_cache: dict[str_type, PandasExtensionDtype] = {}
1045+
_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}
10461046

10471047
def __new__(cls, subtype=None, closed: str_type | None = None):
10481048
from pandas.core.dtypes.common import (
@@ -1099,12 +1099,12 @@ def __new__(cls, subtype=None, closed: str_type | None = None):
10991099

11001100
key = str(subtype) + str(closed)
11011101
try:
1102-
return cls._cache[key]
1102+
return cls._cache_dtypes[key]
11031103
except KeyError:
11041104
u = object.__new__(cls)
11051105
u._subtype = subtype
11061106
u._closed = closed
1107-
cls._cache[key] = u
1107+
cls._cache_dtypes[key] = u
11081108
return u
11091109

11101110
@property

pandas/tests/dtypes/test_dtypes.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ def test_pickle(self, dtype):
6666

6767
# clear the cache
6868
type(dtype).reset_cache()
69-
assert not len(dtype._cache)
69+
assert not len(dtype._cache_dtypes)
7070

7171
# force back to the cache
7272
result = tm.round_trip_pickle(dtype)
7373
if not isinstance(dtype, PeriodDtype):
7474
# Because PeriodDtype has a cython class as a base class,
7575
# it has different pickle semantics, and its cache is re-populated
7676
# on un-pickling.
77-
assert not len(dtype._cache)
77+
assert not len(dtype._cache_dtypes)
7878
assert result == dtype
7979

8080

@@ -791,14 +791,14 @@ def test_basic_dtype(self):
791791
def test_caching(self):
792792
IntervalDtype.reset_cache()
793793
dtype = IntervalDtype("int64", "right")
794-
assert len(IntervalDtype._cache) == 1
794+
assert len(IntervalDtype._cache_dtypes) == 1
795795

796796
IntervalDtype("interval")
797-
assert len(IntervalDtype._cache) == 2
797+
assert len(IntervalDtype._cache_dtypes) == 2
798798

799799
IntervalDtype.reset_cache()
800800
tm.round_trip_pickle(dtype)
801-
assert len(IntervalDtype._cache) == 0
801+
assert len(IntervalDtype._cache_dtypes) == 0
802802

803803
def test_not_string(self):
804804
# GH30568: though IntervalDtype has object kind, it cannot be string

0 commit comments

Comments
 (0)