Skip to content

Commit 4455546

Browse files
committed
Fix PEP 695 generic type alias reassignment
1 parent 03b12a1 commit 4455546

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

mypy/typeanal.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2150,6 +2150,9 @@ def instantiate_type_alias(
21502150
and act_len == 0
21512151
and not (empty_tuple_index and node.tvar_tuple_index is not None)
21522152
):
2153+
if node.python_3_12_type_alias:
2154+
# Pass original type variables to preserve genericity in the alias reference
2155+
return TypeAliasType(node, list(node.alias_tvars), line=ctx.line, column=ctx.column)
21532156
# Interpret bare Alias same as normal generic, i.e., Alias[Any, Any, ...]
21542157
return set_any_tvars(
21552158
node,
@@ -2167,7 +2170,6 @@ def instantiate_type_alias(
21672170
# Note: this is the only case where we use an eager expansion. See more info about
21682171
# no_args aliases like L = List in the docstring for TypeAlias class.
21692172
return Instance(node.target.type, [], line=ctx.line, column=ctx.column)
2170-
return TypeAliasType(node, [], line=ctx.line, column=ctx.column)
21712173
if (
21722174
max_tv_count == 0
21732175
and act_len > 0

test-data/unit/check-python312.test

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,7 @@ class D(C): pass
14791479

14801480
type A[T: C] = list[T]
14811481
a1: A
1482-
reveal_type(a1) # N: Revealed type is "builtins.list[Any]"
1482+
reveal_type(a1) # N: Revealed type is "builtins.list[T`1]"
14831483
a2: A[Any]
14841484
a3: A[C]
14851485
a4: A[D]
@@ -1491,7 +1491,7 @@ x2 = cast(A[None], a1) # E: Type argument "None" of "A" must be a subtype of "C
14911491

14921492
type A2[T: (int, C)] = list[T]
14931493
b1: A2
1494-
reveal_type(b1) # N: Revealed type is "builtins.list[Any]"
1494+
reveal_type(b1) # N: Revealed type is "builtins.list[T`1]"
14951495
b2: A2[Any]
14961496
b3: A2[int]
14971497
b4: A2[C]
@@ -2237,3 +2237,24 @@ class D[*Ts](Generic[Unpack[Us]]): # E: Generic[...] base class is redundant \
22372237
# E: Can only use one type var tuple in a class def
22382238
pass
22392239
[builtins fixtures/tuple.pyi]
2240+
2241+
[case testPep695GenericTypeAliasReassignmentAndUsage]
2242+
import types
2243+
from typing import reveal_type
2244+
2245+
type A[T] = list[T] | str
2246+
2247+
# 1. Assignment (The Fix)
2248+
B = A
2249+
reveal_type(B) # N: Revealed type is "types.UnionType[builtins.list[T`1], builtins.str]"
2250+
2251+
# 2. Usage (The Regression Check)
2252+
def foo(x: B[int]) -> None: # E: Bad number of arguments for type alias, expected 0, given 1
2253+
reveal_type(x) # N: Revealed type is "Union[builtins.list[T`1], builtins.str]"
2254+
2255+
[file types.pyi]
2256+
class UnionType: ...
2257+
class GenericAlias: ...
2258+
2259+
[builtins fixtures/list.pyi]
2260+
[typing fixtures/typing-full.pyi]

0 commit comments

Comments
 (0)