File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 4
4
from pandas ._libs import (
5
5
Timestamp ,
6
6
lib ,
7
+ properties ,
7
8
writers as libwriters ,
8
9
)
9
10
@@ -212,3 +213,48 @@ def test_no_default_pickle():
212
213
# GH#40397
213
214
obj = tm .round_trip_pickle (lib .no_default )
214
215
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
You can’t perform that action at this time.
0 commit comments