Defer type analysis of integer Literal when builtins.int is slow to resolve #18046
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.
This MR fixes two possible crashes related to
builtins.int
not being ready yet when aLiteral
is analyzed. One happens ifbuiltins.int
is entirely unavailable, and the other ifbuiltins.int
is only available as a placeholder node.The first case is responsible for the crash if typeshed imports Literal from
typing
instead oftyping_extensions
. Mypy hasLiteral
available during the first pass ofbuiltins.pyi
, which means it encountersLiteral[0]
before it's seen theclass int
statement: python/typeshed#11247The second case occurs if
builtins.int
is a subclass of something that's slow to resolve, which turns up if you try to makebuiltins.int
inherit fromnumbers.Integral
: python/typeshed#12894Both cases are resolved by deferring during
TypeAnalyser.analyze_literal_param
if the type of the literal isn't ready yet.After fixing those, an additional issue became apparent, where the r.h.s of a TypeAlias assignment is ready but
TypeAlias
itself isn't ready yet, which caused mypy to say it wasn't a valid TypeAlias. An additional check and defer withinSemanticAnalyzer.visit_assignment_stmt
fixes that. This didn't happen before becausetyping_extensions.Literal
andtyping_extensions.TypeAlias
resolved at the same speed, so it wasn't noticeable. Some problems shows up for deferred on any unresolved lhs type, so I restricted it to only defer for unresolved lhs types which were imported from typing or typing_extensions.