diff --git a/mypy/expandtype.py b/mypy/expandtype.py index 7f95b2e25320..5790b717172a 100644 --- a/mypy/expandtype.py +++ b/mypy/expandtype.py @@ -541,7 +541,9 @@ def visit_tuple_type(self, t: TupleType) -> Type: if isinstance(item, UnpackType): unpacked = get_proper_type(item.type) if isinstance(unpacked, Instance): - assert unpacked.type.fullname == "builtins.tuple" + # expand_type() may be called during semantic analysis, before invalid unpacks are fixed. + if unpacked.type.fullname != "builtins.tuple": + return t.partial_fallback.accept(self) if t.partial_fallback.type.fullname != "builtins.tuple": # If it is a subtype (like named tuple) we need to preserve it, # this essentially mimics the logic in tuple_fallback(). diff --git a/test-data/unit/check-type-aliases.test b/test-data/unit/check-type-aliases.test index 61eda2bd7774..e225593312f2 100644 --- a/test-data/unit/check-type-aliases.test +++ b/test-data/unit/check-type-aliases.test @@ -1237,6 +1237,36 @@ Ta2 = TypeAliasType("Ta2", None, type_params=(Unpack[Ts],)) # E: Free type vari [builtins fixtures/tuple.pyi] +[case testGenericTypeAliasArgOfTypeAliasIsUnpackedTuple] +# flags: --python-version 3.11 +from typing_extensions import TypeAlias +from typing import TypeVarTuple, Unpack + +Ts = TypeVarTuple('Ts') +TA: TypeAlias = tuple[Unpack[Ts]] + +# Only "Test suite with py310-ubuntu, mypyc-compiled" expects: Invalid syntax; you likely need to run mypy using Python 3.11 or newer +# The other tests expect: "list[int]" cannot be unpacked (must be tuple or TypeVarTuple) +# v1: TA[*list[int]] + +v2: TA[Unpack[list[int]]] # E: "list[int]" cannot be unpacked (must be tuple or TypeVarTuple) +[builtins fixtures/tuple.pyi] + +[case testGenericTypeAliasArgOfTypeAliasIsUnpackedTuple310] +# flags: --python-version 3.10 +from typing_extensions import TypeAlias +from typing import TypeVarTuple, Unpack + +Ts = TypeVarTuple('Ts') +TA: TypeAlias = tuple[Unpack[Ts]] + +# Only "Test suite with py310-ubuntu, mypyc-compiled" expects: Invalid syntax +# The other tests expect: "list[int]" cannot be unpacked (must be tuple or TypeVarTuple) +# v1: TA[*list[int]] + +v2: TA[Unpack[list[int]]] # E: "list[int]" cannot be unpacked (must be tuple or TypeVarTuple) +[builtins fixtures/tuple.pyi] + [case testAliasInstanceNameClash] from lib import func class A: ...