| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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.
| Back | FazBrowse Home | New Git URL |
Fixes #15717
Binding a classmethod to a second name makes stubtest reject a stub that matches the runtime exactly:
The plain-method alias b is fine; only the classmethod alias errors.
Cause
Both sides are right, and the comparison is what is wrong. d = c in the stub names the classmethod object, whose type keeps the cls argument, whereas reading T.d at runtime goes through the descriptor and yields a method already bound to the class. verify_var then 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_var now compares against the stub type with its leading cls removed.
The check is deliberately narrow. It applies only when:
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: int against a runtime method is still an error, and there is a test for it.
Tests
Two cases added to test_static_class_method:
mypy/test/teststubtest.py passes (67 tests), self-check on mypy/stubtest.py is clean, and ruff / black are clean.
Note
The enum branch of verify_var now reads the same local variable rather than stub.type directly. The value is identical there — an enum member is never a bound method — but self-check cannot narrow stub.type to non-None once the surrounding condition tests the local.