Description
Feature
If Ts
is a TypeVarTuple
, using it in a generic type Foo
will be equivalent to applying Foo
to every component of the tuple. I.e. Foo[Ts]
will be logically equivalent to (Foo[T1], Foo[T2], ...)
where T1
, T2
are the components of the tuple. Foo[Ts]
itself will be treated like a TypeVarTuple
, except that its components are not plain type variables, but an application of Foo
.
Pitch
This will help giving better types to zip
-like functions. Currently, these functions are typed using @overload
, for example:
class map(Iterator[_S]):
@overload
def __new__(cls, func: Callable[[_T1], _S], iter1: Iterable[_T1], /) -> Self: ...
@overload
def __new__(cls, func: Callable[[_T1, _T2], _S], iter1: Iterable[_T1], iter2: Iterable[_T2], /) -> Self: ...
@overload
def __new__(
cls, func: Callable[[_T1, _T2, _T3], _S], iter1: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], /
) -> Self: ...
...
class zip(Iterator[_T_co]):
@overload
def __new__(cls, iter1: Iterable[_T1], /, *, strict: bool = ...) -> zip[tuple[_T1]]: ...
@overload
def __new__(cls, iter1: Iterable[_T1], iter2: Iterable[_T2], /, *, strict: bool = ...) -> zip[tuple[_T1, _T2]]: ...
@overload
def __new__(
cls, iter1: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], /, *, strict: bool = ...
) -> zip[tuple[_T1, _T2, _T3]]: ...
...
This is very verbose and error-prone, and also introduces an arbitrary limit for the number of arguments. (typeshed stubs support up to 6 arguments for map
and zip
and up to 10 for itertools.product
.)
With the proposed feature these can be typed simply as:
class map(Iterator[_S]):
def __new__(cls, func: Callable[[*_Ts], _S], *iters: *Iterable[_Ts]) -> Self: ...
class zip(Iterator[_T_co]):
def __new__(cls, *iters: *Iterable[_Ts], *, strict: bool = ...) -> zip[tuple[*Ts]]: ...
Activity
erictraut commentedon Oct 1, 2024
A feature like this would likely require a PEP and an update to the Python typing spec. The Python typing forum would be a good place to discuss the idea.
A related idea was proposed here if you're interested in commenting (or collaborating) on that proposal.
eltoder commentedon Oct 1, 2024
@erictraut Thank you. Indeed this is a very similar proposal, and I agree with your comments in that thread.
jorenham commentedon Oct 28, 2024
See also python/typing#1216