Repository navigation
stubgen: preserve type alias definitions when TypeAlias is imported from another module - #22383
Open
Shanmukh-l33t wants to merge 1 commit into
Open
Shanmukh-l33t wants to merge 1 commit into
Shanmukh-l33t wants to merge 1 commit into
Conversation
stubgen runs mypy with follow_imports=skip, so a TypeAlias name that is imported from another module (e.g. a version compatibility shim) is never resolved back to typing.TypeAlias. The analyzed annotation then degrades to Any, visit_assignment_stmt() no longer treats the assignment as an explicit (PEP 613) type alias, and the right-hand side is dropped from the generated stub. Fall back to the annotation name as written in the source when deciding whether the assignment is an explicit type alias. Fixes python#22294
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 #22294
Problem
stubgenloses the right-hand side of an explicit (PEP 613) type alias whenTypeAliasis imported from another module rather than directly fromtyping/typing_extensions— e.g. a version-compatibility shim:stubgen example.pyproduced:instead of preserving the alias definition:
Cause
mypy_options()runs mypy withfollow_imports="skip", so the re-exportedTypeAliasname is never resolved back totyping.TypeAlias.SemanticAnalyzer.is_pep_613()therefore fails, the statement is analysed asa plain variable, and
AssignmentStmt.typedegrades toAny(instead ofstaying as the unanalysed
UnboundType("TypeAlias"), which is what happenswhen the annotation resolves normally and the statement returns early from
check_and_set_up_type_alias()).ASTStubGenerator.visit_assignment_stmt()only recognised the explicit aliasvia
o.type.name == "TypeAlias", which is nowAny, so the assignment fellthrough to the plain-variable path and the value was dropped from the stub.
Solution
Keep the existing check on the analysed annotation and, when it doesn't carry
the name, fall back to the annotation name as written in the source
(
o.unanalyzed_type). No other code paths are touched: implicit aliases(
X = ...) and ordinary annotated assignments behave exactly as before.Impact
TypeAliasis imported fromtypingortyping_extensions, or for any non-TypeAliasannotation.Testing
Regression test added:
StubgenTypeAliasSuite .test_explicit_type_alias_imported_from_other_moduleinmypy/test/teststubgen.py(reproduces the issue exactly — the compatibilitymodule is not a stubgen target, so
follow_imports=skipapplies to it).It uses a Python test rather than a
stubgen.testdata case because the dataharness passes
-mfor every[file ...], which would make the shim a stubgentarget and hide the bug.
RED (before the fix):
GREEN (after the fix):
The full test suite was not run locally (mypy's suite is large; CI covers it).
Manual end-to-end check with the exact reproducer from the issue now yields: