Fix class access to functools.cached_property attributes - #21858
Open
aryansk wants to merge 1 commit into
Open
Conversation
Accessing a functools.cached_property through the class object (e.g. `cls.value.attrname` inside a classmethod) previously exposed the getter as a bare callable, so descriptor attributes like `attrname` and `func` were reported as missing. At runtime the value is the `cached_property` instance itself, so type it as such and let the descriptor machinery (`__get__(None, owner) -> Self`) apply.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #21825.
What
Accessing a
functools.cached_propertythrough the class object was typed as the raw getter callable, so descriptor attributes were reported as missing:At runtime
A.valueis thecached_propertyinstance itself (it hasattrname,func, etc.). This PR types class access as the descriptor and lets the existing descriptor machinery apply typeshed's__get__(self, instance: None, ...) -> Selfoverload:How
In
analyze_class_attribute_access, when the member is decorated withfunctools.cached_propertyand accessed as a value (not an lvalue), feed the descriptor instance type (cached_property[<getter return>]) intoanalyze_descriptor_accessinstead of the bare callable. Instance access is unchanged (already went through the descriptor path), and lvalue behavior (e.g.A.value = 5) is unchanged.Test
Added
testCachedPropertyClassAccesstotest-data/unit/check-functools.test; it fails on main and passes with this change. Fulltestchecksuite passes (8178 passed, 33 skipped, 7 xfailed).