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

Prevent crash with Unpack of a fixed tuple in PEP695 type alias #18451

Merged
merged 5 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions mypy/mixedtraverser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
NamedTupleExpr,
NewTypeExpr,
PromoteExpr,
TypeAlias,
TypeAliasExpr,
TypeAliasStmt,
TypeApplication,
TypedDictExpr,
TypeVarExpr,
Expand Down Expand Up @@ -48,9 +50,7 @@ def visit_class_def(self, o: ClassDef, /) -> None:

def visit_type_alias_expr(self, o: TypeAliasExpr, /) -> None:
super().visit_type_alias_expr(o)
self.in_type_alias_expr = True
o.node.target.accept(self)
self.in_type_alias_expr = False
o.node.accept(self)

def visit_type_var_expr(self, o: TypeVarExpr, /) -> None:
super().visit_type_var_expr(o)
Expand Down Expand Up @@ -81,6 +81,17 @@ def visit_assignment_stmt(self, o: AssignmentStmt, /) -> None:
super().visit_assignment_stmt(o)
self.visit_optional_type(o.type)

def visit_type_alias_stmt(self, o: TypeAliasStmt, /) -> None:
super().visit_type_alias_stmt(o)
if o.alias_node is not None:
o.alias_node.accept(self)

def visit_type_alias(self, o: TypeAlias, /) -> None:
super().visit_type_alias(o)
self.in_type_alias_expr = True
o.target.accept(self)
self.in_type_alias_expr = False

def visit_for_stmt(self, o: ForStmt, /) -> None:
super().visit_for_stmt(o)
self.visit_optional_type(o.index_type)
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-python312.test
Original file line number Diff line number Diff line change
Expand Up @@ -1972,3 +1972,19 @@ class D:
class G[Q]:
def g(self, x: Q): ...
d: G[str]

[case testTypeAliasNormalization]
from collections.abc import Callable
from typing import Unpack
from typing_extensions import TypeAlias

type RK_function_args = tuple[float, int]
type RK_functionBIS = Callable[[Unpack[RK_function_args], int], int]

def ff(a: float, b: int, c: int) -> int:
return 2

bis: RK_functionBIS = ff
res: int = bis(1.0, 2, 3)
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]
Loading