New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cached_property no longer works as a data descriptor in Python 3.12 #106292
Comments
Cc. @carljm |
Thanks @treyhunner for the report! Yeah, this was unintended and undocumented behavior pre-3.12, that was an accidental side effect of the locking. We could restore this behavior, but it penalizes performance (double-checks the dict unnecessarily) in the common case (use of I would prefer to document this behavior change in What's New. I can do a code search for subclasses of |
@carljm given that this feature was unintended, documenting the change in What's New sounds reasonable to me. 👍 That code example is based on a Python exercise where I discuss how |
I went through all 47 hits on sourcegraph code search for regex I can submit a PR to Dask before the release of 3.12 to fix this case, but this does suggest that it's possible other people are also doing this, in codebases we don't have access to. I've realized several things that are swaying my opinion here:
So I am now thinking it may be the safest option to just re-add the extra dict check in |
) * gh-106292: restore checking __dict__ in cached_property.__get__ Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
…pythonGH-106380) * pythongh-106292: restore checking __dict__ in cached_property.__get__ (cherry picked from commit 838406b) Co-authored-by: Carl Meyer <carl@oddbird.net> Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
Bug report
In Python 3.11 and below, when
cached_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 of
cached_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 called before the__dict__
attribute is accessed.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 that
cached_property
could be inherited from and turned into a data descriptor.Your environment
Python 3.12.0b3 on Ubuntu Linux
Linked PRs
The text was updated successfully, but these errors were encountered: