Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Description
Bug report
In Python 3.11 and below, whencached_property was inherited from, the__get__ method would always check for the attribute in the cache.
In Python 3.12.0b3 (since#101890 it seems) the__get__ method no longer checks the cache.
This isn't an issue for typical use, but it might be an issue for subclasses.
For example here's a version ofcached_property that inherits from thefunctools version but also allows for asetter (just as@property does).
Since this child class adds a__set__ method, the__get__ method infunctools.cached_property will be calledbefore the__dict__ attribute is accessed.
"""Demonstration of cached_property difference in Python 3.12.0b3."""importfunctoolsclasssettable_cached_property(functools.cached_property):def__init__(self,func):super().__init__(func)self._setter=self._deleter=Nonedefsetter(self,setter):self._setter=setterreturnselfdef__set__(self,obj,value):ifself._setter:self._setter(obj,value)obj.__dict__.pop(self.attrname,None)else:obj.__dict__[self.attrname]=valueclassThing:@settable_cached_propertydefx(self):returnself.ything=Thing()thing.y=4print(f"{thing.x=} (should be 4)")thing.y=5print(f"{thing.x=} (should still be 4)")
This new behavior may be intended, but I wanted to make a note of it because it does break a previous (undocumented I believe?) assumption thatcached_property could be inherited from and turned into a data descriptor.
Your environment
Python 3.12.0b3 on Ubuntu Linux