Skip to content

Commit 17deecc

Browse files
committed
add tests
1 parent b7b5903 commit 17deecc

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

pandas/tests/libs/test_lib.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pandas._libs import (
55
Timestamp,
66
lib,
7+
properties,
78
writers as libwriters,
89
)
910

@@ -212,3 +213,48 @@ def test_no_default_pickle():
212213
# GH#40397
213214
obj = tm.round_trip_pickle(lib.no_default)
214215
assert obj is lib.no_default
216+
217+
218+
def test_cache_readonly_no_global_cache():
219+
"""
220+
Ensure that the definition of a global attribute `_cache` does not interfere with
221+
"""
222+
223+
from typing import Dict
224+
225+
# Just declaring the type is sufficient for the attribute to be considered global
226+
class MyDtypeGlobalAttr:
227+
_cache: Dict
228+
229+
def __init__(self, value):
230+
self._value = value
231+
self._cache = {}
232+
233+
@properties.cache_readonly
234+
def cached_attr(self):
235+
return self._value
236+
237+
a = MyDtypeGlobalAttr("A")
238+
with pytest.raises(TypeError, match=r"Class \w* defines a `_cache` " r"attribute"):
239+
a.cached_attr
240+
241+
class MyDtypeInstanceAttr:
242+
def __init__(self, value):
243+
self._value = value
244+
self._cache = {}
245+
246+
@properties.cache_readonly
247+
def cached_attr(self):
248+
return self._value
249+
250+
a = MyDtypeInstanceAttr("A")
251+
b = MyDtypeInstanceAttr("B")
252+
b._cache = {}
253+
254+
# Assert our assumption that the property cache actually uses an attribute called _cache
255+
assert len(a._cache) == 0
256+
257+
assert a.cached_attr == "A"
258+
assert b.cached_attr == "B"
259+
260+
assert len(a._cache) == 1

0 commit comments

Comments
 (0)