Skip to content

Commit 39ad0d4

Browse files
committed
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 #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.
1 parent 0570905 commit 39ad0d4

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

‎mypy/checkexpr.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,6 +2953,30 @@ def check_argument_types(
29532953
actuals, actual_types, actual_kinds, callee_arg_types, callee_arg_kinds
29542954
):
29552955
# Check that a *arg is valid as varargs.
2956+
if (
2957+
actual_kind == ARG_STAR
2958+
and not callee_arg_kind.is_star()
2959+
and _is_variadic_item_iterable(actual_type)
2960+
):
2961+
# A starred argument with a variable-length iterable whose
2962+
# items are themselves variadic (e.g. tuple[tuple[*Ts], ...])
2963+
# cannot be safely expanded item-by-item against fixed
2964+
# formals: the item count is unknown and each item absorbs a
2965+
# variable number of positions, so no item-to-formal mapping
2966+
# can be trusted. Check the iterable as a whole instead.
2967+
check_arg(
2968+
actual_type,
2969+
actual_type,
2970+
actual_kind,
2971+
callee_arg_type,
2972+
actual + 1,
2973+
i + 1,
2974+
callee,
2975+
object_type,
2976+
args[actual],
2977+
context,
2978+
)
2979+
continue
29562980
expanded_actual = mapper.expand_actual_type(
29572981
actual_type,
29582982
actual_kind,
@@ -6898,6 +6922,34 @@ def is_async_def(t: Type) -> bool:
68986922
return isinstance(t, Instance) and t.type.fullname == "typing.Coroutine"
68996923

69006924

6925+
def _contains_unpack(t: Type) -> bool:
6926+
"""Return True if t contains a variadic type (TypeVarTuple or Unpack)."""
6927+
p = get_proper_type(t)
6928+
if isinstance(p, (TypeVarTupleType, UnpackType)):
6929+
return True
6930+
if isinstance(p, TupleType):
6931+
return any(_contains_unpack(item) for item in p.items)
6932+
if isinstance(p, Instance):
6933+
return any(_contains_unpack(a) for a in p.args)
6934+
return False
6935+
6936+
6937+
def _is_variadic_item_iterable(t: Type) -> bool:
6938+
"""Return True if t is a variable-length iterable with variadic items.
6939+
6940+
This covers types like tuple[tuple[*Ts], ...] (the type of a *args
6941+
parameter annotated with a variadic tuple type), where both the number
6942+
of items and the arity of each item are unknown until type variable
6943+
binding. Such an argument cannot be expanded item-by-item against fixed
6944+
call formals, since each item would absorb a variable number of
6945+
positions.
6946+
"""
6947+
p = get_proper_type(t)
6948+
if isinstance(p, Instance) and p.args:
6949+
return _contains_unpack(p.args[0])
6950+
return False
6951+
6952+
69016953
def is_non_empty_tuple(t: Type) -> bool:
69026954
t = get_proper_type(t)
69036955
return isinstance(t, TupleType) and bool(t.items)

‎test-data/unit/check-typevar-tuple.test‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3324,3 +3324,24 @@ x: tuple[A, B] = pack(A(), B())
33243324
x = pack(C(), C())
33253325
reveal_type(x) # N: Revealed type is "tuple[__main__.C, __main__.C]"
33263326
[builtins fixtures/tuple.pyi]
3327+
3328+
[case testTypeVarTupleUnpackedStarArgIsCheckedIssue22567]
3329+
# flags: --python-version 3.11
3330+
from typing import Generic, Tuple, Unpack
3331+
from typing_extensions import TypeVarTuple
3332+
3333+
Ts = TypeVarTuple("Ts")
3334+
3335+
class Example(Generic[*Ts]):
3336+
def func(self, composed: Tuple[Unpack[Ts]]) -> None: ...
3337+
3338+
def mre(example: Example[Unpack[Ts]], *composed: Tuple[Unpack[Ts]]) -> None:
3339+
example.func(*composed) # E: Argument 1 to "func" of "Example" has incompatible type "*tuple[tuple[*Ts], ...]"; expected "tuple[*Ts]"
3340+
3341+
def fixed_lenient(example: Example[int, str], composed: Tuple[Tuple[int, str], ...]) -> None:
3342+
# Homogeneous tuple[X, ...] keeps its traditional lenient item mapping.
3343+
example.func(*composed)
3344+
3345+
def ok(example: Example[int, str], composed: Tuple[int, str]) -> None:
3346+
example.func(composed)
3347+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)