Fix crashes on incorectly detected recursive aliases#18625
Merged
JukkaL merged 3 commits intopython:masterfrom Feb 7, 2025
Merged
Fix crashes on incorectly detected recursive aliases#18625JukkaL merged 3 commits intopython:masterfrom
JukkaL merged 3 commits intopython:masterfrom
Conversation
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
JukkaL
approved these changes
Feb 7, 2025
Collaborator
JukkaL
left a comment
There was a problem hiding this comment.
Looks good. The logic is complicated, but users rely on this functionality so it's good that we support it.
x612skm
pushed a commit
to x612skm/mypy-dev
that referenced
this pull request
Feb 24, 2025
Fixes python#18505 Fixes python#16757 Fixing the crash is trivial, we simply give an error on something like `type X = X`, instead of crashing. But then I looked at what people actually want to do, they want to create an alias to something currently in scope (not a meaningless no-op alias). Right now we special-case classes/aliases to allow forward references (including recursive type aliases). However, I don't think we have any clear "scoping rules" for forward references. For example: ```python class C: Y = X class X: ... class X: ... ``` where `Y` should point to, `__main__.X` or `__main__.C.X`? Moreover, before this PR forward references can take precedence over real references: ```python class X: ... class C: Y = X # this resolves to __main__.C.X class X: ... ``` After some thinking I found this is not something I can fix in a simple PR. So instead I do just two things here: * Fix the actual crashes (and other potential similar crashes). * Add minimal change to accommodate the typical use case.
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 #18505
Fixes #16757
Fixing the crash is trivial, we simply give an error on something like
type X = X, instead of crashing. But then I looked at what people actually want to do, they want to create an alias to something currently in scope (not a meaningless no-op alias). Right now we special-case classes/aliases to allow forward references (including recursive type aliases). However, I don't think we have any clear "scoping rules" for forward references. For example:where
Yshould point to,__main__.Xor__main__.C.X? Moreover, before this PR forward references can take precedence over real references:After some thinking I found this is not something I can fix in a simple PR. So instead I do just two things here: