Keep the qualified name of a nested class in a stub - #1422
Merged
Conversation
Inside a class body, stubgen stripped the prefix of the enclosing class and wrote the short name of a nested class. The two type checkers do not agree on such a name: mypy reads it in the scope of the class, while pyright reads it as the module level name. A module that holds both 'Sibling' and 'MyClass.Sibling' therefore gets a wrong type from pyright, with no message that points at the stub. The name now stays qualified, which both checkers resolve to the nested class. The test suite gains a module level class that collides with a nested one.
Owner
|
Ugh, looks like you are right. At the time, I tried the offending example code from #1333 in PyRight, but I think I did so after saving it into a Turns out both PyRight and MyPy are perfectly happy with the nested type names when they occur in a |
Contributor
|
apologies for the churn - fwiw this does look to be a problem for my case still, but is perhaps an issue/disparate behaviour specifically with |
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.
Inside a class body,
stubgenstrips the prefix of the enclosing class and writes the short name of a nested class (stubgen.py:844). The two type checkers do not agree on what that name means:mypy reads the short name in the scope of the class, and pyright reads it as the module level name. Nothing in the message points at the stub, and the type is silently wrong whenever the module holds a class of that name.
A qualified name has no such ambiguity, and both checkers accept it:
Relation to #1333
The rule came from #1333, which states that a type checker cannot resolve
EnumWrapper.Valueinside the body ofEnumWrapperand reportsUnknown. A stub is never executed, so neither checker has that problem today:The generated
test_enum_ext.pyigives the same result: pyright reports the identical set of 8 diagnostics with the short and the qualified name, and mypy the same 4. Both sets are unrelated to the names of the nested classes.