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-python311.test b/test-data/unit/check-python311.test index 42bdb72411ec..51b47b6746fd 100644 --- a/test-data/unit/check-python311.test +++ b/test-data/unit/check-python311.test @@ -321,3 +321,15 @@ def exc_name() -> None: except* RuntimeError as e: e = fn_exc(e) [builtins fixtures/exception.pyi] + +[case testGenericTypeAliasArgOfTypeAliasIsUnpackedTuple] +from typing_extensions import TypeAlias +from typing import TypeVarTuple, Unpack + +Ts = TypeVarTuple('Ts') +TA: TypeAlias = tuple[Unpack[Ts]] + +v1: TA[*list[int]] # E: "list[int]" cannot be unpacked (must be tuple or TypeVarTuple) + +v2: TA[Unpack[list[int]]] # E: "list[int]" cannot be unpacked (must be tuple or TypeVarTuple) +[builtins fixtures/tuple.pyi]