Skip to content

Commit 14b3493

Browse files
committed
Fix IndexError when unpacking empty tuple in type alias
When expanding type arguments for a builtins.tuple instance, the normalization code accessed args[0] without checking if args was non-empty. This caused an IndexError when Unpack[tuple[()]] was used in a type alias, since the empty tuple expansion produces zero args. Fixes python#20913
1 parent 1d8ddd5 commit 14b3493

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎mypy/expandtype.py‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def visit_instance(self, t: Instance) -> Type:
223223
# See: https://github.com/python/mypy/issues/16649
224224
return t.copy_modified(args=args)
225225

226-
if t.type.fullname == "builtins.tuple":
226+
if t.type.fullname == "builtins.tuple" and len(args) > 0:
227227
# Normalize Tuple[*Tuple[X, ...], ...] -> Tuple[X, ...]
228228
arg = args[0]
229229
if isinstance(arg, UnpackType):
@@ -519,7 +519,15 @@ def expand_type_list_with_unpack(self, typs: list[Type]) -> list[Type]:
519519
if isinstance(item, UnpackType) and isinstance(item.type, TypeVarTupleType):
520520
items.extend(self.expand_unpack(item))
521521
else:
522-
items.append(item.accept(self))
522+
expanded = item.accept(self)
523+
if isinstance(expanded, UnpackType) and isinstance(
524+
expanded.type, TupleType
525+
):
526+
# Inline Unpack[tuple[X, Y]] -> X, Y
527+
# This handles e.g. Unpack[tuple[()]] -> nothing
528+
items.extend(expanded.type.items)
529+
continue
530+
items.append(expanded)
523531
return items
524532

525533
def expand_type_tuple_with_unpack(self, typs: tuple[Type, ...]) -> list[Type]:
@@ -530,7 +538,15 @@ def expand_type_tuple_with_unpack(self, typs: tuple[Type, ...]) -> list[Type]:
530538
if isinstance(item, UnpackType) and isinstance(item.type, TypeVarTupleType):
531539
items.extend(self.expand_unpack(item))
532540
else:
533-
items.append(item.accept(self))
541+
expanded = item.accept(self)
542+
if isinstance(expanded, UnpackType) and isinstance(
543+
expanded.type, TupleType
544+
):
545+
# Inline Unpack[tuple[X, Y]] -> X, Y
546+
# This handles e.g. Unpack[tuple[()]] -> nothing
547+
items.extend(expanded.type.items)
548+
continue
549+
items.append(expanded)
534550
return items
535551

536552
def visit_tuple_type(self, t: TupleType) -> Type:

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,3 +2237,17 @@ 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 testUnpackEmptyTupleInTypeAliasNoCrash]
2242+
# https://github.com/python/mypy/issues/20913
2243+
from typing import Unpack
2244+
2245+
class C[*Ts]:
2246+
pass
2247+
2248+
type T[T, *Ts] = C[*Ts]
2249+
2250+
x: T[bool, Unpack[tuple[()]]]
2251+
reveal_type(x) # N: Revealed type is "__main__.C[Unpack[builtins.tuple]]"
2252+
[builtins fixtures/tuple.pyi]
2253+
[typing fixtures/typing-full.pyi]

0 commit comments

Comments
 (0)