-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix mypyc crash with enum type aliases #18725
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from __future__ import annotations | ||
|
||
from mypy.nodes import Expression, Node | ||
from mypy.traverser import ExtendedTraverserVisitor | ||
from mypy.types import AnyType, Type, TypeOfAny | ||
|
||
|
||
class MissingTypesVisitor(ExtendedTraverserVisitor): | ||
"""AST visitor that can be used to add any missing types as a generic AnyType.""" | ||
|
||
def __init__(self, types: dict[Expression, Type]) -> None: | ||
super().__init__() | ||
self.types: dict[Expression, Type] = types | ||
|
||
def visit(self, o: Node) -> bool: | ||
if isinstance(o, Expression) and o not in self.types: | ||
self.types[o] = AnyType(TypeOfAny.special_form) | ||
|
||
# If returns True, will continue to nested nodes. | ||
return True |
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also test nested
Literal
types that aren't nested within an IndexExpr, such asLiteral[<...>] | None
. You must target 3.10 to enable the new style union type syntax. I think you can add_python3_10
suffix to the test case name to only run it on 3.10 and later.Test explicit type aliases (e.g.
AliasExplicit: TypeAlias = Literal[<...>]
).Test type alias statements (e.g.
type Alias = Literal[<...>]
). Since it only works on 3.12 and later, it's best to add it totestPEP695Basics
inmypyc/test-data/run-python312.test
. It already has some similar test cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Literal[<...>] | None
didn't seem to work:Even with
[typing fixtures/typing-full.pyi]
.I haven't seen any other test use the
X | Y
form of type definition. I think we should leave the supporting ofX | Y
types as a separate issue.I'll add it in run-python312.test since it seems to be fine there.
Separately, is there any reason why we can't have
Test_python3_12
tests and we must userun-python312.test
?I think we should update the testing README.md with the differences between
Test_python3_12
,run-python312.test
and# flags: --python-version 3.12
. I realize that the last one is just for mypy, not for mypyc, but might be worth documenting the differences in both set of tests.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, it's fine to support
X | Y
in a follow-up issue. It might be a stub issue -- maybe addingimport types
to the test case would help to simulate how the real typeshed stubs work.