← Back to index

generics_typevartuple_specialization.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 2
FP: 16
FN: 4
Optional: 0 / 0
1"""
2Tests the handling of a TypeVarTuple specialization.
3"""
4
5# Specification: https://typing.readthedocs.io/en/latest/spec/generics.html#behaviour-when-type-parameters-are-not-specified
6
7from typing import Any, Generic, NewType, TypeVar, TypeVarTuple, assert_type
8
9
10Ts = TypeVarTuple("Ts")
11Height = NewType("Height", int)
12Width = NewType("Width", int)
13Time = NewType("Time", int)
16class Array(Generic[*Ts]):
17 ...
20def takes_any_array1(arr: Array):
21 ...
24def takes_any_array2(arr: Array[*tuple[Any, ...]]):
25 ...
28def func1(x: Array[Height, Width]):
29 takes_any_array1(x) # OK
30 takes_any_array2(x) # OK
33def func2(y: Array[Time, Height, Width]):
34 takes_any_array1(y) # OK
35 takes_any_array2(y) # OK
38# > Generic aliases can be created using a type variable tuple in a similar
39# > way to regular type variables.
41IntTuple = tuple[int, *Ts]
42NamedArray = tuple[str, Array[*Ts]]
45def func3(a: IntTuple[float, bool], b: NamedArray[Height]):
Unexpected error [not-subscriptable] Cannot subscript non-generic type: `<class 'tuple[str, @Todo]'>` is already specialized
46 assert_type(a, tuple[int, float, bool])
Unexpected error [type-assertion-failure] Type `tuple[int, int | float, bool]` does not match asserted type `tuple[@Todo(TypeVarTuple), ...]`
47 assert_type(b, tuple[str, Array[Height]])
Unexpected error [type-assertion-failure] Type `tuple[str, @Todo]` does not match asserted type `Unknown`
50def func4(a: IntTuple[()], b: NamedArray[()]):
51 assert_type(a, tuple[int])
Unexpected error [type-assertion-failure] Type `tuple[int]` does not match asserted type `tuple[@Todo(TypeVarTuple), ...]`
52 assert_type(b, tuple[str, Array[()]])
Unexpected error [invalid-type-form] Tuple literals are not allowed in this context in a type expression: Did you mean `tuple[()]`?
55Shape = TypeVarTuple("Shape")
56DType = TypeVar("DType")
59class Array2(Generic[DType, *Shape]):
60 ...
63FloatArray = Array2[float, *Shape]
64Array1D = Array2[DType, Any]
67def func5_0(a: Array1D, b: Array1D[int]):
68 assert_type(a, Array2[Any, Any])
69 assert_type(b, Array2[int, Any])
72def takes_float_array_of_any_shape(x: FloatArray):
73 ...
76def func5_1(x: FloatArray[Height, Width]):
77 takes_float_array_of_any_shape(x) # OK
80def takes_float_array_with_specific_shape(y: FloatArray[Height, Width]):
81 ...
84def func5_2(x: FloatArray):
85 takes_float_array_with_specific_shape(x) # OK
88T = TypeVar("T")
89VariadicTuple = tuple[T, *Ts]
92def func6(a: VariadicTuple[str, int], b: VariadicTuple[float], c: VariadicTuple):
93 assert_type(a, tuple[str, int])
Unexpected error [type-assertion-failure] Type `tuple[str, int]` does not match asserted type `tuple[@Todo(TypeVarTuple), ...]`
94 assert_type(b, tuple[float])
Unexpected error [type-assertion-failure] Type `tuple[int | float]` does not match asserted type `tuple[@Todo(TypeVarTuple), ...]`
95 assert_type(c, tuple[Any, *tuple[Any, ...]])
Unexpected error [type-assertion-failure] Type `tuple[Any, *tuple[Any, ...]]` does not match asserted type `tuple[@Todo(TypeVarTuple), ...]`
98Ts1 = TypeVarTuple("Ts1")
99Ts2 = TypeVarTuple("Ts2")
101IntTupleVar = tuple[int, *Ts1] # OK
102IntFloatTupleVar = IntTupleVar[float, *Ts2] # OK
103IntFloatsTupleVar = IntTupleVar[*tuple[float, ...]] # OK
106IntTupleGeneric = tuple[int, T]
108IntTupleGeneric[str] # OK
109IntTupleGeneric[*Ts] # E
Expected a ty diagnostic for this line
110IntTupleGeneric[*tuple[float, ...]] # E
Expected a ty diagnostic for this line
113T1 = TypeVar("T1")
114T2 = TypeVar("T2")
115T3 = TypeVar("T3")
117TA1 = tuple[*Ts, T1, T2] # OK
118TA2 = tuple[T1, T2, *Ts] # OK
119TA3 = tuple[T1, *Ts, T2, T3] # OK
120TA4 = tuple[T1, T2, *tuple[int, ...]] # OK
121TA5 = tuple[T1, *Ts, T2, *Ts] # E
[invalid-type-form] Multiple unpacked variadic tuples are not allowed in a `tuple` specialization
122TA6 = tuple[T1, *Ts, T2, *tuple[int, ...]] # E
[invalid-type-form] Multiple unpacked variadic tuples are not allowed in a `tuple` specialization
125TA7 = tuple[*Ts, T1, T2]
127v1: TA7[int] # E: requires at least two type arguments
Expected a ty diagnostic for this line
130def func7(a: TA7[*Ts, T1, T2]) -> tuple[tuple[*Ts], T1, T2]:
131 raise NotImplementedError
134def func8(a: TA7[str, bool], b: TA7[str, bool, float], c: TA7[str, bool, float, int]):
135 assert_type(func7(a), tuple[tuple[()], str, bool])
Unexpected error [type-assertion-failure] Type `tuple[tuple[()], str, bool]` does not match asserted type `tuple[tuple[@Todo(TypeVarTuple), ...], Unknown, Unknown]`
136 assert_type(func7(b), tuple[tuple[str], bool, float])
Unexpected error [type-assertion-failure] Type `tuple[tuple[str], bool, int | float]` does not match asserted type `tuple[tuple[@Todo(TypeVarTuple), ...], Unknown, Unknown]`
137 assert_type(func7(c), tuple[tuple[str, bool], float, int])
Unexpected error [type-assertion-failure] Type `tuple[tuple[str, bool], int | float, int]` does not match asserted type `tuple[tuple[@Todo(TypeVarTuple), ...], Unknown, Unknown]`
140TA8 = tuple[T1, *Ts, T2, T3]
143def func9(a: TA8[T1, *Ts, T2, T3]) -> tuple[tuple[*Ts], T1, T2, T3]:
144 raise NotImplementedError
147def func10(a: TA8[str, bool, float], b: TA8[str, bool, float, int]):
148 assert_type(func9(a), tuple[tuple[()], str, bool, float])
Unexpected error [type-assertion-failure] Type `tuple[tuple[()], str, bool, int | float]` does not match asserted type `tuple[tuple[@Todo(TypeVarTuple), ...], Unknown, Unknown, Unknown]`
149 assert_type(func9(b), tuple[tuple[bool], str, float, int])
Unexpected error [type-assertion-failure] Type `tuple[tuple[bool], str, int | float, int]` does not match asserted type `tuple[tuple[@Todo(TypeVarTuple), ...], Unknown, Unknown, Unknown]`
152TA9 = tuple[*Ts, T1]
153TA10 = TA9[*tuple[int, ...]] # OK
156def func11(a: TA10, b: TA9[*tuple[int, ...], str], c: TA9[*tuple[int, ...], str]):
157 assert_type(a, tuple[*tuple[int, ...], int])
Unexpected error [type-assertion-failure] Type `tuple[*tuple[int, ...], int]` does not match asserted type `tuple[@Todo(TypeVarTuple), ...]`
158 assert_type(b, tuple[*tuple[int, ...], str])
Unexpected error [type-assertion-failure] Type `tuple[*tuple[int, ...], str]` does not match asserted type `tuple[@Todo(TypeVarTuple), ...]`
159 assert_type(c, tuple[*tuple[int, ...], str])
Unexpected error [type-assertion-failure] Type `tuple[*tuple[int, ...], str]` does not match asserted type `tuple[@Todo(TypeVarTuple), ...]`
162TA11 = tuple[T, *Ts1]
163TA12 = TA11[*Ts2] # E
Expected a ty diagnostic for this line