-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
[mypyc] Support Python 3.12 type alias syntax (PEP 695) #17384
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9ddad1c
Support non-generic type aliases
JukkaL e045e1d
Infer typing.TypeVAr as the type of a type var reference in runtime c…
JukkaL 903c330
Update failing tests
JukkaL 781f2a5
Prefix type variables with underscore in builtins fixtures
JukkaL 63c0ff7
Update another test case
JukkaL fecfa80
Support generic type aliases
JukkaL d009aa5
Set infer_variance=True at runtime
JukkaL 6d250f4
Fix self check
JukkaL 5468930
Refactor
JukkaL d0b2690
Fix a few tests
JukkaL 6510319
Add docstring
JukkaL af3f058
Minor tweaks
JukkaL 2a4d7dd
Merge branch 'master' into mypyc-type-aliases
JukkaL 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 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 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 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 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 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 |
---|---|---|
|
@@ -3765,6 +3765,10 @@ def analyze_alias( | |
last_tvar_name_with_default = tvar_def.name | ||
tvar_defs.append(tvar_def) | ||
|
||
if python_3_12_type_alias: | ||
with self.allow_unbound_tvars_set(): | ||
rvalue.accept(self) | ||
Comment on lines
+3768
to
+3770
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have been fixed by #17404. Can you try it out? |
||
|
||
analyzed, depends_on = analyze_type_alias( | ||
typ, | ||
self, | ||
|
@@ -5359,7 +5363,7 @@ def visit_type_alias_stmt(self, s: TypeAliasStmt) -> None: | |
tag = self.track_incomplete_refs() | ||
res, alias_tvars, depends_on, qualified_tvars, empty_tuple_index = self.analyze_alias( | ||
s.name.name, | ||
s.value, | ||
s.value.expr(), | ||
allow_placeholder=True, | ||
declared_type_vars=type_params, | ||
all_declared_type_params_names=all_type_params_names, | ||
|
@@ -5442,6 +5446,7 @@ def visit_type_alias_stmt(self, s: TypeAliasStmt) -> None: | |
current_node = existing.node if existing else alias_node | ||
assert isinstance(current_node, TypeAlias) | ||
self.disable_invalid_recursive_aliases(s, current_node, s.value) | ||
s.name.accept(self) | ||
finally: | ||
self.pop_type_args(s.type_args) | ||
|
||
|
@@ -5456,7 +5461,11 @@ def visit_name_expr(self, expr: NameExpr) -> None: | |
|
||
def bind_name_expr(self, expr: NameExpr, sym: SymbolTableNode) -> None: | ||
"""Bind name expression to a symbol table node.""" | ||
if isinstance(sym.node, TypeVarExpr) and self.tvar_scope.get_binding(sym): | ||
if ( | ||
isinstance(sym.node, TypeVarExpr) | ||
and self.tvar_scope.get_binding(sym) | ||
and not self.allow_unbound_tvars | ||
): | ||
self.fail(f'"{expr.name}" is a type variable and only valid in type context', expr) | ||
elif isinstance(sym.node, PlaceholderNode): | ||
self.process_placeholder(expr.name, "name", expr) | ||
|
This file contains 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 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 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 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 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 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 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 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
Oops, something went wrong.
Oops, something went wrong.
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.
Will this cause crashes on Python versions where
TypeVar
is not a proper class? And/or if it is imported fromtyping_extensions
(e.g. by people who want the type variables with defaults)?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.
TypeVar has been a class since at least 3.6, for what it's worth: https://github.com/python/cpython/blob/8d999cbf4adea053be6dbb612b9844635c4dfb8e/Lib/typing.py#L453.
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.
TypeVar
is defined unconditionally as a class in typeshed:Also on the oldest supported Python version (3.8)
TypeVar
is a class:But this won't be right if
TypeVar
is imported fromtyping_extensions
. It probably won't usually make a difference, but it's possible to imagine runtime introspection use cases where mypy could get confused. But this wasn't properly supported before (when the type wasobject
), so it doesn't seem like a major issue. I may fix this in a follow-up PR if it's easy enough.