2Tests basic usage of TypeVarTuple.
5# Specification: https://typing.readthedocs.io/en/latest/spec/generics.html#typevartuple
7from typing import Generic, NewType, TypeVarTuple, assert_type
12class Array1(Generic[*Ts]):
16def func1(*args: *Ts) -> tuple[*Ts]:
17 raise NotImplementedError
20Shape = TypeVarTuple("Shape")
23class Array(Generic[*Shape]):
24 def __init__(self, shape: tuple[*Shape]):
25 self._shape: tuple[*Shape] = shape
27 def get_shape(self) -> tuple[*Shape]:
31Height = NewType("Height", int)
32Width = NewType("Width", int)
33Time = NewType("Time", int)
34Batch = NewType("Batch", int)
36v1: Array[Height, Width] = Array((Height(1), Width(2))) # OK
37v2: Array[Batch, Height, Width] = Array((Batch(1), Height(1), Width(1))) # OK
38v3: Array[Time, Batch, Height, Width] = Array(
39 (Time(1), Batch(1), Height(1), Width(1))
42v4: Array[Height, Width] = Array(Height(1)) # E
[invalid-argument-type] Argument to bound method `__init__` is incorrect: Expected `tuple[@Todo(TypeVarTuple), ...]`, found `Height`
43v5: Array[Batch, Height, Width] = Array((Batch(1), Width(1))) # E
Expected a ty diagnostic for this line
44v6: Array[Time, Batch, Height, Width] = Array( # E[v6]
Expected a ty diagnostic for this line (tag 'v6')
45 (Time(1), Batch(1), Width(1), Height(1)) # E[v6]
Expected a ty diagnostic for this line (tag 'v6')
49# > Type Variable Tuples Must Always be Unpacked
52class ClassA(Generic[Shape]): # E: not unpacked
[invalid-generic-class] `TypeVarTuple` must be unpacked with `*` or `Unpack[]` when used as an argument to `Generic`
53 def __init__(self, shape: tuple[Shape]): # E: not unpacked
Expected a ty diagnostic for this line
54 self._shape: tuple[*Shape] = shape
56 def get_shape(self) -> tuple[Shape]: # E: not unpacked
Expected a ty diagnostic for this line
59 def method1(*args: Shape) -> None: # E: not unpacked
Expected a ty diagnostic for this line
63# > TypeVarTuple does not yet support specification of variance, bounds, constraints.
65Ts1 = TypeVarTuple("Ts1", covariant=True) # E
[unknown-argument] Argument `covariant` does not match any known parameter of function `__new__`
66Ts2 = TypeVarTuple("Ts2", int, float) # E
[too-many-positional-arguments] Too many positional arguments to function `__new__`: expected 2, got 4
67Ts3 = TypeVarTuple("Ts3", bound=int) # E
[unknown-argument] Argument `bound` does not match any known parameter of function `__new__`
70# > If the same TypeVarTuple instance is used in multiple places in a signature
71# > or class, a valid type inference might be to bind the TypeVarTuple to a
72# > tuple of a union of types.
75def func2(arg1: tuple[*Ts], arg2: tuple[*Ts]) -> tuple[*Ts]:
76 raise NotImplementedError
79# > We do not allow this; type unions may not appear within the tuple.
80# > If a type variable tuple appears in multiple places in a signature,
81# > the types must match exactly (the list of type parameters must be the
82# > same length, and the type parameters themselves must be identical)
84assert_type(func2((0,), (1,)), tuple[int]) # OK
Unexpected error
[type-assertion-failure] Type `tuple[int]` does not match asserted type `tuple[@Todo(TypeVarTuple), ...]`
85func2((0,), (0.0,)) # OK
86func2((0.0,), (0,)) # OK
89func2((0,), ("0",)) # E
Expected a ty diagnostic for this line
90func2((0, 0), (0,)) # E
Expected a ty diagnostic for this line
93def multiply(x: Array[*Shape], y: Array[*Shape]) -> Array[*Shape]:
94 raise NotImplementedError
97def func3(x: Array[Height], y: Array[Width], z: Array[Height, Width]):
Expected a ty diagnostic for this line
Expected a ty diagnostic for this line
103# > Only a single type variable tuple may appear in a type parameter list.
106class Array3(Generic[*Ts1, *Ts2]): # E
Expected a ty diagnostic for this line