Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer type analysis of integer Literal when builtins.int is slow to resolve #18046

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

tungol
Copy link
Contributor

@tungol tungol commented Oct 25, 2024

This MR fixes two possible crashes related to builtins.int not being ready yet when a Literal is analyzed. One happens if builtins.int is entirely unavailable, and the other if builtins.int is only available as a placeholder node.

The first case is responsible for the crash if typeshed imports Literal from typing instead of typing_extensions. Mypy has Literal available during the first pass of builtins.pyi, which means it encounters Literal[0] before it's seen the class int statement: python/typeshed#11247

The second case occurs if builtins.int is a subclass of something that's slow to resolve, which turns up if you try to make builtins.int inherit from numbers.Integral: python/typeshed#12894

Both 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 within SemanticAnalyzer.visit_assignment_stmt fixes that. This didn't happen before because typing_extensions.Literal and typing_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.

This comment has been minimized.

@tungol

This comment was marked as outdated.

@tungol

This comment was marked as outdated.

accidentally got it into a form that didn't reproduce the crash on master
it's important the int is declared after the literals
@tungol tungol marked this pull request as ready for review October 27, 2024 00:58
@tungol tungol marked this pull request as draft October 27, 2024 01:16

This comment has been minimized.

Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@tungol tungol marked this pull request as ready for review October 27, 2024 02:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant