mirror of
https://github.com/astral-sh/ruff.git
synced 2026-11-25 11:31:16 +08:00
## Summary Infer `TypeVarTuple` from arguments passed to `*args`, including unpacked tuples and iterables, fixed prefixes and suffixes, and nested type variable tuples. This PR contains only the core call-binding change. Callback forwarding is handled separately in #27371 to keep this PR focused and avoid adding more work to the old constraint solver. Moving `TypeVarTuple` and `ParamSpec` to the new solver is separate follow-up work. Continues #25240. ## Test plan - Pack inference for direct arguments, unpacked arguments, fixed boundaries, nested packs, and legacy `Unpack`. - Invalid starred arguments, bounded type variables, and diagnostic locations.
1.1 KiB
1.1 KiB
Starred expression annotations
[environment]
python-version = "3.11"
An unpacked type variable tuple keeps the types of positional arguments passed to *args.
from typing_extensions import TypeVarTuple
Ts = TypeVarTuple("Ts")
def append_int(*args: *Ts) -> tuple[*Ts, int]:
reveal_type(args) # revealed: tuple[*Ts@append_int]
return (*args, 1)
reveal_type(append_int(True, "a")) # revealed: tuple[Literal[True], Literal["a"], int]
reveal_type(append_int()) # revealed: tuple[int]
A concrete starred tuple checks its fixed first argument, remaining argument types, and arity.
def first_arg_int(*args: *tuple[int, *tuple[str, ...]]): ...
first_arg_int(42, "42", "42") # fine
# error: [invalid-argument-type] "Argument to function `first_arg_int` is incorrect: Expected `int`"
first_arg_int("not an int", "42", "42")
# error: [invalid-argument-type] "Argument to function `first_arg_int` is incorrect: Expected `str`, found `Literal[56]`"
first_arg_int(56, "42", 56)
# error: [missing-argument] "No argument provided for required parameter `*args` of function `first_arg_int`"
first_arg_int()