Skip to content

Commit 9542fd4

Browse files
Fix: internal error caused by the generic type alias with an unpacked list (python#20689)
Fix: python#20549 --------- Co-authored-by: Ivan Levkivskyi <levkivskyi@gmail.com>
1 parent 5f3e6ec commit 9542fd4

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

mypy/expandtype.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,9 @@ def visit_tuple_type(self, t: TupleType) -> Type:
541541
if isinstance(item, UnpackType):
542542
unpacked = get_proper_type(item.type)
543543
if isinstance(unpacked, Instance):
544-
assert unpacked.type.fullname == "builtins.tuple"
544+
# expand_type() may be called during semantic analysis, before invalid unpacks are fixed.
545+
if unpacked.type.fullname != "builtins.tuple":
546+
return t.partial_fallback.accept(self)
545547
if t.partial_fallback.type.fullname != "builtins.tuple":
546548
# If it is a subtype (like named tuple) we need to preserve it,
547549
# this essentially mimics the logic in tuple_fallback().

test-data/unit/check-python311.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,15 @@ def exc_name() -> None:
321321
except* RuntimeError as e:
322322
e = fn_exc(e)
323323
[builtins fixtures/exception.pyi]
324+
325+
[case testGenericTypeAliasArgOfTypeAliasIsUnpackedTuple]
326+
from typing_extensions import TypeAlias
327+
from typing import TypeVarTuple, Unpack
328+
329+
Ts = TypeVarTuple('Ts')
330+
TA: TypeAlias = tuple[Unpack[Ts]]
331+
332+
v1: TA[*list[int]] # E: "list[int]" cannot be unpacked (must be tuple or TypeVarTuple)
333+
334+
v2: TA[Unpack[list[int]]] # E: "list[int]" cannot be unpacked (must be tuple or TypeVarTuple)
335+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)