Repository navigation
Bind descriptor accesses in find_member_simple (#22549) - #22553
Open
tony1233200 wants to merge 1 commit into
Open
tony1233200 wants to merge 1 commit into
tony1233200 wants to merge 1 commit into
Conversation
Fixes python#22549 When checking type variable bounds before type checking (e.g. for `type` statements), member lookup falls back to find_member_simple(), which returned the descriptor type itself instead of binding __get__. As a result, a class implementing a protocol via a descriptor failed the bound check, and the wrong negative result was cached and reused by later, accurate checks.
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 #22549.
A
typestatement liketype A = Box[C] | intproduced a falsepositive
Type argument "C" of "Box" must be a subtype of "P"whenCimplements the protocol
Pvia a descriptor attribute, and the wrongresult was then cached, so the later, valid check
x: P = C()failedas well.
Root cause: validating type variable bounds happens before type
checking (in
semanal_typeargs), where member lookup falls back tofind_member_simple(). That helper returned the descriptor type itselffor a class attribute declared with a descriptor type (e.g.
m: Desc),instead of the type produced by binding
__get__, so the protocolcheck compared
Descagainstdict[str, int]and failed.This makes
find_member_simple()bind descriptor accesses for instanceattribute lookups: it resolves
__get__(selecting the overload thatapplies to an instance access when overloaded), and falls back to the
previous behavior whenever the descriptor cannot be bound reliably
(no
__get__, ambiguous overloads, lvalue or class-object accesses).Tests: added regression tests in
check-protocols.testandcheck-python312.test(the latter is the exact repro from the issue),plus a test that a genuinely incompatible descriptor still errors.
Test report:
testcheck.py(protocols, python312, generics, generic-subtyping,generic-alias, type-aliases): 701 passed, 4 skipped.
testsubtypes.py: 26 passed.testcheck.pysuite: 8304 passed, 45 skipped, 7 xfailed.blackandruffonmypy/subtypes.py: clean.Note: this PR was prepared with LLM assistance.