Skip to content

Commit

Permalink
Prevent crash with Unpack of a fixed tuple in PEP695 type alias (#18451)
Browse files Browse the repository at this point in the history
Fixes #18309.

Add missing `visit_type_alias_stmt()` implementation to
mixedtraverser.py to visit the alias target directly.
  • Loading branch information
sterliakov authored Jan 15, 2025
1 parent 5e119d0 commit fb7b254
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
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]

0 comments on commit fb7b254

Please sign in to comment.