diff options
author | Carl Meyer <carl@oddbird.net> | 2023-07-05 17:01:35 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-05 17:01:35 -0600 |
commit | 838406b4fc044c0b2f397c23275c69f16a76205b (patch) | |
tree | bf782499c256dba23947916be1dcb48df20e202d /Lib/functools.py | |
parent | 217f47d6e5e56bca78b8556e910cd00890f6f84a (diff) | |
download | cpython-838406b4fc044c0b2f397c23275c69f16a76205b.tar.gz cpython-838406b4fc044c0b2f397c23275c69f16a76205b.zip |
gh-106292: restore checking __dict__ in cached_property.__get__ (#106380)
* gh-106292: restore checking __dict__ in cached_property.__get__
Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
Diffstat (limited to 'Lib/functools.py')
-rw-r--r-- | Lib/functools.py | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/Lib/functools.py b/Lib/functools.py index 4d5e2709007..8518450a8d4 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -957,9 +957,10 @@ class singledispatchmethod: ################################################################################ -### cached_property() - computed once per instance, cached as attribute +### cached_property() - property result cached as instance attribute ################################################################################ +_NOT_FOUND = object() class cached_property: def __init__(self, func): @@ -990,15 +991,17 @@ class cached_property: f"instance to cache {self.attrname!r} property." ) raise TypeError(msg) from None - val = self.func(instance) - try: - cache[self.attrname] = val - except TypeError: - msg = ( - f"The '__dict__' attribute on {type(instance).__name__!r} instance " - f"does not support item assignment for caching {self.attrname!r} property." - ) - raise TypeError(msg) from None + val = cache.get(self.attrname, _NOT_FOUND) + if val is _NOT_FOUND: + val = self.func(instance) + try: + cache[self.attrname] = val + except TypeError: + msg = ( + f"The '__dict__' attribute on {type(instance).__name__!r} instance " + f"does not support item assignment for caching {self.attrname!r} property." + ) + raise TypeError(msg) from None return val __class_getitem__ = classmethod(GenericAlias) |