Skip to content

Commit fb7b254

Browse files
authoredJan 15, 2025
Prevent crash with Unpack of a fixed tuple in PEP695 type alias (#18451)
Fixes #18309. Add missing `visit_type_alias_stmt()` implementation to mixedtraverser.py to visit the alias target directly.
1 parent 5e119d0 commit fb7b254

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed
 

‎mypy/mixedtraverser.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
NamedTupleExpr,
1111
NewTypeExpr,
1212
PromoteExpr,
13+
TypeAlias,
1314
TypeAliasExpr,
15+
TypeAliasStmt,
1416
TypeApplication,
1517
TypedDictExpr,
1618
TypeVarExpr,
@@ -48,9 +50,7 @@ def visit_class_def(self, o: ClassDef, /) -> None:
4850

4951
def visit_type_alias_expr(self, o: TypeAliasExpr, /) -> None:
5052
super().visit_type_alias_expr(o)
51-
self.in_type_alias_expr = True
52-
o.node.target.accept(self)
53-
self.in_type_alias_expr = False
53+
o.node.accept(self)
5454

5555
def visit_type_var_expr(self, o: TypeVarExpr, /) -> None:
5656
super().visit_type_var_expr(o)
@@ -81,6 +81,17 @@ def visit_assignment_stmt(self, o: AssignmentStmt, /) -> None:
8181
super().visit_assignment_stmt(o)
8282
self.visit_optional_type(o.type)
8383

84+
def visit_type_alias_stmt(self, o: TypeAliasStmt, /) -> None:
85+
super().visit_type_alias_stmt(o)
86+
if o.alias_node is not None:
87+
o.alias_node.accept(self)
88+
89+
def visit_type_alias(self, o: TypeAlias, /) -> None:
90+
super().visit_type_alias(o)
91+
self.in_type_alias_expr = True
92+
o.target.accept(self)
93+
self.in_type_alias_expr = False
94+
8495
def visit_for_stmt(self, o: ForStmt, /) -> None:
8596
super().visit_for_stmt(o)
8697
self.visit_optional_type(o.index_type)

‎test-data/unit/check-python312.test

+16
Original file line numberDiff line numberDiff line change
@@ -1972,3 +1972,19 @@ class D:
19721972
class G[Q]:
19731973
def g(self, x: Q): ...
19741974
d: G[str]
1975+
1976+
[case testTypeAliasNormalization]
1977+
from collections.abc import Callable
1978+
from typing import Unpack
1979+
from typing_extensions import TypeAlias
1980+
1981+
type RK_function_args = tuple[float, int]
1982+
type RK_functionBIS = Callable[[Unpack[RK_function_args], int], int]
1983+
1984+
def ff(a: float, b: int, c: int) -> int:
1985+
return 2
1986+
1987+
bis: RK_functionBIS = ff
1988+
res: int = bis(1.0, 2, 3)
1989+
[builtins fixtures/tuple.pyi]
1990+
[typing fixtures/typing-full.pyi]

0 commit comments

Comments
 (0)