From 4fcf2d63d96d573ae19682eee73d9eeadfbc7f73 Mon Sep 17 00:00:00 2001 From: Vishwas Date: Fri, 27 Nov 2026 12:38:06 +0000 Subject: [PATCH] Check starred args with variadic items against fixed formals as a whole A starred argument whose type is a variable-length iterable of variadic items (e.g. tuple[tuple[*Ts], ...], the type of *composed when annotated *composed: tuple[*Ts]) previously had each of its items checked against a single fixed formal, silently accepting calls that should be rejected because the unpacked item count cannot match a fixed-arity signature. In check_argument_types, when such an ARG_STAR actual maps to a non-star formal, check the iterable as a whole against the formal instead of expanding it item-by-item. This catches python#22567: passing *composed (tuple[tuple[*Ts], ...]) to func(composed: tuple[*Ts]) now reports an incompatible-argument error instead of succeeding. Add a regression test case (testTypeVarTupleUnpackedStarArgIsCheckedIssue22567) in check-typevar-tuple.test. --- mypy/checkexpr.py | 52 +++++++++++++++++++++++++ test-data/unit/check-typevar-tuple.test | 21 ++++++++++ 2 files changed, 73 insertions(+) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index c48f3da51f85e..859a47ed136cd 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -2953,6 +2953,30 @@ def check_argument_types( actuals, actual_types, actual_kinds, callee_arg_types, callee_arg_kinds ): # Check that a *arg is valid as varargs. + if ( + actual_kind == ARG_STAR + and not callee_arg_kind.is_star() + and _is_variadic_item_iterable(actual_type) + ): + # A starred argument with a variable-length iterable whose + # items are themselves variadic (e.g. tuple[tuple[*Ts], ...]) + # cannot be safely expanded item-by-item against fixed + # formals: the item count is unknown and each item absorbs a + # variable number of positions, so no item-to-formal mapping + # can be trusted. Check the iterable as a whole instead. + check_arg( + actual_type, + actual_type, + actual_kind, + callee_arg_type, + actual + 1, + i + 1, + callee, + object_type, + args[actual], + context, + ) + continue expanded_actual = mapper.expand_actual_type( actual_type, actual_kind, @@ -6898,6 +6922,34 @@ def is_async_def(t: Type) -> bool: return isinstance(t, Instance) and t.type.fullname == "typing.Coroutine" +def _contains_unpack(t: Type) -> bool: + """Return True if t contains a variadic type (TypeVarTuple or Unpack).""" + p = get_proper_type(t) + if isinstance(p, (TypeVarTupleType, UnpackType)): + return True + if isinstance(p, TupleType): + return any(_contains_unpack(item) for item in p.items) + if isinstance(p, Instance): + return any(_contains_unpack(a) for a in p.args) + return False + + +def _is_variadic_item_iterable(t: Type) -> bool: + """Return True if t is a variable-length iterable with variadic items. + + This covers types like tuple[tuple[*Ts], ...] (the type of a *args + parameter annotated with a variadic tuple type), where both the number + of items and the arity of each item are unknown until type variable + binding. Such an argument cannot be expanded item-by-item against fixed + call formals, since each item would absorb a variable number of + positions. + """ + p = get_proper_type(t) + if isinstance(p, Instance) and p.args: + return _contains_unpack(p.args[0]) + return False + + def is_non_empty_tuple(t: Type) -> bool: t = get_proper_type(t) return isinstance(t, TupleType) and bool(t.items) diff --git a/test-data/unit/check-typevar-tuple.test b/test-data/unit/check-typevar-tuple.test index 0efaad63cf0d5..53ea9bf27b4a9 100644 --- a/test-data/unit/check-typevar-tuple.test +++ b/test-data/unit/check-typevar-tuple.test @@ -3324,3 +3324,24 @@ x: tuple[A, B] = pack(A(), B()) x = pack(C(), C()) reveal_type(x) # N: Revealed type is "tuple[__main__.C, __main__.C]" [builtins fixtures/tuple.pyi] + +[case testTypeVarTupleUnpackedStarArgIsCheckedIssue22567] +# flags: --python-version 3.11 +from typing import Generic, Tuple, Unpack +from typing_extensions import TypeVarTuple + +Ts = TypeVarTuple("Ts") + +class Example(Generic[*Ts]): + def func(self, composed: Tuple[Unpack[Ts]]) -> None: ... + +def mre(example: Example[Unpack[Ts]], *composed: Tuple[Unpack[Ts]]) -> None: + example.func(*composed) # E: Argument 1 to "func" of "Example" has incompatible type "*tuple[tuple[*Ts], ...]"; expected "tuple[*Ts]" + +def fixed_lenient(example: Example[int, str], composed: Tuple[Tuple[int, str], ...]) -> None: + # Homogeneous tuple[X, ...] keeps its traditional lenient item mapping. + example.func(*composed) + +def ok(example: Example[int, str], composed: Tuple[int, str]) -> None: + example.func(composed) +[builtins fixtures/tuple.pyi]