Fix stubtest false positive for an alias of a classmethod - #21863
Open
arose26 wants to merge 1 commit into
Open
Conversation
A stub assignment such as d = c refers to the classmethod object and keeps its cls argument, while reading the same name off the class at runtime yields a method already bound to it.
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 #15717
Binding a classmethod to a second name makes stubtest reject a stub that matches the runtime exactly:
The plain-method alias
bis fine; only the classmethod alias errors.Cause
Both sides are right, and the comparison is what is wrong.
d = cin the stub names the classmethod object, whose type keeps theclsargument, whereas readingT.dat runtime goes through the descriptor and yields a method already bound to the class.verify_varthen compares an unbound signature against a bound one and reports a difference that does not exist.Change
When the runtime value is a method bound to a class,
verify_varnow compares against the stub type with its leadingclsremoved.The check is deliberately narrow. It applies only when:
__self__is a class — an instance method bound to an instance is untouched,type[...], or is namedcls/mcls/metacls.So a stub that already accounts for the binding, or one whose first parameter is a genuine value parameter, keeps being compared as before. A real mismatch still fails: annotating the alias as
c_alias: intagainst a runtime method is still an error, and there is a test for it.Tests
Two cases added to
test_static_class_method:intin the stub: still an errormypy/test/teststubtest.pypasses (67 tests), self-check onmypy/stubtest.pyis clean, andruff/blackare clean.Note
The enum branch of
verify_varnow reads the same local variable rather thanstub.typedirectly. The value is identical there — an enum member is never a bound method — but self-check cannot narrowstub.typeto non-Noneonce the surrounding condition tests the local.