Debian Codegraph @

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

raw
1 [case testTypeVarTupleBasic]
2 from typing import Any, Tuple
3 from typing_extensions import Unpack, TypeVarTuple
4
5 Ts = TypeVarTuple("Ts")
6
7 def f(a: Tuple[Unpack[Ts]]) -> Tuple[Unpack[Ts]]:
8 return a
9
10 any: Any
11 args: Tuple[int, str] = (1, 'x')
12 args2: Tuple[bool, str] = (False, 'y')
13 args3: Tuple[int, str, bool] = (2, 'z', True)
14 varargs: Tuple[int, ...] = (1, 2, 3)
15
16 reveal_type(f(args)) # N: Revealed type is "tuple[builtins.int, builtins.str]"
17
18 reveal_type(f(varargs)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
19
20 f(0) # E: Argument 1 to "f" has incompatible type "int"; expected "tuple[Never, ...]"
21
22 def g(a: Tuple[Unpack[Ts]], b: Tuple[Unpack[Ts]]) -> Tuple[Unpack[Ts]]:
23 return a
24
25 reveal_type(g(args, args)) # N: Revealed type is "tuple[builtins.int, builtins.str]"
26 reveal_type(g(args, args2)) # N: Revealed type is "tuple[builtins.int, builtins.str]"
27 reveal_type(g(args, args3)) # N: Revealed type is "builtins.tuple[builtins.int | builtins.str, ...]"
28 reveal_type(g(any, any)) # N: Revealed type is "builtins.tuple[Any, ...]"
29 [builtins fixtures/tuple.pyi]
30
31 [case testTypeVarTupleMixed]
32 from typing import Tuple
33 from typing_extensions import Unpack, TypeVarTuple
34
35 Ts = TypeVarTuple("Ts")
36
37 def to_str(i: int) -> str:
38 ...
39
40 def f(a: Tuple[int, Unpack[Ts]]) -> Tuple[str, Unpack[Ts]]:
41 return (to_str(a[0]),) + a[1:]
42
43 def g(a: Tuple[Unpack[Ts], int]) -> Tuple[Unpack[Ts], str]:
44 return a[:-1] + (to_str(a[-1]),)
45
46 def h(a: Tuple[bool, int, Unpack[Ts], str, object]) -> Tuple[Unpack[Ts]]:
47 return a[2:-2]
48
49 empty = ()
50 bad_args: Tuple[str, str]
51 var_len_tuple: Tuple[int, ...]
52
53 f_args: Tuple[int, str]
54 f_args2: Tuple[int]
55 f_args3: Tuple[int, str, bool]
56
57 reveal_type(f(f_args)) # N: Revealed type is "tuple[builtins.str, builtins.str]"
58 reveal_type(f(f_args2)) # N: Revealed type is "tuple[builtins.str]"
59 reveal_type(f(f_args3)) # N: Revealed type is "tuple[builtins.str, builtins.str, builtins.bool]"
60 f(empty) # E: Argument 1 to "f" has incompatible type "tuple[()]"; expected "tuple[int]"
61 f(bad_args) # E: Argument 1 to "f" has incompatible type "tuple[str, str]"; expected "tuple[int, str]"
62
63 # The reason for error in subtle: actual can be empty, formal cannot.
64 reveal_type(f(var_len_tuple)) # N: Revealed type is "tuple[builtins.str, Unpack[builtins.tuple[builtins.int, ...]]]" \
65 # E: Argument 1 to "f" has incompatible type "tuple[int, ...]"; expected "tuple[int, Unpack[tuple[int, ...]]]"
66
67 g_args: Tuple[str, int]
68 reveal_type(g(g_args)) # N: Revealed type is "tuple[builtins.str, builtins.str]"
69
70 h_args: Tuple[bool, int, str, int, str, object]
71 reveal_type(h(h_args)) # N: Revealed type is "tuple[builtins.str, builtins.int]"
72 [builtins fixtures/tuple.pyi]
73
74 [case testTypeVarTupleChaining]
75 from typing import Tuple
76 from typing_extensions import Unpack, TypeVarTuple
77
78 Ts = TypeVarTuple("Ts")
79
80 def to_str(i: int) -> str:
81 ...
82
83 def f(a: Tuple[int, Unpack[Ts]]) -> Tuple[str, Unpack[Ts]]:
84 return (to_str(a[0]),) + a[1:]
85
86 def g(a: Tuple[bool, int, Unpack[Ts], str, object]) -> Tuple[str, Unpack[Ts]]:
87 return f(a[1:-2])
88
89 def h(a: Tuple[bool, int, Unpack[Ts], str, object]) -> Tuple[str, Unpack[Ts]]:
90 x = f(a[1:-2])
91 return x
92
93 args: Tuple[bool, int, str, int, str, object]
94 reveal_type(g(args)) # N: Revealed type is "tuple[builtins.str, builtins.str, builtins.int]"
95 reveal_type(h(args)) # N: Revealed type is "tuple[builtins.str, builtins.str, builtins.int]"
96 [builtins fixtures/tuple.pyi]
97
98 [case testTypeVarTupleGenericClassDefn]
99 from typing import Generic, TypeVar, Tuple, Union
100 from typing_extensions import TypeVarTuple, Unpack
101
102 T = TypeVar("T")
103 Ts = TypeVarTuple("Ts")
104
105 class Variadic(Generic[Unpack[Ts]]):
106 pass
107
108 class Mixed1(Generic[T, Unpack[Ts]]):
109 pass
110
111 class Mixed2(Generic[Unpack[Ts], T]):
112 pass
113
114 variadic: Variadic[int, str]
115 reveal_type(variadic) # N: Revealed type is "__main__.Variadic[builtins.int, builtins.str]"
116
117 variadic_single: Variadic[int]
118 reveal_type(variadic_single) # N: Revealed type is "__main__.Variadic[builtins.int]"
119
120 empty: Variadic[()]
121 reveal_type(empty) # N: Revealed type is "__main__.Variadic[()]"
122
123 omitted: Variadic
124 reveal_type(omitted) # N: Revealed type is "__main__.Variadic[Unpack[builtins.tuple[Any, ...]]]"
125
126 bad: Variadic[Unpack[Tuple[int, ...]], str, Unpack[Tuple[bool, ...]]] # E: More than one variadic Unpack in a type is not allowed
127 reveal_type(bad) # N: Revealed type is "__main__.Variadic[Unpack[builtins.tuple[builtins.int, ...]], builtins.str]"
128
129 bad2: Unpack[Tuple[int, ...]] # E: Unpack is only valid in a variadic position
130
131 m1: Mixed1[int, str, bool]
132 reveal_type(m1) # N: Revealed type is "__main__.Mixed1[builtins.int, builtins.str, builtins.bool]"
133 [builtins fixtures/tuple.pyi]
134
135 [case testTypeVarTupleGenericClassWithFunctions]
136 from typing import Generic, Tuple, TypeVar
137 from typing_extensions import TypeVarTuple, Unpack
138
139 Ts = TypeVarTuple("Ts")
140 T = TypeVar("T")
141 S = TypeVar("S")
142
143 class Variadic(Generic[T, Unpack[Ts], S]):
144 pass
145
146 def foo(t: Variadic[int, Unpack[Ts], object]) -> Tuple[int, Unpack[Ts]]:
147 ...
148
149 v: Variadic[int, str, bool, object]
150 reveal_type(foo(v)) # N: Revealed type is "tuple[builtins.int, builtins.str, builtins.bool]"
151 [builtins fixtures/tuple.pyi]
152
153 [case testTypeVarTupleGenericClassWithMethods]
154 from typing import Generic, Tuple, TypeVar
155 from typing_extensions import TypeVarTuple, Unpack
156
157 Ts = TypeVarTuple("Ts")
158 T = TypeVar("T")
159 S = TypeVar("S")
160
161 class Variadic(Generic[T, Unpack[Ts], S]):
162 def __init__(self, t: Tuple[Unpack[Ts]]) -> None:
163 ...
164
165 def foo(self, t: int) -> Tuple[int, Unpack[Ts]]:
166 ...
167
168 v: Variadic[float, str, bool, object]
169 reveal_type(v.foo(0)) # N: Revealed type is "tuple[builtins.int, builtins.str, builtins.bool]"
170 [builtins fixtures/tuple.pyi]
171
172 [case testTypeVarTupleIsNotValidAliasTarget]
173 from typing_extensions import TypeVarTuple
174
175 Ts = TypeVarTuple("Ts")
176 B = Ts # E: Type variable "__main__.Ts" is invalid as target for type alias
177 [builtins fixtures/tuple.pyi]
178
179 [case testTypeVarTuplePep646ArrayExample]
180 from typing import Generic, Tuple, TypeVar, Protocol, NewType
181 from typing_extensions import TypeVarTuple, Unpack
182
183 Shape = TypeVarTuple('Shape')
184
185 Height = NewType('Height', int)
186 Width = NewType('Width', int)
187
188 T_co = TypeVar("T_co", covariant=True)
189 T = TypeVar("T")
190
191 class SupportsAbs(Protocol[T_co]):
192 def __abs__(self) -> T_co: pass
193
194 def abs(a: SupportsAbs[T]) -> T:
195 ...
196
197 class Array(Generic[Unpack[Shape]]):
198 def __init__(self, shape: Tuple[Unpack[Shape]]):
199 self._shape: Tuple[Unpack[Shape]] = shape
200
201 def get_shape(self) -> Tuple[Unpack[Shape]]:
202 return self._shape
203
204 def __abs__(self) -> Array[Unpack[Shape]]: ...
205
206 def __add__(self, other: Array[Unpack[Shape]]) -> Array[Unpack[Shape]]: ...
207
208 shape = (Height(480), Width(640))
209 x: Array[Height, Width] = Array(shape)
210 reveal_type(abs(x)) # N: Revealed type is "__main__.Array[__main__.Height, __main__.Width]"
211 reveal_type(x + x) # N: Revealed type is "__main__.Array[__main__.Height, __main__.Width]"
212 [builtins fixtures/tuple.pyi]
213
214 [case testTypeVarTuplePep646ArrayExampleWithDType]
215 from typing import Generic, Tuple, TypeVar, Protocol, NewType
216 from typing_extensions import TypeVarTuple, Unpack
217
218 DType = TypeVar("DType")
219 Shape = TypeVarTuple('Shape')
220
221 Height = NewType('Height', int)
222 Width = NewType('Width', int)
223
224 T_co = TypeVar("T_co", covariant=True)
225 T = TypeVar("T")
226
227 class SupportsAbs(Protocol[T_co]):
228 def __abs__(self) -> T_co: pass
229
230 def abs(a: SupportsAbs[T]) -> T:
231 ...
232
233 class Array(Generic[DType, Unpack[Shape]]):
234 def __init__(self, shape: Tuple[Unpack[Shape]]):
235 self._shape: Tuple[Unpack[Shape]] = shape
236
237 def get_shape(self) -> Tuple[Unpack[Shape]]:
238 return self._shape
239
240 def __abs__(self) -> Array[DType, Unpack[Shape]]: ...
241
242 def __add__(self, other: Array[DType, Unpack[Shape]]) -> Array[DType, Unpack[Shape]]: ...
243
244 shape = (Height(480), Width(640))
245 x: Array[float, Height, Width] = Array(shape)
246 reveal_type(abs(x)) # N: Revealed type is "__main__.Array[builtins.float, __main__.Height, __main__.Width]"
247 reveal_type(x + x) # N: Revealed type is "__main__.Array[builtins.float, __main__.Height, __main__.Width]"
248 [builtins fixtures/tuple.pyi]
249
250 [case testTypeVarTuplePep646ArrayExampleInfer]
251 from typing import Generic, Tuple, TypeVar, NewType
252 from typing_extensions import TypeVarTuple, Unpack
253
254 Shape = TypeVarTuple('Shape')
255
256 Height = NewType('Height', int)
257 Width = NewType('Width', int)
258
259 class Array(Generic[Unpack[Shape]]):
260 pass
261
262 x: Array[float, Height, Width] = Array()
263 [builtins fixtures/tuple.pyi]
264
265 [case testTypeVarTuplePep646TypeConcatenation]
266 from typing import Generic, TypeVar, NewType
267 from typing_extensions import TypeVarTuple, Unpack
268
269 Shape = TypeVarTuple('Shape')
270
271 Channels = NewType("Channels", int)
272 Batch = NewType("Batch", int)
273 Height = NewType('Height', int)
274 Width = NewType('Width', int)
275
276 class Array(Generic[Unpack[Shape]]):
277 pass
278
279
280 def add_batch_axis(x: Array[Unpack[Shape]]) -> Array[Batch, Unpack[Shape]]: ...
281 def del_batch_axis(x: Array[Batch, Unpack[Shape]]) -> Array[Unpack[Shape]]: ...
282 def add_batch_channels(
283 x: Array[Unpack[Shape]]
284 ) -> Array[Batch, Unpack[Shape], Channels]: ...
285
286 a: Array[Height, Width]
287 b = add_batch_axis(a)
288 reveal_type(b) # N: Revealed type is "__main__.Array[__main__.Batch, __main__.Height, __main__.Width]"
289 c = del_batch_axis(b)
290 reveal_type(c) # N: Revealed type is "__main__.Array[__main__.Height, __main__.Width]"
291 d = add_batch_channels(a)
292 reveal_type(d) # N: Revealed type is "__main__.Array[__main__.Batch, __main__.Height, __main__.Width, __main__.Channels]"
293 [builtins fixtures/tuple.pyi]
294
295 [case testTypeVarTuplePep646TypeVarConcatenation]
296 from typing import Generic, TypeVar, NewType, Tuple
297 from typing_extensions import TypeVarTuple, Unpack
298
299 T = TypeVar('T')
300 Ts = TypeVarTuple('Ts')
301
302 def prefix_tuple(
303 x: T,
304 y: Tuple[Unpack[Ts]],
305 ) -> Tuple[T, Unpack[Ts]]:
306 ...
307
308 z = prefix_tuple(x=0, y=(True, 'a'))
309 reveal_type(z) # N: Revealed type is "tuple[builtins.int, builtins.bool, builtins.str]"
310 [builtins fixtures/tuple.pyi]
311
312 [case testTypeVarTuplePep646TypeVarTupleUnpacking]
313 from typing import Generic, TypeVar, NewType, Any, Tuple
314 from typing_extensions import TypeVarTuple, Unpack
315
316 Shape = TypeVarTuple('Shape')
317
318 Channels = NewType("Channels", int)
319 Batch = NewType("Batch", int)
320 Height = NewType('Height', int)
321 Width = NewType('Width', int)
322
323 class Array(Generic[Unpack[Shape]]):
324 pass
325
326 def process_batch_channels(
327 x: Array[Batch, Unpack[Tuple[Any, ...]], Channels]
328 ) -> None:
329 ...
330
331 x: Array[Batch, Height, Width, Channels]
332 process_batch_channels(x)
333 y: Array[Batch, Channels]
334 process_batch_channels(y)
335 z: Array[Batch]
336 process_batch_channels(z) # E: Argument 1 to "process_batch_channels" has incompatible type "Array[Batch]"; expected "Array[Batch, Unpack[tuple[Any, ...]], Channels]"
337
338 u: Array[Unpack[Tuple[Any, ...]]]
339
340 def expect_variadic_array(
341 x: Array[Batch, Unpack[Shape]]
342 ) -> None:
343 ...
344
345 def expect_variadic_array_2(
346 x: Array[Batch, Height, Width, Channels]
347 ) -> None:
348 ...
349
350 expect_variadic_array(u)
351 expect_variadic_array_2(u)
352
353 Ts = TypeVarTuple("Ts")
354 Ts2 = TypeVarTuple("Ts2")
355
356 def bad(x: Tuple[int, Unpack[Ts], str, Unpack[Ts2]]) -> None: # E: More than one variadic Unpack in a type is not allowed
357
358 ...
359 reveal_type(bad) # N: Revealed type is "def [Ts, Ts2] (x: tuple[builtins.int, Unpack[Ts`-1], builtins.str])"
360
361 def bad2(x: Tuple[int, Unpack[Tuple[int, ...]], str, Unpack[Tuple[str, ...]]]) -> None: # E: More than one variadic Unpack in a type is not allowed
362 ...
363 reveal_type(bad2) # N: Revealed type is "def (x: tuple[builtins.int, Unpack[builtins.tuple[builtins.int, ...]], builtins.str])"
364 [builtins fixtures/tuple.pyi]
365
366 [case testTypeVarTuplePep646TypeVarStarArgsBasic]
367 from typing import Tuple
368 from typing_extensions import TypeVarTuple, Unpack
369
370 Ts = TypeVarTuple("Ts")
371
372 def args_to_tuple(*args: Unpack[Ts]) -> Tuple[Unpack[Ts]]:
373 reveal_type(args) # N: Revealed type is "tuple[Unpack[Ts`-1]]"
374 reveal_type(args_to_tuple(1, *args)) # N: Revealed type is "tuple[Literal[1]?, Unpack[Ts`-1]]"
375 reveal_type(args_to_tuple(*args, 'a')) # N: Revealed type is "tuple[Unpack[Ts`-1], Literal['a']?]"
376 reveal_type(args_to_tuple(1, *args, 'a')) # N: Revealed type is "tuple[Literal[1]?, Unpack[Ts`-1], Literal['a']?]"
377 args_to_tuple(*args, *args) # E: Passing multiple variadic unpacks in a call is not supported
378 ok = (1, 'a')
379 reveal_type(args_to_tuple(*ok, *ok)) # N: Revealed type is "tuple[builtins.int, builtins.str, builtins.int, builtins.str]"
380 if int():
381 return args
382 else:
383 return args_to_tuple(*args)
384
385 reveal_type(args_to_tuple(1, 'a')) # N: Revealed type is "tuple[Literal[1]?, Literal['a']?]"
386 vt: Tuple[int, ...]
387 reveal_type(args_to_tuple(1, *vt)) # N: Revealed type is "tuple[Literal[1]?, Unpack[builtins.tuple[builtins.int, ...]]]"
388 reveal_type(args_to_tuple(*vt, 'a')) # N: Revealed type is "tuple[Unpack[builtins.tuple[builtins.int, ...]], Literal['a']?]"
389 reveal_type(args_to_tuple(1, *vt, 'a')) # N: Revealed type is "tuple[Literal[1]?, Unpack[builtins.tuple[builtins.int, ...]], Literal['a']?]"
390 args_to_tuple(*vt, *vt) # E: Passing multiple variadic unpacks in a call is not supported
391 [builtins fixtures/tuple.pyi]
392
393 [case testTypeVarTuplePep646TypeVarStarArgs]
394 from typing import Tuple
395 from typing_extensions import TypeVarTuple, Unpack
396
397 Ts = TypeVarTuple("Ts")
398
399 def args_to_tuple(*args: Unpack[Ts]) -> Tuple[Unpack[Ts]]:
400 with_prefix_suffix(*args) # E: Too few arguments for "with_prefix_suffix" \
401 # E: Argument 1 to "with_prefix_suffix" has incompatible type "*tuple[Unpack[Ts]]"; expected "bool"
402 new_args = (True, "foo", *args, 5)
403 with_prefix_suffix(*new_args)
404 return args
405
406 def with_prefix_suffix(*args: Unpack[Tuple[bool, str, Unpack[Ts], int]]) -> Tuple[bool, str, Unpack[Ts], int]:
407 reveal_type(args) # N: Revealed type is "tuple[builtins.bool, builtins.str, Unpack[Ts`-1], builtins.int]"
408 reveal_type(args_to_tuple(*args)) # N: Revealed type is "tuple[builtins.bool, builtins.str, Unpack[Ts`-1], builtins.int]"
409 reveal_type(args_to_tuple(1, *args, 'a')) # N: Revealed type is "tuple[Literal[1]?, builtins.bool, builtins.str, Unpack[Ts`-1], builtins.int, Literal['a']?]"
410 return args
411
412 reveal_type(with_prefix_suffix(True, "bar", "foo", 5)) # N: Revealed type is "tuple[builtins.bool, builtins.str, Literal['foo']?, builtins.int]"
413 reveal_type(with_prefix_suffix(True, "bar", 5)) # N: Revealed type is "tuple[builtins.bool, builtins.str, builtins.int]"
414
415 with_prefix_suffix(True, "bar", "foo", 1.0) # E: Argument 4 to "with_prefix_suffix" has incompatible type "float"; expected "int"
416 with_prefix_suffix(True, "bar") # E: Too few arguments for "with_prefix_suffix"
417
418 t = (True, "bar", "foo", 5)
419 reveal_type(with_prefix_suffix(*t)) # N: Revealed type is "tuple[builtins.bool, builtins.str, builtins.str, builtins.int]"
420 reveal_type(with_prefix_suffix(True, *("bar", "foo"), 5)) # N: Revealed type is "tuple[builtins.bool, builtins.str, Literal['foo']?, builtins.int]"
421
422 reveal_type(with_prefix_suffix(True, "bar", *["foo1", "foo2"], 5)) # N: Revealed type is "tuple[builtins.bool, builtins.str, Unpack[builtins.tuple[builtins.str, ...]], builtins.int]"
423
424 bad_t = (True, "bar")
425 with_prefix_suffix(*bad_t) # E: Too few arguments for "with_prefix_suffix"
426
427 def foo(*args: Unpack[Ts]) -> None:
428 reveal_type(with_prefix_suffix(True, "bar", *args, 5)) # N: Revealed type is "tuple[builtins.bool, builtins.str, Unpack[Ts`-1], builtins.int]"
429 [builtins fixtures/tuple.pyi]
430
431 [case testTypeVarTuplePep646TypeVarStarArgsFixedLengthTuple]
432 from typing import Tuple
433 from typing_extensions import Unpack
434
435 def foo(*args: Unpack[Tuple[int, str]]) -> None:
436 reveal_type(args) # N: Revealed type is "tuple[builtins.int, builtins.str]"
437
438 foo(0, "foo")
439 foo(0, 1) # E: Argument 2 to "foo" has incompatible type "int"; expected "str"
440 foo("foo", "bar") # E: Argument 1 to "foo" has incompatible type "str"; expected "int"
441 foo(0, "foo", 1) # E: Too many arguments for "foo"
442 foo(0) # E: Too few arguments for "foo"
443 foo() # E: Too few arguments for "foo"
444 foo(*(0, "foo"))
445
446 def foo2(*args: Unpack[Tuple[bool, Unpack[Tuple[int, str]], bool]]) -> None:
447 reveal_type(args) # N: Revealed type is "tuple[builtins.bool, builtins.int, builtins.str, builtins.bool]"
448
449 # It is hard to normalize callable types in definition, because there is deep relation between `FuncDef.type`
450 # and `FuncDef.arguments`, therefore various typeops need to be sure to normalize Callable types before using them.
451 reveal_type(foo2) # N: Revealed type is "def (*args: Unpack[tuple[builtins.bool, builtins.int, builtins.str, builtins.bool]])"
452
453 class C:
454 def foo2(self, *args: Unpack[Tuple[bool, Unpack[Tuple[int, str]], bool]]) -> None: ...
455 reveal_type(C().foo2) # N: Revealed type is "def (*args: Unpack[tuple[builtins.bool, builtins.int, builtins.str, builtins.bool]])"
456 [builtins fixtures/tuple.pyi]
457
458 [case testTypeVarTuplePep646TypeVarStarArgsVariableLengthTuple]
459 from typing import Tuple
460 from typing_extensions import Unpack, TypeVarTuple
461
462 def foo(*args: Unpack[Tuple[int, ...]]) -> None:
463 reveal_type(args) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
464
465 foo(0, 1, 2)
466 foo(0, 1, "bar") # E: Argument 3 to "foo" has incompatible type "str"; expected "int"
467
468 def foo2(*args: Unpack[Tuple[str, Unpack[Tuple[int, ...]], bool, bool]]) -> None:
469 reveal_type(args) # N: Revealed type is "tuple[builtins.str, Unpack[builtins.tuple[builtins.int, ...]], builtins.bool, builtins.bool]"
470 reveal_type(args[1]) # N: Revealed type is "builtins.int"
471
472 def foo3(*args: Unpack[Tuple[str, Unpack[Tuple[int, ...]], str, float]]) -> None:
473 reveal_type(args[0]) # N: Revealed type is "builtins.str"
474 reveal_type(args[1]) # N: Revealed type is "builtins.int | builtins.str"
475 reveal_type(args[2]) # N: Revealed type is "builtins.int | builtins.str | builtins.float"
476 args[3] # E: Tuple index out of range \
477 # N: Variadic tuple can have length 3
478 reveal_type(args[-1]) # N: Revealed type is "builtins.float"
479 reveal_type(args[-2]) # N: Revealed type is "builtins.str"
480 reveal_type(args[-3]) # N: Revealed type is "builtins.str | builtins.int"
481 args[-4] # E: Tuple index out of range \
482 # N: Variadic tuple can have length 3
483 reveal_type(args[::-1]) # N: Revealed type is "tuple[builtins.float, builtins.str, Unpack[builtins.tuple[builtins.int, ...]], builtins.str]"
484 args[::2] # E: Ambiguous slice of a variadic tuple
485 args[:2] # E: Ambiguous slice of a variadic tuple
486
487 Ts = TypeVarTuple("Ts")
488 def foo4(*args: Unpack[Tuple[str, Unpack[Ts], bool, bool]]) -> None:
489 reveal_type(args[1]) # N: Revealed type is "builtins.object"
490
491 foo2("bar", 1, 2, 3, False, True)
492 foo2(0, 1, 2, 3, False, True) # E: Argument 1 to "foo2" has incompatible type "int"; expected "str"
493 foo2("bar", "bar", 2, 3, False, True) # E: Argument 2 to "foo2" has incompatible type "str"; expected "Unpack[tuple[Unpack[tuple[int, ...]], bool, bool]]"
494 foo2("bar", 1, 2, 3, 4, True) # E: Argument 5 to "foo2" has incompatible type "int"; expected "Unpack[tuple[Unpack[tuple[int, ...]], bool, bool]]"
495 foo2(*("bar", 1, 2, 3, False, True))
496 [builtins fixtures/tuple.pyi]
497
498 [case testTypeVarTuplePep646Callable]
499 from typing import Tuple, Callable
500 from typing_extensions import Unpack, TypeVarTuple
501
502 Ts = TypeVarTuple("Ts")
503
504 def call(
505 target: Callable[[Unpack[Ts]], None],
506 args: Tuple[Unpack[Ts]],
507 ) -> None:
508 pass
509
510 def func(arg1: int, arg2: str) -> None: ...
511 def func2(arg1: int, arg2: int) -> None: ...
512 def func3(*args: int) -> None: ...
513
514 vargs: Tuple[int, ...]
515 vargs_str: Tuple[str, ...]
516
517 call(target=func, args=(0, 'foo'))
518 call(target=func, args=('bar', 'foo')) # E: Argument "target" to "call" has incompatible type "Callable[[int, str], None]"; expected "Callable[[str, str], None]"
519 call(target=func, args=(True, 'foo', 0)) # E: Argument "target" to "call" has incompatible type "Callable[[int, str], None]"; expected "Callable[[bool, str, int], None]"
520 call(target=func, args=(0, 0, 'foo')) # E: Argument "target" to "call" has incompatible type "Callable[[int, str], None]"; expected "Callable[[int, int, str], None]"
521 call(target=func, args=vargs) # E: Argument "target" to "call" has incompatible type "Callable[[int, str], None]"; expected "def (*int) -> None"
522
523 # NOTE: This behavior may be a bit contentious, it is maybe inconsistent with our handling of
524 # PEP646 but consistent with our handling of callable constraints.
525 call(target=func2, args=vargs) # E: Argument "target" to "call" has incompatible type "Callable[[int, int], None]"; expected "def (*int) -> None"
526 call(target=func3, args=vargs)
527 call(target=func3, args=(0,1))
528 call(target=func3, args=(0,'foo')) # E: Argument "target" to "call" has incompatible type "def func3(*args: int) -> None"; expected "Callable[[int, str], None]"
529 call(target=func3, args=vargs_str) # E: Argument "target" to "call" has incompatible type "def func3(*args: int) -> None"; expected "def (*str) -> None"
530 [builtins fixtures/tuple.pyi]
531
532 [case testTypeVarTuplePep646CallableWithPrefixSuffix]
533 from typing import Tuple, Callable
534 from typing_extensions import Unpack, TypeVarTuple
535
536 Ts = TypeVarTuple("Ts")
537
538 def call_prefix(
539 target: Callable[[bytes, Unpack[Ts]], None],
540 args: Tuple[Unpack[Ts]],
541 ) -> None:
542 pass
543
544 def func_prefix(arg0: bytes, arg1: int, arg2: str) -> None: ...
545 def func2_prefix(arg0: str, arg1: int, arg2: str) -> None: ...
546
547 call_prefix(target=func_prefix, args=(0, 'foo'))
548 call_prefix(target=func2_prefix, args=(0, 'foo')) # E: Argument "target" to "call_prefix" has incompatible type "Callable[[str, int, str], None]"; expected "Callable[[bytes, int, str], None]"
549 [builtins fixtures/tuple.pyi]
550
551 [case testTypeVarTuplePep646CallableSuffixSyntax]
552 from typing import Callable, Tuple, TypeVar
553 from typing_extensions import Unpack, TypeVarTuple
554
555 x: Callable[[str, Unpack[Tuple[int, ...]], bool], None]
556 reveal_type(x) # N: Revealed type is "def (builtins.str, *Unpack[tuple[Unpack[builtins.tuple[builtins.int, ...]], builtins.bool]])"
557
558 T = TypeVar("T")
559 S = TypeVar("S")
560 Ts = TypeVarTuple("Ts")
561 A = Callable[[T, Unpack[Ts], S], int]
562 y: A[int, str, bool]
563 reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.str, builtins.bool) -> builtins.int"
564 z: A[Unpack[Tuple[int, ...]]]
565 reveal_type(z) # N: Revealed type is "def (builtins.int, *Unpack[tuple[Unpack[builtins.tuple[builtins.int, ...]], builtins.int]]) -> builtins.int"
566 [builtins fixtures/tuple.pyi]
567
568 [case testTypeVarTuplePep646CallableInvalidSyntax]
569 from typing import Callable, Tuple, TypeVar
570 from typing_extensions import Unpack, TypeVarTuple
571
572 Ts = TypeVarTuple("Ts")
573 Us = TypeVarTuple("Us")
574 a: Callable[[Unpack[Ts], Unpack[Us]], int] # E: More than one variadic Unpack in a type is not allowed
575 reveal_type(a) # N: Revealed type is "def [Ts, Us] (*Unpack[Ts`-1]) -> builtins.int"
576 b: Callable[[Unpack], int] # E: Unpack[...] requires exactly one type argument
577 reveal_type(b) # N: Revealed type is "def (*Any) -> builtins.int"
578 [builtins fixtures/tuple.pyi]
579
580 [case testTypeVarTuplePep646CallableNewSyntax]
581 # flags: --python-version 3.11
582 from typing import Callable, Generic, Tuple
583 from typing_extensions import ParamSpec
584
585 x: Callable[[str, *Tuple[int, ...]], None]
586 reveal_type(x) # N: Revealed type is "def (builtins.str, *builtins.int)"
587 y: Callable[[str, *Tuple[int, ...], bool], None]
588 reveal_type(y) # N: Revealed type is "def (builtins.str, *Unpack[tuple[Unpack[builtins.tuple[builtins.int, ...]], builtins.bool]])"
589
590 P = ParamSpec("P")
591 class C(Generic[P]): ...
592 bad: C[[int, *Tuple[int, ...], int]] # E: Unpack is only valid in a variadic position
593 reveal_type(bad) # N: Revealed type is "__main__.C[[builtins.int, *Any]]"
594 [builtins fixtures/tuple.pyi]
595
596 [case testTypeVarTuplePep646UnspecifiedParameters]
597 from typing import Tuple, Generic, TypeVar
598 from typing_extensions import Unpack, TypeVarTuple
599
600 Ts = TypeVarTuple("Ts")
601
602 class Array(Generic[Unpack[Ts]]):
603 ...
604
605 def takes_any_array(arr: Array) -> None:
606 ...
607
608 x: Array[int, bool]
609 takes_any_array(x)
610
611 T = TypeVar("T")
612
613 class Array2(Generic[T, Unpack[Ts]]):
614 ...
615
616 def takes_empty_array2(arr: Array2[int]) -> None:
617 ...
618
619 y: Array2[int]
620 takes_empty_array2(y)
621 [builtins fixtures/tuple.pyi]
622
623 [case testTypeVarTuplePep646CallableStarArgs]
624 from typing import Tuple, Callable
625 from typing_extensions import Unpack, TypeVarTuple
626
627 Ts = TypeVarTuple("Ts")
628
629 def call(
630 target: Callable[[Unpack[Ts]], None],
631 *args: Unpack[Ts],
632 ) -> None:
633 ...
634 target(*args)
635
636 class A:
637 def func(self, arg1: int, arg2: str) -> None: ...
638 def func2(self, arg1: int, arg2: int) -> None: ...
639 def func3(self, *args: int) -> None: ...
640
641 vargs: Tuple[int, ...]
642 vargs_str: Tuple[str, ...]
643
644 call(A().func) # E: Argument 1 to "call" has incompatible type "Callable[[int, str], None]"; expected "Callable[[], None]"
645 call(A().func, 0, 'foo')
646 call(A().func, 0, 'foo', 0) # E: Argument 1 to "call" has incompatible type "Callable[[int, str], None]"; expected "Callable[[int, str, int], None]"
647 call(A().func, 0) # E: Argument 1 to "call" has incompatible type "Callable[[int, str], None]"; expected "Callable[[int], None]"
648 call(A().func, 0, 1) # E: Argument 1 to "call" has incompatible type "Callable[[int, str], None]"; expected "Callable[[int, int], None]"
649 call(A().func2, 0, 0)
650 call(A().func3, 0, 1, 2)
651 call(A().func3)
652 [builtins fixtures/tuple.pyi]
653
654 [case testVariadicAliasBasicTuple]
655 from typing import Tuple, List, TypeVar
656 from typing_extensions import Unpack, TypeVarTuple
657
658 T = TypeVar("T")
659 Ts = TypeVarTuple("Ts")
660
661 A = List[Tuple[T, Unpack[Ts], T]]
662 x: A[int, str, str]
663 reveal_type(x) # N: Revealed type is "builtins.list[tuple[builtins.int, builtins.str, builtins.str, builtins.int]]"
664 [builtins fixtures/tuple.pyi]
665
666 [case testVariadicAliasBasicCallable]
667 from typing import TypeVar, Callable
668 from typing_extensions import Unpack, TypeVarTuple
669
670 T = TypeVar("T")
671 S = TypeVar("S")
672 Ts = TypeVarTuple("Ts")
673
674 A = Callable[[T, Unpack[Ts]], S]
675 x: A[int, str, int, str]
676 reveal_type(x) # N: Revealed type is "def (builtins.int, builtins.str, builtins.int) -> builtins.str"
677 [builtins fixtures/tuple.pyi]
678
679 [case testVariadicAliasBasicInstance]
680 from typing import TypeVar, Generic
681 from typing_extensions import Unpack, TypeVarTuple
682
683 T = TypeVar("T")
684 Ts = TypeVarTuple("Ts")
685
686 class G(Generic[Unpack[Ts], T]): ...
687
688 A = G[T, Unpack[Ts], T]
689 x: A[int, str, str]
690 reveal_type(x) # N: Revealed type is "__main__.G[builtins.int, builtins.str, builtins.str, builtins.int]"
691 [builtins fixtures/tuple.pyi]
692
693 [case testVariadicAliasUnpackFixedTupleArgs]
694 from typing import Tuple, List, TypeVar
695 from typing_extensions import Unpack, TypeVarTuple
696
697 T = TypeVar("T")
698 S = TypeVar("S")
699 Ts = TypeVarTuple("Ts")
700
701 Start = Tuple[int, str]
702 A = List[Tuple[T, Unpack[Ts], S]]
703 x: A[Unpack[Start], int]
704 reveal_type(x) # N: Revealed type is "builtins.list[tuple[builtins.int, builtins.str, builtins.int]]"
705 [builtins fixtures/tuple.pyi]
706
707 [case testVariadicAliasUnpackFixedTupleTarget]
708 from typing import Tuple, TypeVar
709 from typing_extensions import Unpack, TypeVarTuple
710
711 T = TypeVar("T")
712 S = TypeVar("S")
713 Ts = TypeVarTuple("Ts")
714
715 Prefix = Tuple[int, int]
716 A = Tuple[Unpack[Prefix], Unpack[Ts]]
717 x: A[str, str]
718 reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int, builtins.str, builtins.str]"
719 [builtins fixtures/tuple.pyi]
720
721 [case testVariadicAliasMultipleUnpacks]
722 from typing import Tuple, Generic, Callable
723 from typing_extensions import Unpack, TypeVarTuple
724
725 Ts = TypeVarTuple("Ts")
726 Us = TypeVarTuple("Us")
727 class G(Generic[Unpack[Ts]]): ...
728
729 A = Tuple[Unpack[Ts], Unpack[Us]] # E: More than one variadic Unpack in a type is not allowed
730 x: A[int, str]
731 reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.str]"
732
733 B = Callable[[Unpack[Ts], Unpack[Us]], int] # E: More than one variadic Unpack in a type is not allowed
734 y: B[int, str]
735 reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.str) -> builtins.int"
736
737 C = G[Unpack[Ts], Unpack[Us]] # E: More than one variadic Unpack in a type is not allowed
738 z: C[int, str]
739 reveal_type(z) # N: Revealed type is "__main__.G[builtins.int, builtins.str]"
740 [builtins fixtures/tuple.pyi]
741
742 [case testVariadicAliasNoArgs]
743 from typing import Tuple, TypeVar, Generic, Callable, List
744 from typing_extensions import Unpack, TypeVarTuple
745
746 T = TypeVar("T")
747 Ts = TypeVarTuple("Ts")
748 class G(Generic[Unpack[Ts]]): ...
749
750 A = List[Tuple[T, Unpack[Ts], T]]
751 x: A
752 reveal_type(x) # N: Revealed type is "builtins.list[tuple[Any, Unpack[builtins.tuple[Any, ...]], Any]]"
753
754 B = Callable[[T, Unpack[Ts]], int]
755 y: B
756 reveal_type(y) # N: Revealed type is "def (Any, *Any) -> builtins.int"
757
758 C = G[T, Unpack[Ts], T]
759 z: C
760 reveal_type(z) # N: Revealed type is "__main__.G[Any, Unpack[builtins.tuple[Any, ...]], Any]"
761 [builtins fixtures/tuple.pyi]
762
763 [case testVariadicAliasFewArgs]
764 from typing import Tuple, List, TypeVar, Generic, Callable
765 from typing_extensions import Unpack, TypeVarTuple
766
767 T = TypeVar("T")
768 S = TypeVar("S")
769 Ts = TypeVarTuple("Ts")
770 class G(Generic[Unpack[Ts]]): ...
771
772 A = List[Tuple[T, Unpack[Ts], S]]
773 x: A[int] # E: Bad number of arguments for type alias, expected at least 2, given 1
774 reveal_type(x) # N: Revealed type is "builtins.list[tuple[Any, Unpack[builtins.tuple[Any, ...]], Any]]"
775
776 B = Callable[[T, S, Unpack[Ts]], int]
777 y: B[int] # E: Bad number of arguments for type alias, expected at least 2, given 1
778 reveal_type(y) # N: Revealed type is "def (Any, Any, *Any) -> builtins.int"
779
780 C = G[T, Unpack[Ts], S]
781 z: C[int] # E: Bad number of arguments for type alias, expected at least 2, given 1
782 reveal_type(z) # N: Revealed type is "__main__.G[Any, Unpack[builtins.tuple[Any, ...]], Any]"
783 [builtins fixtures/tuple.pyi]
784
785 [case testVariadicAliasRecursiveUnpack]
786 from typing import Tuple, Optional
787 from typing_extensions import Unpack, TypeVarTuple
788
789 Ts = TypeVarTuple("Ts")
790
791 A = Tuple[Unpack[Ts], Optional[A[Unpack[Ts]]]]
792 x: A[int, str]
793 reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.str, ... | None]"
794
795 *_, last = x
796 if last is not None:
797 reveal_type(last) # N: Revealed type is "tuple[builtins.int, builtins.str, tuple[builtins.int, builtins.str, ... | None] | None]"
798 [builtins fixtures/tuple.pyi]
799
800 [case testVariadicAliasUpperBoundCheck]
801 from typing import Tuple, TypeVar
802 from typing_extensions import Unpack, TypeVarTuple
803
804 class A: ...
805 class B: ...
806 class C: ...
807 class D: ...
808
809 T = TypeVar("T", bound=int)
810 S = TypeVar("S", bound=str)
811 Ts = TypeVarTuple("Ts")
812
813 Alias = Tuple[T, Unpack[Ts], S]
814 First = Tuple[A, B]
815 Second = Tuple[C, D]
816 x: Alias[Unpack[First], Unpack[Second]] # E: Type argument "A" of "Alias" must be a subtype of "int" \
817 # E: Type argument "D" of "Alias" must be a subtype of "str"
818 [builtins fixtures/tuple.pyi]
819
820 [case testVariadicAliasEmptyArg]
821 from typing import Tuple
822 from typing_extensions import TypeVarTuple, Unpack
823
824 Ts = TypeVarTuple("Ts")
825 A = Tuple[int, Unpack[Ts], str]
826 x: A[()]
827 reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.str]"
828 [builtins fixtures/tuple.pyi]
829
830 [case testVariadicAliasVariadicTupleArg]
831 from typing import Tuple, TypeVar
832 from typing_extensions import Unpack, TypeVarTuple
833
834 Ts = TypeVarTuple("Ts")
835
836 A = Tuple[int, Unpack[Ts]]
837 B = A[str, Unpack[Ts]]
838 C = B[Unpack[Tuple[bool, ...]]]
839 x: C
840 reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.str, Unpack[builtins.tuple[builtins.bool, ...]]]"
841 [builtins fixtures/tuple.pyi]
842
843 [case testVariadicAliasVariadicTupleArgGeneric]
844 from typing import Tuple, TypeVar
845 from typing_extensions import Unpack, TypeVarTuple
846
847 T = TypeVar("T")
848 Ts = TypeVarTuple("Ts")
849
850 A = Tuple[int, Unpack[Ts]]
851 B = A[Unpack[Tuple[T, ...]]]
852 x: B[str]
853 reveal_type(x) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.str, ...]]]"
854 [builtins fixtures/tuple.pyi]
855
856 [case testVariadicAliasVariadicTupleArgSplit]
857 from typing import Tuple, TypeVar
858 from typing_extensions import Unpack, TypeVarTuple
859
860 T = TypeVar("T")
861 S = TypeVar("S")
862 Ts = TypeVarTuple("Ts")
863
864 A = Tuple[T, Unpack[Ts], S, T]
865
866 x: A[int, Unpack[Tuple[bool, ...]], str]
867 reveal_type(x) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.bool, ...]], builtins.str, builtins.int]"
868
869 y: A[Unpack[Tuple[bool, ...]]]
870 reveal_type(y) # N: Revealed type is "tuple[builtins.bool, Unpack[builtins.tuple[builtins.bool, ...]], builtins.bool, builtins.bool]"
871 [builtins fixtures/tuple.pyi]
872
873 [case testBanPathologicalRecursiveTuples]
874 from typing import Tuple
875 from typing_extensions import Unpack
876 A = Tuple[int, Unpack[A]] # E: Invalid recursive alias: a tuple item of itself
877 B = Tuple[int, Unpack[C]] # E: Invalid recursive alias: a tuple item of itself \
878 # E: Name "C" is used before definition
879 C = Tuple[int, Unpack[B]]
880 x: A
881 y: B
882 z: C
883 reveal_type(x) # N: Revealed type is "Any"
884 reveal_type(y) # N: Revealed type is "Any"
885 reveal_type(z) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[Any, ...]]]"
886 [builtins fixtures/tuple.pyi]
887
888 [case testBanPathologicalRecursiveTuplesGeneric]
889 from typing import TypeVarTuple, Unpack
890
891 Ts = TypeVarTuple("Ts")
892 A = tuple[Unpack[B[Unpack[Ts]]]] # E: Invalid recursive alias: a tuple item of itself \
893 # E: Name "B" is used before definition
894 B = tuple[Unpack[A[Unpack[Ts]]]]
895 [builtins fixtures/tuple.pyi]
896
897 [case testNoCrashOnInvalidRecursiveUnpackOfUnion]
898 from typing import Unpack
899
900 A = tuple[int, str] | list[tuple[Unpack[A]]] # E: "tuple[int, str] | list[tuple[Unpack[A]]]" cannot be unpacked (must be tuple or TypeVarTuple)
901 [builtins fixtures/tuple.pyi]
902
903 [case testNoCrashOnNonNormalRecursiveTuple]
904 from typing import Unpack
905
906 A = tuple[int, list[tuple[str, Unpack[A]]]]
907 a: A
908 x, y = a
909 y[0] = 1 # E: Incompatible types in assignment (expression has type "int", target has type "tuple[str, Unpack[A]]")
910 [builtins fixtures/list.pyi]
911
912 [case testBanTypeVarTupleNotImmediatelyInsideUnpack]
913 from typing import TypeVarTuple, Unpack
914
915 Ts = TypeVarTuple("Ts")
916 A = tuple[Unpack[tuple[Ts]]] # E: TypeVarTuple "Ts" is only valid with an unpack
917 x: A[int, str]
918 reveal_type(x) # N: Revealed type is "tuple[Any]"
919 [builtins fixtures/tuple.pyi]
920
921 [case testInferenceAgainstGenericVariadicWithBadType]
922 from typing import TypeVar, Callable, Generic
923 from typing_extensions import Unpack, TypeVarTuple
924
925 T = TypeVar("T")
926 Ts = TypeVarTuple("Ts")
927 Us = TypeVarTuple("Us")
928
929 class Foo(Generic[Unpack[Ts]]): ...
930
931 def dec(f: Callable[[Unpack[Ts]], T]) -> Callable[[Unpack[Ts]], T]: ...
932 def f(*args: Unpack[Us]) -> Foo[Us]: ... # E: TypeVarTuple "Us" is only valid with an unpack
933 dec(f) # No crash
934 [builtins fixtures/tuple.pyi]
935
936 [case testHomogeneousGenericTupleUnpackInferenceNoCrash1]
937 from typing import Any, TypeVar, Tuple, Type, Optional
938 from typing_extensions import Unpack
939
940 T = TypeVar("T")
941 def convert(obj: Any, *to_classes: Unpack[Tuple[Type[T], ...]]) -> Optional[T]:
942 ...
943
944 x = convert(1, int, float)
945 reveal_type(x) # N: Revealed type is "builtins.float | None"
946 [builtins fixtures/tuple.pyi]
947
948 [case testHomogeneousGenericTupleUnpackInferenceNoCrash2]
949 from typing import TypeVar, Tuple, Callable, Iterable
950 from typing_extensions import Unpack
951
952 T = TypeVar("T")
953 def combine(x: T, y: T) -> T: ...
954 def reduce(fn: Callable[[T, T], T], xs: Iterable[T]) -> T: ...
955
956 def pipeline(*xs: Unpack[Tuple[int, Unpack[Tuple[str, ...]], bool]]) -> None:
957 reduce(combine, xs)
958 [builtins fixtures/tuple.pyi]
959
960 [case testVariadicStarArgsCallNoCrash]
961 from typing import TypeVar, Callable, Tuple
962 from typing_extensions import TypeVarTuple, Unpack
963
964 X = TypeVar("X")
965 Y = TypeVar("Y")
966 Xs = TypeVarTuple("Xs")
967 Ys = TypeVarTuple("Ys")
968
969 def nil() -> Tuple[()]:
970 return ()
971
972 def cons(
973 f: Callable[[X], Y],
974 g: Callable[[Unpack[Xs]], Tuple[Unpack[Ys]]],
975 ) -> Callable[[X, Unpack[Xs]], Tuple[Y, Unpack[Ys]]]:
976 def wrapped(x: X, *xs: Unpack[Xs]) -> Tuple[Y, Unpack[Ys]]:
977 y, ys = f(x), g(*xs)
978 return y, *ys
979 return wrapped
980
981 def star(f: Callable[[X], Y]) -> Callable[[Unpack[Tuple[X, ...]]], Tuple[Y, ...]]:
982 def wrapped(*xs: X) -> Tuple[Y, ...]:
983 if not xs:
984 return nil()
985 return cons(f, star(f))(*xs)
986 return wrapped
987 [builtins fixtures/tuple.pyi]
988
989 [case testInvalidTypeVarTupleUseNoCrash]
990 from typing_extensions import TypeVarTuple
991
992 Ts = TypeVarTuple("Ts")
993
994 def f(x: Ts) -> Ts: # E: TypeVarTuple "Ts" is only valid with an unpack
995 return x
996
997 v = f(1, 2, "A") # E: Too many arguments for "f"
998 reveal_type(v) # N: Revealed type is "Any"
999 [builtins fixtures/tuple.pyi]
1000
1001 [case testTypeVarTupleSimpleDecoratorWorks]
1002 from typing import TypeVar, Callable
1003 from typing_extensions import TypeVarTuple, Unpack
1004
1005 Ts = TypeVarTuple("Ts")
1006 T = TypeVar("T")
1007
1008 def decorator(f: Callable[[Unpack[Ts]], T]) -> Callable[[Unpack[Ts]], T]:
1009 def wrapper(*args: Unpack[Ts]) -> T:
1010 return f(*args)
1011 return wrapper
1012
1013 @decorator
1014 def f(a: int, b: int) -> int: ...
1015 reveal_type(f) # N: Revealed type is "def (builtins.int, builtins.int) -> builtins.int"
1016 [builtins fixtures/tuple.pyi]
1017
1018 [case testTupleWithUnpackIterator]
1019 from typing import Tuple
1020 from typing_extensions import Unpack
1021
1022 def pipeline(*xs: Unpack[Tuple[int, Unpack[Tuple[float, ...]], bool]]) -> None:
1023 for x in xs:
1024 reveal_type(x) # N: Revealed type is "builtins.int | builtins.float"
1025 [builtins fixtures/tuple.pyi]
1026
1027 [case testFixedUnpackItemInInstanceArguments]
1028 from typing import TypeVar, Callable, Tuple, Generic
1029 from typing_extensions import TypeVarTuple, Unpack
1030
1031 T = TypeVar("T")
1032 S = TypeVar("S")
1033 Ts = TypeVarTuple("Ts")
1034
1035 class C(Generic[T, Unpack[Ts], S]):
1036 prefix: T
1037 suffix: S
1038 middle: Tuple[Unpack[Ts]]
1039
1040 Ints = Tuple[int, int]
1041 c: C[Unpack[Ints]]
1042 reveal_type(c.prefix) # N: Revealed type is "builtins.int"
1043 reveal_type(c.suffix) # N: Revealed type is "builtins.int"
1044 reveal_type(c.middle) # N: Revealed type is "tuple[()]"
1045 [builtins fixtures/tuple.pyi]
1046
1047 [case testVariadicUnpackItemInInstanceArguments]
1048 from typing import TypeVar, Callable, Tuple, Generic
1049 from typing_extensions import TypeVarTuple, Unpack
1050
1051 T = TypeVar("T")
1052 S = TypeVar("S")
1053 Ts = TypeVarTuple("Ts")
1054
1055 class Other(Generic[Unpack[Ts]]): ...
1056 class C(Generic[T, Unpack[Ts], S]):
1057 prefix: T
1058 suffix: S
1059 x: Tuple[Unpack[Ts]]
1060 y: Callable[[Unpack[Ts]], None]
1061 z: Other[Unpack[Ts]]
1062
1063 Ints = Tuple[int, ...]
1064 c: C[Unpack[Ints]]
1065 reveal_type(c.prefix) # N: Revealed type is "builtins.int"
1066 reveal_type(c.suffix) # N: Revealed type is "builtins.int"
1067 reveal_type(c.x) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1068 reveal_type(c.y) # N: Revealed type is "def (*builtins.int)"
1069 reveal_type(c.z) # N: Revealed type is "__main__.Other[Unpack[builtins.tuple[builtins.int, ...]]]"
1070 [builtins fixtures/tuple.pyi]
1071
1072 [case testTooFewItemsInInstanceArguments]
1073 from typing import Generic, TypeVar
1074 from typing_extensions import TypeVarTuple, Unpack
1075
1076 T = TypeVar("T")
1077 S = TypeVar("S")
1078 Ts = TypeVarTuple("Ts")
1079 class C(Generic[T, Unpack[Ts], S]): ...
1080
1081 c: C[int] # E: Bad number of arguments, expected: at least 2, given: 1
1082 reveal_type(c) # N: Revealed type is "__main__.C[Any, Unpack[builtins.tuple[Any, ...]], Any]"
1083 [builtins fixtures/tuple.pyi]
1084
1085 [case testVariadicClassUpperBoundCheck]
1086 from typing import Tuple, TypeVar, Generic
1087 from typing_extensions import Unpack, TypeVarTuple
1088
1089 class A: ...
1090 class B: ...
1091 class C: ...
1092 class D: ...
1093
1094 T = TypeVar("T", bound=int)
1095 S = TypeVar("S", bound=str)
1096 Ts = TypeVarTuple("Ts")
1097
1098 class G(Generic[T, Unpack[Ts], S]): ...
1099 First = Tuple[A, B]
1100 Second = Tuple[C, D]
1101 x: G[Unpack[First], Unpack[Second]] # E: Type argument "A" of "G" must be a subtype of "int" \
1102 # E: Type argument "D" of "G" must be a subtype of "str"
1103 [builtins fixtures/tuple.pyi]
1104
1105 [case testVariadicTupleType]
1106 from typing import Tuple, Callable
1107 from typing_extensions import TypeVarTuple, Unpack
1108
1109 Ts = TypeVarTuple("Ts")
1110 class A(Tuple[Unpack[Ts]]):
1111 fn: Callable[[Unpack[Ts]], None]
1112
1113 x: A[int]
1114 reveal_type(x) # N: Revealed type is "tuple[builtins.int, fallback=__main__.A[builtins.int]]"
1115 reveal_type(x[0]) # N: Revealed type is "builtins.int"
1116 reveal_type(x.fn) # N: Revealed type is "def (builtins.int)"
1117
1118 y: A[int, str]
1119 reveal_type(y) # N: Revealed type is "tuple[builtins.int, builtins.str, fallback=__main__.A[builtins.int, builtins.str]]"
1120 reveal_type(y[0]) # N: Revealed type is "builtins.int"
1121 reveal_type(y.fn) # N: Revealed type is "def (builtins.int, builtins.str)"
1122
1123 z: A[Unpack[Tuple[int, ...]]]
1124 reveal_type(z) # N: Revealed type is "__main__.A[Unpack[builtins.tuple[builtins.int, ...]]]"
1125 reveal_type(z[0]) # N: Revealed type is "builtins.int"
1126 reveal_type(z.fn) # N: Revealed type is "def (*builtins.int)"
1127
1128 t: A[int, Unpack[Tuple[int, str]], str]
1129 reveal_type(t) # N: Revealed type is "tuple[builtins.int, builtins.int, builtins.str, builtins.str, fallback=__main__.A[builtins.int, builtins.int, builtins.str, builtins.str]]"
1130 reveal_type(t[0]) # N: Revealed type is "builtins.int"
1131 reveal_type(t.fn) # N: Revealed type is "def (builtins.int, builtins.int, builtins.str, builtins.str)"
1132 [builtins fixtures/tuple.pyi]
1133
1134 [case testVariadicNamedTuple]
1135 from typing import Tuple, Callable, NamedTuple, Generic, TypeVar
1136 from typing_extensions import TypeVarTuple, Unpack
1137
1138 T = TypeVar("T")
1139 Ts = TypeVarTuple("Ts")
1140 class A(NamedTuple, Generic[Unpack[Ts], T]):
1141 fn: Callable[[Unpack[Ts]], None]
1142 val: T
1143
1144 y: A[int, str]
1145 reveal_type(y) # N: Revealed type is "tuple[def (builtins.int), builtins.str, fallback=__main__.A[builtins.int, builtins.str]]"
1146 reveal_type(y[0]) # N: Revealed type is "def (builtins.int)"
1147 reveal_type(y.fn) # N: Revealed type is "def (builtins.int)"
1148
1149 z: A[Unpack[Tuple[int, ...]]]
1150 reveal_type(z) # N: Revealed type is "tuple[def (*builtins.int), builtins.int, fallback=__main__.A[Unpack[builtins.tuple[builtins.int, ...]], builtins.int]]"
1151 reveal_type(z.fn) # N: Revealed type is "def (*builtins.int)"
1152
1153 t: A[int, Unpack[Tuple[int, str]], str]
1154 reveal_type(t) # N: Revealed type is "tuple[def (builtins.int, builtins.int, builtins.str), builtins.str, fallback=__main__.A[builtins.int, builtins.int, builtins.str, builtins.str]]"
1155
1156 def test(x: int, y: str) -> None: ...
1157 nt = A(fn=test, val=42)
1158 reveal_type(nt) # N: Revealed type is "tuple[def (builtins.int, builtins.str), builtins.int, fallback=__main__.A[builtins.int, builtins.str, builtins.int]]"
1159
1160 def bad() -> int: ...
1161 nt2 = A(fn=bad, val=42) # E: Argument "fn" to "A" has incompatible type "Callable[[], int]"; expected "Callable[[], None]"
1162 [builtins fixtures/tuple.pyi]
1163
1164 [case testVariadicTypedDict]
1165 from typing import Tuple, Callable, Generic, TypedDict, TypeVar
1166 from typing_extensions import TypeVarTuple, Unpack
1167
1168 T = TypeVar("T")
1169 Ts = TypeVarTuple("Ts")
1170 class A(TypedDict, Generic[Unpack[Ts], T]):
1171 fn: Callable[[Unpack[Ts]], None]
1172 val: T
1173
1174 y: A[int, str]
1175 reveal_type(y) # N: Revealed type is "TypedDict('__main__.A', {'fn': def (builtins.int), 'val': builtins.str})"
1176 reveal_type(y["fn"]) # N: Revealed type is "def (builtins.int)"
1177
1178 z: A[Unpack[Tuple[int, ...]]]
1179 reveal_type(z) # N: Revealed type is "TypedDict('__main__.A', {'fn': def (*builtins.int), 'val': builtins.int})"
1180 reveal_type(z["fn"]) # N: Revealed type is "def (*builtins.int)"
1181
1182 t: A[int, Unpack[Tuple[int, str]], str]
1183 reveal_type(t) # N: Revealed type is "TypedDict('__main__.A', {'fn': def (builtins.int, builtins.int, builtins.str), 'val': builtins.str})"
1184
1185 def test(x: int, y: str) -> None: ...
1186 td = A({"fn": test, "val": 42})
1187 reveal_type(td) # N: Revealed type is "TypedDict('__main__.A', {'fn': def (builtins.int, builtins.str), 'val': builtins.int})"
1188
1189 def bad() -> int: ...
1190 td2 = A({"fn": bad, "val": 42}) # E: Incompatible types (expression has type "Callable[[], int]", TypedDict item "fn" has type "Callable[[], None]")
1191 [builtins fixtures/dict.pyi]
1192 [typing fixtures/typing-typeddict.pyi]
1193
1194 [case testFixedUnpackWithRegularInstance]
1195 from typing import Tuple, Generic, TypeVar
1196 from typing_extensions import Unpack
1197
1198 T1 = TypeVar("T1")
1199 T2 = TypeVar("T2")
1200 T3 = TypeVar("T3")
1201 T4 = TypeVar("T4")
1202
1203 class C(Generic[T1, T2, T3, T4]): ...
1204 x: C[int, Unpack[Alias], str]
1205 Alias = Tuple[int, str]
1206 reveal_type(x) # N: Revealed type is "__main__.C[builtins.int, builtins.int, builtins.str, builtins.str]"
1207 [builtins fixtures/tuple.pyi]
1208
1209 [case testVariadicUnpackWithRegularInstance]
1210 from typing import Tuple, Generic, TypeVar
1211 from typing_extensions import Unpack
1212
1213 T1 = TypeVar("T1")
1214 T2 = TypeVar("T2")
1215 T3 = TypeVar("T3")
1216 T4 = TypeVar("T4")
1217
1218 class C(Generic[T1, T2, T3, T4]): ...
1219 x: C[int, Unpack[Alias], str, str] # E: Unpack is only valid in a variadic position
1220 Alias = Tuple[int, ...]
1221 reveal_type(x) # N: Revealed type is "__main__.C[Any, Any, Any, Any]"
1222 y: C[int, Unpack[Undefined]] # E: Name "Undefined" is not defined
1223 reveal_type(y) # N: Revealed type is "__main__.C[Any, Any, Any, Any]"
1224 [builtins fixtures/tuple.pyi]
1225
1226 [case testVariadicAliasInvalidUnpackNoCrash]
1227 from typing import Tuple, Generic, Union, List
1228 from typing_extensions import Unpack, TypeVarTuple
1229
1230 Ts = TypeVarTuple("Ts")
1231 Alias = Tuple[int, Unpack[Ts], str]
1232
1233 A = Union[int, str]
1234 x: List[Alias[int, Unpack[A], str]] # E: "int | str" cannot be unpacked (must be tuple or TypeVarTuple)
1235 reveal_type(x) # N: Revealed type is "builtins.list[tuple[builtins.int, builtins.int, Unpack[builtins.tuple[Any, ...]], builtins.str, builtins.str]]"
1236 y: List[Alias[int, Unpack[Undefined], str]] # E: Name "Undefined" is not defined
1237 reveal_type(y) # N: Revealed type is "builtins.list[tuple[builtins.int, Unpack[builtins.tuple[Any, ...]], builtins.str]]"
1238 [builtins fixtures/tuple.pyi]
1239
1240 [case testVariadicAliasForwardRefToFixedUnpack]
1241 from typing import Tuple, Generic, TypeVar
1242 from typing_extensions import Unpack, TypeVarTuple
1243
1244 T = TypeVar("T")
1245 S = TypeVar("S")
1246 Ts = TypeVarTuple("Ts")
1247 Alias = Tuple[T, Unpack[Ts], S]
1248 x: Alias[int, Unpack[Other]]
1249 Other = Tuple[int, str]
1250 reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int, builtins.str]"
1251 [builtins fixtures/tuple.pyi]
1252
1253 [case testVariadicAliasForwardRefToVariadicUnpack]
1254 from typing import Tuple, Generic, TypeVar
1255 from typing_extensions import Unpack, TypeVarTuple
1256
1257 T = TypeVar("T")
1258 S = TypeVar("S")
1259 Ts = TypeVarTuple("Ts")
1260 Alias = Tuple[T, Unpack[Ts], S]
1261 x: Alias[int, Unpack[Other]]
1262 Other = Tuple[int, ...]
1263 reveal_type(x) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.int, ...]], builtins.int]"
1264 [builtins fixtures/tuple.pyi]
1265
1266 [case testVariadicInstanceStrictPrefixSuffixCheck]
1267 from typing import Tuple, Generic, TypeVar
1268 from typing_extensions import Unpack, TypeVarTuple
1269
1270 T = TypeVar("T")
1271 S = TypeVar("S")
1272 Ts = TypeVarTuple("Ts")
1273 class C(Generic[T, Unpack[Ts], S]): ...
1274
1275 def foo(x: Tuple[Unpack[Ts]]) -> Tuple[Unpack[Ts]]:
1276 y: C[int, Unpack[Ts]] # E: TypeVarTuple cannot be split
1277 z: C[Unpack[Ts], int] # E: TypeVarTuple cannot be split
1278 return x
1279 [builtins fixtures/tuple.pyi]
1280
1281 [case testVariadicAliasStrictPrefixSuffixCheck]
1282 from typing import Tuple, TypeVar
1283 from typing_extensions import Unpack, TypeVarTuple
1284
1285 T = TypeVar("T")
1286 S = TypeVar("S")
1287 Ts = TypeVarTuple("Ts")
1288 Alias = Tuple[T, Unpack[Ts], S]
1289
1290 def foo(x: Tuple[Unpack[Ts]]) -> Tuple[Unpack[Ts]]:
1291 y: Alias[int, Unpack[Ts]] # E: TypeVarTuple cannot be split
1292 z: Alias[Unpack[Ts], int] # E: TypeVarTuple cannot be split
1293 return x
1294 [builtins fixtures/tuple.pyi]
1295
1296 [case testTypeVarTupleWithIsInstance]
1297 # flags: --warn-unreachable
1298 from typing import Generic, Tuple
1299 from typing_extensions import TypeVarTuple, Unpack
1300
1301 TP = TypeVarTuple("TP")
1302 class A(Tuple[Unpack[TP]]): ...
1303
1304 def test(d: A[int, str]) -> None:
1305 if isinstance(d, A):
1306 reveal_type(d) # N: Revealed type is "tuple[builtins.int, builtins.str, fallback=__main__.A[builtins.int, builtins.str]]"
1307 else:
1308 reveal_type(d) # E: Statement is unreachable
1309
1310 class B(Generic[Unpack[TP]]): ...
1311
1312 def test2(d: B[int, str]) -> None:
1313 if isinstance(d, B):
1314 reveal_type(d) # N: Revealed type is "__main__.B[builtins.int, builtins.str]"
1315 else:
1316 reveal_type(d) # E: Statement is unreachable
1317 [builtins fixtures/isinstancelist.pyi]
1318
1319 [case testVariadicTupleSubtyping]
1320 from typing import Tuple
1321 from typing_extensions import Unpack
1322
1323 def f1(x: Tuple[float, ...]) -> None: ...
1324 def f2(x: Tuple[float, Unpack[Tuple[float, ...]]]) -> None: ...
1325 def f3(x: Tuple[Unpack[Tuple[float, ...]], float]) -> None: ...
1326 def f4(x: Tuple[float, Unpack[Tuple[float, ...]], float]) -> None: ...
1327
1328 t1: Tuple[int, int]
1329 t2: Tuple[int, Unpack[Tuple[int, ...]]]
1330 t3: Tuple[Unpack[Tuple[int, ...]], int]
1331 t4: Tuple[int, Unpack[Tuple[int, ...]], int]
1332 t5: Tuple[int, ...]
1333
1334 tl: Tuple[int, int, Unpack[Tuple[int, ...]]]
1335 tr: Tuple[Unpack[Tuple[int, ...]], int, int]
1336
1337 f1(t1)
1338 f1(t2)
1339 f1(t3)
1340 f1(t4)
1341 f1(t5)
1342
1343 f1(tl)
1344 f1(tr)
1345
1346 f2(t1)
1347 f2(t2)
1348 f2(t3)
1349 f2(t4)
1350 f2(t5) # E: Argument 1 to "f2" has incompatible type "tuple[int, ...]"; expected "tuple[float, Unpack[tuple[float, ...]]]"
1351
1352 f2(tl)
1353 f2(tr)
1354
1355 f3(t1)
1356 f3(t2)
1357 f3(t3)
1358 f3(t4)
1359 f3(t5) # E: Argument 1 to "f3" has incompatible type "tuple[int, ...]"; expected "tuple[Unpack[tuple[float, ...]], float]"
1360
1361 f3(tl)
1362 f3(tr)
1363
1364 f4(t1)
1365 f4(t2) # E: Argument 1 to "f4" has incompatible type "tuple[int, Unpack[tuple[int, ...]]]"; expected "tuple[float, Unpack[tuple[float, ...]], float]"
1366 f4(t3) # E: Argument 1 to "f4" has incompatible type "tuple[Unpack[tuple[int, ...]], int]"; expected "tuple[float, Unpack[tuple[float, ...]], float]"
1367 f4(t4)
1368 f4(t5) # E: Argument 1 to "f4" has incompatible type "tuple[int, ...]"; expected "tuple[float, Unpack[tuple[float, ...]], float]"
1369
1370 f4(tl)
1371 f4(tr)
1372
1373 t5_verbose: Tuple[Unpack[Tuple[int, ...]]]
1374 t5 = t5_verbose # OK
1375 [builtins fixtures/tuple.pyi]
1376
1377 [case testVariadicTupleInference]
1378 from typing import List, Tuple, TypeVar
1379 from typing_extensions import TypeVarTuple, Unpack
1380
1381 T = TypeVar("T")
1382 def f(x: Tuple[int, Unpack[Tuple[T, ...]]]) -> T: ...
1383
1384 vt0: Tuple[int, ...]
1385 f(vt0) # E: Argument 1 to "f" has incompatible type "tuple[int, ...]"; expected "tuple[int, Unpack[tuple[int, ...]]]"
1386
1387 vt1: Tuple[Unpack[Tuple[int, ...]], int]
1388 reveal_type(f(vt1)) # N: Revealed type is "builtins.int"
1389
1390 S = TypeVar("S")
1391 Ts = TypeVarTuple("Ts")
1392 def g(x: Tuple[T, Unpack[Ts], S]) -> Tuple[T, Unpack[Ts], S]: ...
1393 g(vt0) # E: Argument 1 to "g" has incompatible type "tuple[int, ...]"; expected "tuple[int, Unpack[tuple[int, ...]], int]"
1394
1395 U = TypeVar("U")
1396 def h(x: List[Tuple[T, S, U]]) -> Tuple[T, S, U]: ...
1397 vt2: Tuple[Unpack[Tuple[int, ...]], int]
1398 vt2 = h(reveal_type([])) # N: Revealed type is "builtins.list[tuple[builtins.int, builtins.int, builtins.int]]"
1399 [builtins fixtures/tuple.pyi]
1400
1401 [case testVariadicSelfTypeErasure]
1402 from typing import Generic
1403 from typing_extensions import TypeVarTuple, Unpack
1404
1405 Ts = TypeVarTuple("Ts")
1406 class Array(Generic[Unpack[Ts]]):
1407 def _close(self) -> None: ...
1408
1409 def close(self) -> None:
1410 self._close()
1411 [builtins fixtures/tuple.pyi]
1412
1413 [case testVariadicSubclassFixed]
1414 from typing import Generic, Tuple
1415 from typing_extensions import TypeVarTuple, Unpack
1416
1417 Ts = TypeVarTuple("Ts")
1418 class B(Generic[Unpack[Ts]]): ...
1419 class C(B[int, str]): ...
1420 class D(B[Unpack[Tuple[int, ...]]]): ...
1421
1422 def fii(x: B[int, int]) -> None: ...
1423 def fis(x: B[int, str]) -> None: ...
1424 def fiv(x: B[Unpack[Tuple[int, ...]]]) -> None: ...
1425
1426 fii(C()) # E: Argument 1 to "fii" has incompatible type "C"; expected "B[int, int]"
1427 fii(D()) # E: Argument 1 to "fii" has incompatible type "D"; expected "B[int, int]"
1428 fis(C())
1429 fis(D()) # E: Argument 1 to "fis" has incompatible type "D"; expected "B[int, str]"
1430 fiv(C()) # E: Argument 1 to "fiv" has incompatible type "C"; expected "B[Unpack[tuple[int, ...]]]"
1431 fiv(D())
1432 [builtins fixtures/tuple.pyi]
1433
1434 [case testVariadicSubclassSame]
1435 from typing import Generic, Tuple, TypeVar
1436 from typing_extensions import TypeVarTuple, Unpack
1437
1438 Ts = TypeVarTuple("Ts")
1439 class B(Generic[Unpack[Ts]]): ...
1440 class C(B[Unpack[Ts]]): ...
1441
1442 def fii(x: B[int, int]) -> None: ...
1443 def fis(x: B[int, str]) -> None: ...
1444 def fiv(x: B[Unpack[Tuple[int, ...]]]) -> None: ...
1445
1446 cii: C[int, int]
1447 cis: C[int, str]
1448 civ: C[Unpack[Tuple[int, ...]]]
1449
1450 fii(cii)
1451 fii(cis) # E: Argument 1 to "fii" has incompatible type "C[int, str]"; expected "B[int, int]"
1452 fii(civ) # E: Argument 1 to "fii" has incompatible type "C[Unpack[tuple[int, ...]]]"; expected "B[int, int]"
1453
1454 fis(cii) # E: Argument 1 to "fis" has incompatible type "C[int, int]"; expected "B[int, str]"
1455 fis(cis)
1456 fis(civ) # E: Argument 1 to "fis" has incompatible type "C[Unpack[tuple[int, ...]]]"; expected "B[int, str]"
1457
1458 fiv(cii)
1459 fiv(cis) # E: Argument 1 to "fiv" has incompatible type "C[int, str]"; expected "B[Unpack[tuple[int, ...]]]"
1460 fiv(civ)
1461 [builtins fixtures/tuple.pyi]
1462
1463 [case testVariadicSubclassExtra]
1464 from typing import Generic, Tuple, TypeVar
1465 from typing_extensions import TypeVarTuple, Unpack
1466
1467 Ts = TypeVarTuple("Ts")
1468 class B(Generic[Unpack[Ts]]): ...
1469
1470 T = TypeVar("T")
1471 class C(B[int, Unpack[Ts], T]): ...
1472
1473 def ff(x: B[int, int, int]) -> None: ...
1474 def fv(x: B[Unpack[Tuple[int, ...]]]) -> None: ...
1475
1476 cii: C[int, int]
1477 cis: C[int, str]
1478 civ: C[Unpack[Tuple[int, ...]]]
1479
1480 ff(cii)
1481 ff(cis) # E: Argument 1 to "ff" has incompatible type "C[int, str]"; expected "B[int, int, int]"
1482 ff(civ) # E: Argument 1 to "ff" has incompatible type "C[Unpack[tuple[int, ...]]]"; expected "B[int, int, int]"
1483
1484 fv(cii)
1485 fv(cis) # E: Argument 1 to "fv" has incompatible type "C[int, str]"; expected "B[Unpack[tuple[int, ...]]]"
1486 fv(civ)
1487 [builtins fixtures/tuple.pyi]
1488
1489 [case testVariadicSubclassVariadic]
1490 from typing import Generic, Tuple, TypeVar
1491 from typing_extensions import TypeVarTuple, Unpack
1492
1493 Ts = TypeVarTuple("Ts")
1494 class B(Generic[Unpack[Ts]]): ...
1495 T = TypeVar("T")
1496 class C(B[Unpack[Tuple[T, ...]]]): ...
1497
1498 def ff(x: B[int, int]) -> None: ...
1499 def fv(x: B[Unpack[Tuple[int, ...]]]) -> None: ...
1500
1501 ci: C[int]
1502 ff(ci) # E: Argument 1 to "ff" has incompatible type "C[int]"; expected "B[int, int]"
1503 fv(ci)
1504 [builtins fixtures/tuple.pyi]
1505
1506 [case testVariadicSubclassMethodAccess]
1507 from typing import Generic, Tuple, TypeVar
1508 from typing_extensions import TypeVarTuple, Unpack
1509
1510 Ts = TypeVarTuple("Ts")
1511 class B(Generic[Unpack[Ts]]):
1512 def meth(self) -> Tuple[Unpack[Ts]]: ...
1513
1514 class C1(B[int, str]): ...
1515 class C2(B[Unpack[Ts]]): ...
1516 T = TypeVar("T")
1517 class C3(B[int, Unpack[Ts], T]): ...
1518 class C4(B[Unpack[Tuple[T, ...]]]): ...
1519
1520 c1: C1
1521 reveal_type(c1.meth()) # N: Revealed type is "tuple[builtins.int, builtins.str]"
1522
1523 c2f: C2[int, str]
1524 c2v: C2[Unpack[Tuple[int, ...]]]
1525 reveal_type(c2f.meth()) # N: Revealed type is "tuple[builtins.int, builtins.str]"
1526 reveal_type(c2v.meth()) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1527
1528 c3f: C3[int, str]
1529 c3v: C3[Unpack[Tuple[int, ...]]]
1530 reveal_type(c3f.meth()) # N: Revealed type is "tuple[builtins.int, builtins.int, builtins.str]"
1531 reveal_type(c3v.meth()) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.int, ...]], builtins.int]"
1532
1533 c4: C4[int]
1534 reveal_type(c4.meth()) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1535 [builtins fixtures/tuple.pyi]
1536
1537 [case testVariadicTupleAnySubtype]
1538 from typing import Any, Generic, Tuple
1539 from typing_extensions import TypeVarTuple, Unpack
1540
1541 Ts = TypeVarTuple("Ts")
1542 class B(Generic[Unpack[Ts]]): ...
1543 class C1(B[Unpack[Tuple[Any, ...]]]): ...
1544 c1 = C1()
1545 class C2(B): ...
1546 c2 = C2()
1547 x: B[int, str]
1548 x = c1
1549 x = c2
1550 [builtins fixtures/tuple.pyi]
1551
1552 [case testVariadicTupleAnySubtypeTupleType]
1553 from typing import Any, Generic, Tuple
1554 from typing_extensions import TypeVarTuple, Unpack
1555
1556 Ts = TypeVarTuple("Ts")
1557 class B(Tuple[Unpack[Ts]]): ...
1558 class C1(B[Unpack[Tuple[Any, ...]]]): ...
1559 c1 = C1()
1560 class C2(B): ...
1561 c2 = C2()
1562 x: B[int, str]
1563 x = c1
1564 x = c2
1565 [builtins fixtures/tuple.pyi]
1566
1567 [case testUnpackingVariadicTuplesTypeVar]
1568 from typing import Tuple
1569 from typing_extensions import TypeVarTuple, Unpack
1570
1571 Ts = TypeVarTuple("Ts")
1572 def foo(arg: Tuple[int, Unpack[Ts], str]) -> None:
1573 x1, y1, z1 = arg # E: Variadic tuple unpacking requires a star target
1574 reveal_type(x1) # N: Revealed type is "Any"
1575 reveal_type(y1) # N: Revealed type is "Any"
1576 reveal_type(z1) # N: Revealed type is "Any"
1577 x2, *y2, z2 = arg
1578 reveal_type(x2) # N: Revealed type is "builtins.int"
1579 reveal_type(y2) # N: Revealed type is "builtins.list[builtins.object]"
1580 reveal_type(z2) # N: Revealed type is "builtins.str"
1581 x3, *y3 = arg
1582 reveal_type(x3) # N: Revealed type is "builtins.int"
1583 reveal_type(y3) # N: Revealed type is "builtins.list[builtins.object]"
1584 *y4, z4 = arg
1585 reveal_type(y4) # N: Revealed type is "builtins.list[builtins.object]"
1586 reveal_type(z4) # N: Revealed type is "builtins.str"
1587 x5, xx5, *y5, z5, zz5 = arg # E: Too many assignment targets for variadic unpack
1588 reveal_type(x5) # N: Revealed type is "Any"
1589 reveal_type(xx5) # N: Revealed type is "Any"
1590 reveal_type(y5) # N: Revealed type is "builtins.list[Any]"
1591 reveal_type(z5) # N: Revealed type is "Any"
1592 reveal_type(zz5) # N: Revealed type is "Any"
1593 [builtins fixtures/tuple.pyi]
1594
1595 [case testUnpackingVariadicTuplesHomogeneous]
1596 from typing import Tuple
1597 from typing_extensions import Unpack
1598
1599 def bar(arg: Tuple[int, Unpack[Tuple[float, ...]], str]) -> None:
1600 x1, y1, z1 = arg # E: Variadic tuple unpacking requires a star target
1601 reveal_type(x1) # N: Revealed type is "Any"
1602 reveal_type(y1) # N: Revealed type is "Any"
1603 reveal_type(z1) # N: Revealed type is "Any"
1604 x2, *y2, z2 = arg
1605 reveal_type(x2) # N: Revealed type is "builtins.int"
1606 reveal_type(y2) # N: Revealed type is "builtins.list[builtins.float]"
1607 reveal_type(z2) # N: Revealed type is "builtins.str"
1608 x3, *y3 = arg
1609 reveal_type(x3) # N: Revealed type is "builtins.int"
1610 reveal_type(y3) # N: Revealed type is "builtins.list[builtins.object]"
1611 *y4, z4 = arg
1612 reveal_type(y4) # N: Revealed type is "builtins.list[builtins.float]"
1613 reveal_type(z4) # N: Revealed type is "builtins.str"
1614 x5, xx5, *y5, z5, zz5 = arg # E: Too many assignment targets for variadic unpack
1615 reveal_type(x5) # N: Revealed type is "Any"
1616 reveal_type(xx5) # N: Revealed type is "Any"
1617 reveal_type(y5) # N: Revealed type is "builtins.list[Any]"
1618 reveal_type(z5) # N: Revealed type is "Any"
1619 reveal_type(zz5) # N: Revealed type is "Any"
1620 [builtins fixtures/tuple.pyi]
1621
1622 [case testRepackingVariadicTuplesTypeVar]
1623 from typing import Tuple
1624 from typing_extensions import TypeVarTuple, Unpack
1625
1626 Ts = TypeVarTuple("Ts")
1627 def foo(arg: Tuple[int, Unpack[Ts], str]) -> None:
1628 x1, *y1, z1 = *arg,
1629 reveal_type(x1) # N: Revealed type is "builtins.int"
1630 reveal_type(y1) # N: Revealed type is "builtins.list[builtins.object]"
1631 reveal_type(z1) # N: Revealed type is "builtins.str"
1632 x2, *y2, z2 = 1, *arg, 2
1633 reveal_type(x2) # N: Revealed type is "builtins.int"
1634 reveal_type(y2) # N: Revealed type is "builtins.list[builtins.object]"
1635 reveal_type(z2) # N: Revealed type is "builtins.int"
1636 x3, *y3 = *arg, 42
1637 reveal_type(x3) # N: Revealed type is "builtins.int"
1638 reveal_type(y3) # N: Revealed type is "builtins.list[builtins.object]"
1639 *y4, z4 = 42, *arg
1640 reveal_type(y4) # N: Revealed type is "builtins.list[builtins.object]"
1641 reveal_type(z4) # N: Revealed type is "builtins.str"
1642 x5, xx5, *y5, z5, zz5 = 1, *arg, 2
1643 reveal_type(x5) # N: Revealed type is "builtins.int"
1644 reveal_type(xx5) # N: Revealed type is "builtins.int"
1645 reveal_type(y5) # N: Revealed type is "builtins.list[builtins.object]"
1646 reveal_type(z5) # N: Revealed type is "builtins.str"
1647 reveal_type(zz5) # N: Revealed type is "builtins.int"
1648 [builtins fixtures/tuple.pyi]
1649
1650 [case testRepackingVariadicTuplesHomogeneous]
1651 from typing import Tuple
1652 from typing_extensions import Unpack
1653
1654 def foo(arg: Tuple[int, Unpack[Tuple[float, ...]], str]) -> None:
1655 x1, *y1, z1 = *arg,
1656 reveal_type(x1) # N: Revealed type is "builtins.int"
1657 reveal_type(y1) # N: Revealed type is "builtins.list[builtins.float]"
1658 reveal_type(z1) # N: Revealed type is "builtins.str"
1659 x2, *y2, z2 = 1, *arg, 2
1660 reveal_type(x2) # N: Revealed type is "builtins.int"
1661 reveal_type(y2) # N: Revealed type is "builtins.list[builtins.object]"
1662 reveal_type(z2) # N: Revealed type is "builtins.int"
1663 x3, *y3 = *arg, 42
1664 reveal_type(x3) # N: Revealed type is "builtins.int"
1665 reveal_type(y3) # N: Revealed type is "builtins.list[builtins.object]"
1666 *y4, z4 = 42, *arg
1667 reveal_type(y4) # N: Revealed type is "builtins.list[builtins.float]"
1668 reveal_type(z4) # N: Revealed type is "builtins.str"
1669 x5, xx5, *y5, z5, zz5 = 1, *arg, 2
1670 reveal_type(x5) # N: Revealed type is "builtins.int"
1671 reveal_type(xx5) # N: Revealed type is "builtins.int"
1672 reveal_type(y5) # N: Revealed type is "builtins.list[builtins.float]"
1673 reveal_type(z5) # N: Revealed type is "builtins.str"
1674 reveal_type(zz5) # N: Revealed type is "builtins.int"
1675 [builtins fixtures/tuple.pyi]
1676
1677 [case testPackingVariadicTuplesTypeVar]
1678 from typing import Tuple
1679 from typing_extensions import TypeVarTuple, Unpack
1680
1681 Ts = TypeVarTuple("Ts")
1682 def foo(arg: Tuple[int, Unpack[Ts], str]) -> None:
1683 x = *arg,
1684 reveal_type(x) # N: Revealed type is "tuple[builtins.int, Unpack[Ts`-1], builtins.str]"
1685 y = 1, *arg, 2
1686 reveal_type(y) # N: Revealed type is "tuple[builtins.int, builtins.int, Unpack[Ts`-1], builtins.str, builtins.int]"
1687 z = (*arg, *arg)
1688 reveal_type(z) # N: Revealed type is "builtins.tuple[builtins.object, ...]"
1689 [builtins fixtures/tuple.pyi]
1690
1691 [case testPackingVariadicTuplesHomogeneous]
1692 # flags: --enable-incomplete-feature=PreciseTupleTypes
1693 from typing import Tuple
1694 from typing_extensions import Unpack
1695
1696 a: Tuple[float, ...]
1697 b: Tuple[int, Unpack[Tuple[float, ...]], str]
1698
1699 x = *a,
1700 reveal_type(x) # N: Revealed type is "builtins.tuple[builtins.float, ...]"
1701 y = 1, *a, 2
1702 reveal_type(y) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.float, ...]], builtins.int]"
1703 z = (*a, *a)
1704 reveal_type(z) # N: Revealed type is "builtins.tuple[builtins.float, ...]"
1705
1706 x2 = *b,
1707 reveal_type(x2) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.float, ...]], builtins.str]"
1708 y2 = 1, *b, 2
1709 reveal_type(y2) # N: Revealed type is "tuple[builtins.int, builtins.int, Unpack[builtins.tuple[builtins.float, ...]], builtins.str, builtins.int]"
1710 z2 = (*b, *b)
1711 reveal_type(z2) # N: Revealed type is "builtins.tuple[builtins.object, ...]"
1712 [builtins fixtures/tuple.pyi]
1713
1714 [case testVariadicTupleInListSetExpr]
1715 from typing import Tuple
1716 from typing_extensions import TypeVarTuple, Unpack
1717
1718 vt: Tuple[int, Unpack[Tuple[float, ...]], int]
1719 reveal_type([1, *vt]) # N: Revealed type is "builtins.list[builtins.float]"
1720 reveal_type({1, *vt}) # N: Revealed type is "builtins.set[builtins.float]"
1721
1722 Ts = TypeVarTuple("Ts")
1723 def foo(arg: Tuple[int, Unpack[Ts], str]) -> None:
1724 reveal_type([1, *arg]) # N: Revealed type is "builtins.list[builtins.object]"
1725 reveal_type({1, *arg}) # N: Revealed type is "builtins.set[builtins.object]"
1726 [builtins fixtures/isinstancelist.pyi]
1727
1728 [case testVariadicTupleInTupleContext]
1729 # flags: --enable-incomplete-feature=PreciseTupleTypes
1730 from typing import Tuple, Optional
1731 from typing_extensions import TypeVarTuple, Unpack
1732
1733 Ts = TypeVarTuple("Ts")
1734 def test(x: Optional[Tuple[Unpack[Ts]]] = None) -> Tuple[Unpack[Ts]]: ...
1735
1736 vt: Tuple[int, Unpack[Tuple[float, ...]], int]
1737 vt = 1, *test(), 2 # OK, type context is used
1738 vt2 = 1, *test(), 2 # E: Need type annotation for "vt2"
1739 [builtins fixtures/tuple.pyi]
1740
1741 [case testVariadicTupleConcatenation]
1742 # flags: --enable-incomplete-feature=PreciseTupleTypes
1743 from typing import Tuple
1744 from typing_extensions import TypeVarTuple, Unpack
1745
1746 vtf: Tuple[float, ...]
1747 vt: Tuple[int, Unpack[Tuple[float, ...]], int]
1748
1749 reveal_type(vt + (1, 2)) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.float, ...]], builtins.int, Literal[1]?, Literal[2]?]"
1750 reveal_type((1, 2) + vt) # N: Revealed type is "tuple[Literal[1]?, Literal[2]?, builtins.int, Unpack[builtins.tuple[builtins.float, ...]], builtins.int]"
1751 reveal_type(vt + vt) # N: Revealed type is "builtins.tuple[builtins.int | builtins.float, ...]"
1752 reveal_type(vtf + (1, 2)) # N: Revealed type is "tuple[Unpack[builtins.tuple[builtins.float, ...]], Literal[1]?, Literal[2]?]"
1753 reveal_type((1, 2) + vtf) # N: Revealed type is "tuple[Literal[1]?, Literal[2]?, Unpack[builtins.tuple[builtins.float, ...]]]"
1754
1755 Ts = TypeVarTuple("Ts")
1756 def foo(arg: Tuple[int, Unpack[Ts], str]) -> None:
1757 reveal_type(arg + (1, 2)) # N: Revealed type is "tuple[builtins.int, Unpack[Ts`-1], builtins.str, Literal[1]?, Literal[2]?]"
1758 reveal_type((1, 2) + arg) # N: Revealed type is "tuple[Literal[1]?, Literal[2]?, builtins.int, Unpack[Ts`-1], builtins.str]"
1759 reveal_type(arg + arg) # N: Revealed type is "builtins.tuple[builtins.object, ...]"
1760 [builtins fixtures/tuple.pyi]
1761
1762 [case testTypeVarTupleAnyOverload]
1763 from typing import Any, Generic, overload, Tuple
1764 from typing_extensions import TypeVarTuple, Unpack
1765
1766 Ts = TypeVarTuple("Ts")
1767 class Array(Generic[Unpack[Ts]]): ...
1768
1769 class A:
1770 @overload
1771 def f(self, x: Tuple[Unpack[Ts]]) -> Array[Unpack[Ts]]: ...
1772 @overload
1773 def f(self, x: Any) -> Any: ...
1774 def f(self, x: Any) -> Any:
1775 ...
1776 [builtins fixtures/tuple.pyi]
1777
1778 [case testTypeVarTupleInferAgainstAny]
1779 from typing import Any, Tuple, TypeVar
1780 from typing_extensions import Unpack
1781
1782 T = TypeVar("T")
1783
1784 def test(x: int, t: Tuple[T, ...]) -> Tuple[int, Unpack[Tuple[T, ...]]]:
1785 ...
1786 a: Any = test(42, ())
1787 [builtins fixtures/tuple.pyi]
1788
1789 [case testTypeVarTupleIndexTypeVar]
1790 from typing import Any, List, Sequence, Tuple, TypeVar
1791 from typing_extensions import TypeVarTuple, Unpack
1792
1793 Ts = TypeVarTuple("Ts")
1794 def f(data: Sequence[Tuple[Unpack[Ts]]]) -> List[Any]:
1795 return [d[0] for d in data] # E: Tuple index out of range \
1796 # N: Variadic tuple can have length 0
1797
1798 T = TypeVar("T")
1799 def g(data: Sequence[Tuple[T, Unpack[Ts]]]) -> List[T]:
1800 return [d[0] for d in data] # OK
1801 [builtins fixtures/tuple.pyi]
1802
1803 [case testTypeVarTupleOverloadMatch]
1804 from typing import Any, Generic, overload, Tuple, TypeVar
1805 from typing_extensions import TypeVarTuple, Unpack
1806
1807 _Ts = TypeVarTuple("_Ts")
1808 _T = TypeVar("_T")
1809 _T2 = TypeVar("_T2")
1810
1811 class Container(Generic[_T]): ...
1812 class Array(Generic[Unpack[_Ts]]): ...
1813
1814 @overload
1815 def build(entity: Container[_T], /) -> Array[_T]: ...
1816 @overload
1817 def build(entity: Container[_T], entity2: Container[_T2], /) -> Array[_T, _T2]: ...
1818 @overload
1819 def build(*entities: Container[Any]) -> Array[Unpack[Tuple[Any, ...]]]: ...
1820 def build(*entities: Container[Any]) -> Array[Unpack[Tuple[Any, ...]]]:
1821 ...
1822
1823 def test(a: Container[Any], b: Container[int], c: Container[str]):
1824 reveal_type(build(a, b)) # N: Revealed type is "__main__.Array[Any, builtins.int]"
1825 reveal_type(build(b, c)) # N: Revealed type is "__main__.Array[builtins.int, builtins.str]"
1826 [builtins fixtures/tuple.pyi]
1827
1828 [case testTypeVarTupleOverloadArbitraryLength]
1829 from typing import Any, Tuple, TypeVar, TypeVarTuple, Unpack, overload
1830
1831 T = TypeVar("T")
1832 Ts = TypeVarTuple("Ts")
1833 @overload
1834 def add(self: Tuple[Unpack[Ts]], other: Tuple[T]) -> Tuple[Unpack[Ts], T]:
1835 ...
1836 @overload
1837 def add(self: Tuple[T, ...], other: Tuple[T, ...]) -> Tuple[T, ...]:
1838 ...
1839 def add(self: Any, other: Any) -> Any:
1840 ...
1841 def test(a: Tuple[int, str], b: Tuple[bool], c: Tuple[bool, ...]):
1842 reveal_type(add(a, b)) # N: Revealed type is "tuple[builtins.int, builtins.str, builtins.bool]"
1843 reveal_type(add(b, c)) # N: Revealed type is "builtins.tuple[builtins.bool, ...]"
1844 [builtins fixtures/tuple.pyi]
1845
1846 [case testTypeVarTupleOverloadOverlap]
1847 from typing import Union, overload, Tuple
1848 from typing_extensions import Unpack
1849
1850 class Int(int): ...
1851
1852 A = Tuple[int, Unpack[Tuple[int, ...]]]
1853 B = Tuple[int, Unpack[Tuple[str, ...]]]
1854
1855 @overload
1856 def f(arg: A) -> int: ...
1857 @overload
1858 def f(arg: B) -> str: ...
1859 def f(arg: Union[A, B]) -> Union[int, str]:
1860 ...
1861
1862 A1 = Tuple[int, Unpack[Tuple[Int, ...]]]
1863 B1 = Tuple[Unpack[Tuple[Int, ...]], int]
1864
1865 @overload
1866 def f1(arg: A1) -> int: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
1867 @overload
1868 def f1(arg: B1) -> str: ...
1869 def f1(arg: Union[A1, B1]) -> Union[int, str]:
1870 ...
1871
1872 A2 = Tuple[int, int, int]
1873 B2 = Tuple[int, Unpack[Tuple[int, ...]]]
1874
1875 @overload
1876 def f2(arg: A2) -> int: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
1877 @overload
1878 def f2(arg: B2) -> str: ...
1879 def f2(arg: Union[A2, B2]) -> Union[int, str]:
1880 ...
1881
1882 A3 = Tuple[int, int, int]
1883 B3 = Tuple[int, Unpack[Tuple[str, ...]]]
1884
1885 @overload
1886 def f3(arg: A3) -> int: ...
1887 @overload
1888 def f3(arg: B3) -> str: ...
1889 def f3(arg: Union[A3, B3]) -> Union[int, str]:
1890 ...
1891
1892 A4 = Tuple[int, int, Unpack[Tuple[int, ...]]]
1893 B4 = Tuple[int]
1894
1895 @overload
1896 def f4(arg: A4) -> int: ...
1897 @overload
1898 def f4(arg: B4) -> str: ...
1899 def f4(arg: Union[A4, B4]) -> Union[int, str]:
1900 ...
1901 [builtins fixtures/tuple.pyi]
1902
1903 [case testTypeVarTupleIndexOldStyleNonNormalizedAndNonLiteral]
1904 from typing import Any, Tuple
1905 from typing_extensions import Unpack
1906
1907 t: Tuple[Unpack[Tuple[int, ...]]]
1908 reveal_type(t[42]) # N: Revealed type is "builtins.int"
1909 i: int
1910 reveal_type(t[i]) # N: Revealed type is "builtins.int"
1911 t1: Tuple[int, Unpack[Tuple[int, ...]]]
1912 reveal_type(t1[i]) # N: Revealed type is "builtins.int"
1913 [builtins fixtures/tuple.pyi]
1914
1915 [case testTypeVarTupleNotConcreteCallable]
1916 from typing_extensions import Unpack, TypeVarTuple
1917 from typing import Callable, TypeVar, Tuple
1918
1919 T = TypeVar("T")
1920 Args = TypeVarTuple("Args")
1921 Args2 = TypeVarTuple("Args2")
1922
1923 def submit(fn: Callable[[Unpack[Args]], T], *args: Unpack[Args]) -> T:
1924 ...
1925
1926 def submit2(fn: Callable[[int, Unpack[Args]], T], *args: Unpack[Tuple[int, Unpack[Args]]]) -> T:
1927 ...
1928
1929 def foo(func: Callable[[Unpack[Args]], T], *args: Unpack[Args]) -> T:
1930 return submit(func, *args)
1931
1932 def foo2(func: Callable[[Unpack[Args2]], T], *args: Unpack[Args2]) -> T:
1933 return submit(func, *args)
1934
1935 def foo3(func: Callable[[int, Unpack[Args2]], T], *args: Unpack[Args2]) -> T:
1936 return submit2(func, 1, *args)
1937
1938 def foo_bad(func: Callable[[Unpack[Args2]], T], *args: Unpack[Args2]) -> T:
1939 return submit2(func, 1, *args) # E: Argument 1 to "submit2" has incompatible type "def (*Unpack[Args2]) -> T"; expected "def (int, /, *Unpack[Args2]) -> T"
1940 [builtins fixtures/tuple.pyi]
1941
1942 [case testTypeVarTupleParamSpecInteraction]
1943 from typing_extensions import Unpack, TypeVarTuple, ParamSpec
1944 from typing import Callable, TypeVar
1945
1946 T = TypeVar("T")
1947 Args = TypeVarTuple("Args")
1948 Args2 = TypeVarTuple("Args2")
1949 P = ParamSpec("P")
1950
1951 def submit(fn: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
1952 ...
1953
1954 def foo(func: Callable[[Unpack[Args]], T], *args: Unpack[Args]) -> T:
1955 return submit(func, *args)
1956
1957 def foo2(func: Callable[[Unpack[Args]], T], *args: Unpack[Args2]) -> T:
1958 return submit(func, *args) # E: Argument 2 to "submit" has incompatible type "*tuple[Unpack[Args2]]"; expected "Unpack[Args]"
1959
1960 def foo3(func: Callable[[int, Unpack[Args2]], T], *args: Unpack[Args2]) -> T:
1961 return submit(func, 1, *args)
1962 [builtins fixtures/tuple.pyi]
1963
1964 [case testTypeVarTupleEmptySpecialCase]
1965 from typing import Any, Callable, Generic
1966 from typing_extensions import Unpack, TypeVarTuple
1967
1968 Ts = TypeVarTuple("Ts")
1969 class MyClass(Generic[Unpack[Ts]]):
1970 func: Callable[[Unpack[Ts]], object]
1971
1972 def __init__(self, func: Callable[[Unpack[Ts]], object]) -> None:
1973 self.func = func
1974
1975 explicit: MyClass[()]
1976 reveal_type(explicit) # N: Revealed type is "__main__.MyClass[()]"
1977 reveal_type(explicit.func) # N: Revealed type is "def () -> builtins.object"
1978
1979 a: Any
1980 explicit_2 = MyClass[()](a)
1981 reveal_type(explicit_2) # N: Revealed type is "__main__.MyClass[()]"
1982 reveal_type(explicit_2.func) # N: Revealed type is "def () -> builtins.object"
1983
1984 Alias = MyClass[()]
1985 explicit_3: Alias
1986 reveal_type(explicit_3) # N: Revealed type is "__main__.MyClass[()]"
1987 reveal_type(explicit_3.func) # N: Revealed type is "def () -> builtins.object"
1988
1989 explicit_4 = Alias(a)
1990 reveal_type(explicit_4) # N: Revealed type is "__main__.MyClass[()]"
1991 reveal_type(explicit_4.func) # N: Revealed type is "def () -> builtins.object"
1992
1993 def no_args() -> None: ...
1994 implicit = MyClass(no_args)
1995 reveal_type(implicit) # N: Revealed type is "__main__.MyClass[()]"
1996 reveal_type(implicit.func) # N: Revealed type is "def () -> builtins.object"
1997
1998 def one_arg(__a: int) -> None: ...
1999 x = MyClass(one_arg)
2000 x = explicit # E: Incompatible types in assignment (expression has type "MyClass[()]", variable has type "MyClass[int]")
2001
2002 # Consistently handle special case for no argument aliases
2003 Direct = MyClass
2004 y = Direct(one_arg)
2005 reveal_type(y) # N: Revealed type is "__main__.MyClass[builtins.int]"
2006 [builtins fixtures/tuple.pyi]
2007
2008 [case testTypeVarTupleRuntimeTypeApplication]
2009 from typing import Generic, TypeVar, Tuple
2010 from typing_extensions import Unpack, TypeVarTuple
2011
2012 T = TypeVar("T")
2013 S = TypeVar("S")
2014 Ts = TypeVarTuple("Ts")
2015 class C(Generic[T, Unpack[Ts], S]): ...
2016
2017 Ints = Tuple[int, int]
2018 x = C[Unpack[Ints]]()
2019 reveal_type(x) # N: Revealed type is "__main__.C[builtins.int, builtins.int]"
2020
2021 y = C[Unpack[Tuple[int, ...]]]()
2022 reveal_type(y) # N: Revealed type is "__main__.C[builtins.int, Unpack[builtins.tuple[builtins.int, ...]], builtins.int]"
2023
2024 z = C[int]() # E: Bad number of arguments, expected: at least 2, given: 1
2025 reveal_type(z) # N: Revealed type is "__main__.C[Any, Unpack[builtins.tuple[Any, ...]], Any]"
2026 [builtins fixtures/tuple.pyi]
2027
2028 [case testVariadicTupleTupleSubclassPrefixSuffix]
2029 from typing import Tuple
2030 from typing_extensions import Unpack
2031
2032 i: int
2033
2034 class A(Tuple[int, Unpack[Tuple[int, ...]]]): ...
2035 a: A
2036 reveal_type(a[i]) # N: Revealed type is "builtins.int"
2037
2038 class B(Tuple[Unpack[Tuple[int, ...]], int]): ...
2039 b: B
2040 reveal_type(b[i]) # N: Revealed type is "builtins.int"
2041 [builtins fixtures/tuple.pyi]
2042
2043 [case testVariadicClassSubclassInit]
2044 from typing import Tuple, Generic, TypeVar
2045 from typing_extensions import TypeVarTuple, Unpack
2046
2047 Ts = TypeVarTuple("Ts")
2048 class B(Generic[Unpack[Ts]]):
2049 def __init__(self, x: Tuple[Unpack[Ts]], *args: Unpack[Ts]) -> None: ...
2050 reveal_type(B) # N: Revealed type is "def [Ts] (x: tuple[Unpack[Ts`1]], *args: Unpack[Ts`1]) -> __main__.B[Unpack[Ts`1]]"
2051
2052 T = TypeVar("T")
2053 S = TypeVar("S")
2054 class C(B[T, S]): ...
2055 reveal_type(C) # N: Revealed type is "def [T, S] (x: tuple[T`1, S`2], T`1, S`2) -> __main__.C[T`1, S`2]"
2056 [builtins fixtures/tuple.pyi]
2057
2058 [case testVariadicClassGenericSelf]
2059 from typing import Tuple, Generic, TypeVar
2060 from typing_extensions import TypeVarTuple, Unpack
2061
2062 T = TypeVar("T")
2063 S = TypeVar("S")
2064 Ts = TypeVarTuple("Ts")
2065 class B(Generic[Unpack[Ts]]):
2066 def copy(self: T) -> T: ...
2067 def on_pair(self: B[T, S]) -> Tuple[T, S]: ...
2068
2069 b1: B[int]
2070 reveal_type(b1.on_pair()) # E: Invalid self argument "B[int]" to attribute function "on_pair" with type "Callable[[B[T, S]], tuple[T, S]]" \
2071 # N: Revealed type is "tuple[Never, Never]"
2072 b2: B[int, str]
2073 reveal_type(b2.on_pair()) # N: Revealed type is "tuple[builtins.int, builtins.str]"
2074 b3: B[int, str, int]
2075 reveal_type(b3.on_pair()) # E: Invalid self argument "B[int, str, int]" to attribute function "on_pair" with type "Callable[[B[T, S]], tuple[T, S]]" \
2076 # N: Revealed type is "tuple[Never, Never]"
2077
2078 class C(B[T, S]): ...
2079 c: C[int, str]
2080 reveal_type(c.copy()) # N: Revealed type is "__main__.C[builtins.int, builtins.str]"
2081 [builtins fixtures/tuple.pyi]
2082
2083 [case testVariadicClassNewStyleSelf]
2084 from typing import Generic, TypeVar, Self
2085 from typing_extensions import TypeVarTuple, Unpack
2086
2087 Ts = TypeVarTuple("Ts")
2088 class B(Generic[Unpack[Ts]]):
2089 next: Self
2090 def copy(self) -> Self:
2091 return self.next
2092
2093 b: B[int, str, int]
2094 reveal_type(b.next) # N: Revealed type is "__main__.B[builtins.int, builtins.str, builtins.int]"
2095 reveal_type(b.copy()) # N: Revealed type is "__main__.B[builtins.int, builtins.str, builtins.int]"
2096 reveal_type(B.copy(b)) # N: Revealed type is "__main__.B[builtins.int, builtins.str, builtins.int]"
2097
2098 T = TypeVar("T")
2099 S = TypeVar("S")
2100 class C(B[T, S]): ...
2101 c: C[int, str]
2102
2103 reveal_type(c.next) # N: Revealed type is "__main__.C[builtins.int, builtins.str]"
2104 reveal_type(c.copy()) # N: Revealed type is "__main__.C[builtins.int, builtins.str]"
2105 reveal_type(C.copy(c)) # N: Revealed type is "__main__.C[builtins.int, builtins.str]"
2106 [builtins fixtures/tuple.pyi]
2107
2108 [case testVariadicTupleDataclass]
2109 from dataclasses import dataclass
2110 from typing import Generic, TypeVar, Tuple
2111 from typing_extensions import TypeVarTuple, Unpack
2112
2113 Ts = TypeVarTuple("Ts")
2114
2115 @dataclass
2116 class B(Generic[Unpack[Ts]]):
2117 items: Tuple[Unpack[Ts]]
2118
2119 reveal_type(B) # N: Revealed type is "def [Ts] (items: tuple[Unpack[Ts`1]]) -> __main__.B[Unpack[Ts`1]]"
2120 b = B((1, "yes"))
2121 reveal_type(b.items) # N: Revealed type is "tuple[builtins.int, builtins.str]"
2122
2123 T = TypeVar("T")
2124 S = TypeVar("S")
2125
2126 @dataclass
2127 class C(B[T, S]):
2128 first: T
2129 second: S
2130
2131 reveal_type(C) # N: Revealed type is "def [T, S] (items: tuple[T`1, S`2], first: T`1, second: S`2) -> __main__.C[T`1, S`2]"
2132 c = C((1, "yes"), 2, "no")
2133 reveal_type(c.items) # N: Revealed type is "tuple[builtins.int, builtins.str]"
2134 reveal_type(c.first) # N: Revealed type is "builtins.int"
2135 reveal_type(c.second) # N: Revealed type is "builtins.str"
2136 [builtins fixtures/dataclasses.pyi]
2137 [typing fixtures/typing-medium.pyi]
2138
2139 [case testVariadicTupleInProtocol]
2140 from typing import Protocol, Tuple, List
2141 from typing_extensions import TypeVarTuple, Unpack
2142
2143 Ts = TypeVarTuple("Ts")
2144 class P(Protocol[Unpack[Ts]]):
2145 def items(self) -> Tuple[Unpack[Ts]]: ...
2146
2147 class PC(Protocol[Unpack[Ts]]):
2148 def meth(self, *args: Unpack[Ts]) -> None: ...
2149
2150 def get_items(x: P[Unpack[Ts]]) -> Tuple[Unpack[Ts]]: ...
2151 def match(x: PC[Unpack[Ts]]) -> Tuple[Unpack[Ts]]: ...
2152
2153 class Bad:
2154 def items(self) -> List[int]: ...
2155 def meth(self, *, named: int) -> None: ...
2156
2157 class Good:
2158 def items(self) -> Tuple[int, str]: ...
2159 def meth(self, __x: int, y: str) -> None: ...
2160
2161 g: Good
2162 reveal_type(get_items(g)) # N: Revealed type is "tuple[builtins.int, builtins.str]"
2163 reveal_type(match(g)) # N: Revealed type is "tuple[builtins.int, builtins.str]"
2164
2165 b: Bad
2166 get_items(b) # E: Argument 1 to "get_items" has incompatible type "Bad"; expected "P[Unpack[tuple[Never, ...]]]" \
2167 # N: Following member(s) of "Bad" have conflicts: \
2168 # N: Expected: \
2169 # N: def items(self) -> tuple[Never, ...] \
2170 # N: Got: \
2171 # N: def items(self) -> list[int]
2172 match(b) # E: Argument 1 to "match" has incompatible type "Bad"; expected "PC[Unpack[tuple[Never, ...]]]" \
2173 # N: Following member(s) of "Bad" have conflicts: \
2174 # N: Expected: \
2175 # N: def meth(self, *args: Never) -> None \
2176 # N: Got: \
2177 # N: def meth(self, *, named: int) -> None
2178 [builtins fixtures/tuple.pyi]
2179
2180 [case testVariadicTupleCollectionCheck]
2181 from typing_extensions import Unpack
2182
2183 allowed: tuple[int, Unpack[tuple[int, ...]]]
2184
2185 def f(x: int | None):
2186 if x in allowed:
2187 reveal_type(x) # N: Revealed type is "builtins.int"
2188 [builtins fixtures/tuple.pyi]
2189
2190 [case testJoinOfVariadicTupleCallablesNoCrash]
2191 # flags: --python-version 3.11
2192 from typing import Callable, Tuple
2193
2194 f: Callable[[int, *Tuple[str, ...], int], None]
2195 g: Callable[[int, *Tuple[str, ...], int], None]
2196 reveal_type([f, g]) # N: Revealed type is "builtins.list[def (builtins.int, *Unpack[tuple[Unpack[builtins.tuple[builtins.str, ...]], builtins.int]])]"
2197
2198 h: Callable[[int, *Tuple[str, ...], str], None]
2199 reveal_type([f, h]) # N: Revealed type is "builtins.list[def (builtins.int, *Unpack[tuple[Unpack[builtins.tuple[builtins.str, ...]], Never]])]"
2200 [builtins fixtures/tuple.pyi]
2201
2202 [case testTypeVarTupleBothUnpacksSimple]
2203 from typing import Tuple, TypedDict
2204 from typing_extensions import Unpack, TypeVarTuple
2205
2206 class Keywords(TypedDict):
2207 a: str
2208 b: str
2209
2210 Ints = Tuple[int, ...]
2211
2212 def f(*args: Unpack[Ints], other: str = "no", **kwargs: Unpack[Keywords]) -> None: ...
2213 reveal_type(f) # N: Revealed type is "def (*args: builtins.int, other: builtins.str =, **kwargs: Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])"
2214 f(1, 2, a="a", b="b") # OK
2215 f(1, 2, 3) # E: Missing named argument "a" for "f" \
2216 # E: Missing named argument "b" for "f"
2217
2218 Ts = TypeVarTuple("Ts")
2219 def g(*args: Unpack[Ts], other: str = "no", **kwargs: Unpack[Keywords]) -> None: ...
2220 reveal_type(g) # N: Revealed type is "def [Ts] (*args: Unpack[Ts`-1], other: builtins.str =, **kwargs: Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])"
2221 g(1, 2, a="a", b="b") # OK
2222 g(1, 2, 3) # E: Missing named argument "a" for "g" \
2223 # E: Missing named argument "b" for "g"
2224
2225 def bad(
2226 *args: Unpack[Keywords], # E: "Keywords" cannot be unpacked (must be tuple or TypeVarTuple)
2227 **kwargs: Unpack[Ints], # E: Unpack item in ** parameter must be a TypedDict
2228 ) -> None: ...
2229 reveal_type(bad) # N: Revealed type is "def (*args: Any, **kwargs: Any)"
2230
2231 def bad2(
2232 one: int,
2233 *args: Unpack[Keywords], # E: "Keywords" cannot be unpacked (must be tuple or TypeVarTuple)
2234 other: str = "no",
2235 **kwargs: Unpack[Ints], # E: Unpack item in ** parameter must be a TypedDict
2236 ) -> None: ...
2237 reveal_type(bad2) # N: Revealed type is "def (one: builtins.int, *args: Any, other: builtins.str =, **kwargs: Any)"
2238 [builtins fixtures/dict.pyi]
2239 [typing fixtures/typing-typeddict.pyi]
2240
2241 [case testTypeVarTupleBothUnpacksCallable]
2242 from typing import Callable, Tuple, TypedDict
2243 from typing_extensions import Unpack
2244
2245 class Keywords(TypedDict):
2246 a: str
2247 b: str
2248 Ints = Tuple[int, ...]
2249
2250 cb: Callable[[Unpack[Ints], Unpack[Keywords]], None]
2251 reveal_type(cb) # N: Revealed type is "def (*builtins.int, **Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])"
2252
2253 cb2: Callable[[int, Unpack[Ints], int, Unpack[Keywords]], None]
2254 reveal_type(cb2) # N: Revealed type is "def (builtins.int, *Unpack[tuple[Unpack[builtins.tuple[builtins.int, ...]], builtins.int]], **Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])"
2255 cb2(1, 2, 3, a="a", b="b")
2256 cb2(1, a="a", b="b") # E: Too few arguments
2257 cb2(1, 2, 3, a="a") # E: Missing named argument "b"
2258
2259 bad1: Callable[[Unpack[Ints], Unpack[Ints]], None] # E: More than one variadic Unpack in a type is not allowed
2260 reveal_type(bad1) # N: Revealed type is "def (*builtins.int)"
2261 bad2: Callable[[Unpack[Keywords], Unpack[Keywords]], None] # E: "Keywords" cannot be unpacked (must be tuple or TypeVarTuple)
2262 reveal_type(bad2) # N: Revealed type is "def (*Any, **Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])"
2263 bad3: Callable[[Unpack[Keywords], Unpack[Ints]], None] # E: "Keywords" cannot be unpacked (must be tuple or TypeVarTuple) \
2264 # E: More than one variadic Unpack in a type is not allowed
2265 reveal_type(bad3) # N: Revealed type is "def (*Any)"
2266 [builtins fixtures/dict.pyi]
2267 [typing fixtures/typing-typeddict.pyi]
2268
2269 [case testTypeVarTupleBothUnpacksApplication]
2270 from typing import Callable, TypedDict, TypeVar, Optional
2271 from typing_extensions import Unpack, TypeVarTuple
2272
2273 class Keywords(TypedDict):
2274 a: str
2275 b: str
2276
2277 T = TypeVar("T")
2278 Ts = TypeVarTuple("Ts")
2279 def test(
2280 x: int,
2281 func: Callable[[Unpack[Ts]], T],
2282 *args: Unpack[Ts],
2283 other: Optional[str] = None,
2284 **kwargs: Unpack[Keywords],
2285 ) -> T:
2286 if bool():
2287 func(*args, **kwargs) # E: Extra argument "a" from **args
2288 return func(*args)
2289 def test2(
2290 x: int,
2291 func: Callable[[Unpack[Ts], Unpack[Keywords]], T],
2292 *args: Unpack[Ts],
2293 other: Optional[str] = None,
2294 **kwargs: Unpack[Keywords],
2295 ) -> T:
2296 if bool():
2297 func(*args) # E: Missing named argument "a" \
2298 # E: Missing named argument "b"
2299 return func(*args, **kwargs)
2300 [builtins fixtures/dict.pyi]
2301 [typing fixtures/typing-typeddict.pyi]
2302
2303 [case testUnpackTupleSpecialCaseNoCrash]
2304 from typing import Tuple, TypeVar
2305 from typing_extensions import Unpack
2306
2307 T = TypeVar("T")
2308
2309 def foo(*x: object) -> None: ...
2310 def bar(*x: int) -> None: ...
2311 def baz(*x: T) -> T: ...
2312
2313 keys: Tuple[Unpack[Tuple[int, ...]]]
2314
2315 foo(keys, 1)
2316 foo(*keys, 1)
2317
2318 bar(keys, 1) # E: Argument 1 to "bar" has incompatible type "tuple[Unpack[tuple[int, ...]]]"; expected "int"
2319 bar(*keys, 1) # OK
2320
2321 reveal_type(baz(keys, 1)) # N: Revealed type is "builtins.object"
2322 reveal_type(baz(*keys, 1)) # N: Revealed type is "builtins.int"
2323 [builtins fixtures/tuple.pyi]
2324
2325 [case testVariadicTupleContextNoCrash]
2326 from typing import Tuple, Unpack
2327
2328 x: Tuple[int, Unpack[Tuple[int, ...]]] = () # E: Incompatible types in assignment (expression has type "tuple[()]", variable has type "tuple[int, Unpack[tuple[int, ...]]]")
2329 y: Tuple[int, Unpack[Tuple[int, ...]]] = (1, 2)
2330 z: Tuple[int, Unpack[Tuple[int, ...]]] = (1,)
2331 w: Tuple[int, Unpack[Tuple[int, ...]]] = (1, *[2, 3, 4])
2332 t: Tuple[int, Unpack[Tuple[int, ...]]] = (1, *(2, 3, 4))
2333 [builtins fixtures/tuple.pyi]
2334
2335 [case testAliasToCallableWithUnpack]
2336 from typing import Any, Callable, Tuple, Unpack
2337
2338 _CallableValue = Callable[[Unpack[Tuple[Any, ...]]], Any]
2339 def higher_order(f: _CallableValue) -> None: ...
2340
2341 def good1(*args: int) -> None: ...
2342 def good2(*args: str) -> int: ...
2343
2344 # These are special-cased for *args: Any (as opposite to *args: object)
2345 def ok1(a: str, b: int, /) -> None: ...
2346 def ok2(c: bytes, *args: int) -> str: ...
2347
2348 def bad1(*, d: str) -> int: ...
2349 def bad2(**kwargs: None) -> None: ...
2350
2351 higher_order(good1)
2352 higher_order(good2)
2353
2354 higher_order(ok1)
2355 higher_order(ok2)
2356
2357 higher_order(bad1) # E: Argument 1 to "higher_order" has incompatible type "def bad1(*, d: str) -> int"; expected "def (*Any) -> Any"
2358 higher_order(bad2) # E: Argument 1 to "higher_order" has incompatible type "def bad2(**kwargs: None) -> None"; expected "def (*Any) -> Any"
2359 [builtins fixtures/tuple.pyi]
2360
2361 [case testAliasToCallableWithUnpack2]
2362 from typing import Any, Callable, Tuple, Unpack
2363
2364 _CallableValue = Callable[[int, str, Unpack[Tuple[Any, ...]], int], Any]
2365 def higher_order(f: _CallableValue) -> None: ...
2366
2367 def good(a: int, b: str, *args: Unpack[Tuple[Unpack[Tuple[Any, ...]], int]]) -> int: ...
2368 def bad1(a: str, b: int, /) -> None: ...
2369 def bad2(c: bytes, *args: int) -> str: ...
2370 def bad3(*, d: str) -> int: ...
2371 def bad4(**kwargs: None) -> None: ...
2372
2373 higher_order(good)
2374 higher_order(bad1) # E: Argument 1 to "higher_order" has incompatible type "Callable[[str, int], None]"; expected "def (int, str, /, *Unpack[tuple[Unpack[tuple[Any, ...]], int]]) -> Any"
2375 higher_order(bad2) # E: Argument 1 to "higher_order" has incompatible type "def bad2(c: bytes, *args: int) -> str"; expected "def (int, str, /, *Unpack[tuple[Unpack[tuple[Any, ...]], int]]) -> Any"
2376 higher_order(bad3) # E: Argument 1 to "higher_order" has incompatible type "def bad3(*, d: str) -> int"; expected "def (int, str, /, *Unpack[tuple[Unpack[tuple[Any, ...]], int]]) -> Any"
2377 higher_order(bad4) # E: Argument 1 to "higher_order" has incompatible type "def bad4(**kwargs: None) -> None"; expected "def (int, str, /, *Unpack[tuple[Unpack[tuple[Any, ...]], int]]) -> Any"
2378 [builtins fixtures/tuple.pyi]
2379
2380 [case testAliasToCallableWithUnpackInvalid]
2381 from typing import Any, Callable, List, Tuple, TypeVar, Unpack
2382
2383 T = TypeVar("T")
2384 Ts = TypeVarTuple("Ts") # E: Name "TypeVarTuple" is not defined
2385
2386 def good(*x: int) -> int: ...
2387 def bad(*x: int, y: int) -> int: ...
2388
2389 Alias = Callable[[Unpack[T]], int] # E: "T" cannot be unpacked (must be tuple or TypeVarTuple)
2390 x: Alias[int]
2391 reveal_type(x) # N: Revealed type is "def (*Any) -> builtins.int"
2392 x = good
2393 x = bad # E: Incompatible types in assignment (expression has type "def bad(*x: int, y: int) -> int", variable has type "def (*Any) -> int")
2394 [builtins fixtures/tuple.pyi]
2395
2396 [case testTypeVarTupleInvariant]
2397 from typing import Generic, Tuple
2398 from typing_extensions import Unpack, TypeVarTuple
2399 Ts = TypeVarTuple("Ts")
2400
2401 class Array(Generic[Unpack[Ts]]): ...
2402
2403 def pointwise_multiply(x: Array[Unpack[Ts]], y: Array[Unpack[Ts]]) -> Array[Unpack[Ts]]: ...
2404
2405 def a1(x: Array[int], y: Array[str], z: Array[int, str]) -> None:
2406 reveal_type(pointwise_multiply(x, x)) # N: Revealed type is "__main__.Array[builtins.int]"
2407 reveal_type(pointwise_multiply(x, y)) # E: Cannot infer value of type parameter "Ts" of "pointwise_multiply" \
2408 # N: Revealed type is "__main__.Array[Unpack[builtins.tuple[Any, ...]]]"
2409 reveal_type(pointwise_multiply(x, z)) # E: Cannot infer value of type parameter "Ts" of "pointwise_multiply" \
2410 # N: Revealed type is "__main__.Array[Unpack[builtins.tuple[Any, ...]]]"
2411
2412 def func(x: Array[Unpack[Ts]], *args: Unpack[Ts]) -> Tuple[Unpack[Ts]]:
2413 ...
2414
2415 def a2(x: Array[int, str]) -> None:
2416 reveal_type(func(x, 2, "Hello")) # N: Revealed type is "tuple[builtins.int, builtins.str]"
2417 reveal_type(func(x, 2)) # E: Cannot infer value of type parameter "Ts" of "func" \
2418 # N: Revealed type is "builtins.tuple[Any, ...]"
2419 reveal_type(func(x, 2, "Hello", True)) # E: Cannot infer value of type parameter "Ts" of "func" \
2420 # N: Revealed type is "builtins.tuple[Any, ...]"
2421 [builtins fixtures/tuple.pyi]
2422
2423 [case testTypeVarTupleTypeApplicationOverload]
2424 from typing import Generic, TypeVar, TypeVarTuple, Unpack, overload, Callable
2425
2426 T = TypeVar("T")
2427 T1 = TypeVar("T1")
2428 T2 = TypeVar("T2")
2429 T3 = TypeVar("T3")
2430 Ts = TypeVarTuple("Ts")
2431
2432 class C(Generic[T, Unpack[Ts]]):
2433 @overload
2434 def __init__(self, f: Callable[[Unpack[Ts]], T]) -> None: ...
2435 @overload
2436 def __init__(self, f: Callable[[T1, T2, T3, Unpack[Ts]], T], a: T1, b: T2, c: T3) -> None: ...
2437 def __init__(self, f, *args, **kwargs) -> None:
2438 ...
2439
2440 reveal_type(C[int, str]) # N: Revealed type is "Overload(def (f: def (builtins.str) -> builtins.int) -> __main__.C[builtins.int, builtins.str], def [T1, T2, T3] (f: def (T1`-1, T2`-2, T3`-3, builtins.str) -> builtins.int, a: T1`-1, b: T2`-2, c: T3`-3) -> __main__.C[builtins.int, builtins.str])"
2441 Alias = C[int, str]
2442
2443 def f(x: int, y: int, z: int, t: int) -> str: ...
2444 x = C(f, 0, 0, "hm") # E: Argument 1 to "C" has incompatible type "Callable[[int, int, int, int], str]"; expected "Callable[[int, int, str, int], str]"
2445 reveal_type(x) # N: Revealed type is "__main__.C[builtins.str, builtins.int]"
2446 reveal_type(C(f)) # N: Revealed type is "__main__.C[builtins.str, builtins.int, builtins.int, builtins.int, builtins.int]"
2447 C[()] # E: At least 1 type argument(s) expected, none given
2448 [builtins fixtures/tuple.pyi]
2449
2450 [case testTypeVarTupleAgainstParamSpecActualSuccess]
2451 from typing import Generic, TypeVar, TypeVarTuple, Unpack, Callable, Tuple, List
2452 from typing_extensions import ParamSpec
2453
2454 R = TypeVar("R")
2455 P = ParamSpec("P")
2456
2457 class CM(Generic[R]): ...
2458 def cm(fn: Callable[P, R]) -> Callable[P, CM[R]]: ...
2459
2460 Ts = TypeVarTuple("Ts")
2461 @cm
2462 def test(*args: Unpack[Ts]) -> Tuple[Unpack[Ts]]: ...
2463
2464 reveal_type(test) # N: Revealed type is "def [Ts] (*args: Unpack[Ts`-1]) -> __main__.CM[tuple[Unpack[Ts`-1]]]"
2465 reveal_type(test(1, 2, 3)) # N: Revealed type is "__main__.CM[tuple[Literal[1]?, Literal[2]?, Literal[3]?]]"
2466 [builtins fixtures/tuple.pyi]
2467
2468 [case testTypeVarTupleAgainstParamSpecActualFailedNoCrash_no_verbose_reveal]
2469 from typing import Generic, TypeVar, TypeVarTuple, Unpack, Callable, Tuple, List
2470 from typing_extensions import ParamSpec
2471
2472 R = TypeVar("R")
2473 P = ParamSpec("P")
2474
2475 class CM(Generic[R]): ...
2476 def cm(fn: Callable[P, List[R]]) -> Callable[P, CM[R]]: ...
2477
2478 Ts = TypeVarTuple("Ts")
2479 @cm # E: Argument 1 to "cm" has incompatible type "def [Ts] test(*args: *Ts) -> tuple[*Ts]"; expected "def (*args: Never) -> list[Never]"
2480 def test(*args: Unpack[Ts]) -> Tuple[Unpack[Ts]]: ...
2481
2482 reveal_type(test) # N: Revealed type is "def (*args: Never) -> __main__.CM[Never]"
2483 [builtins fixtures/tuple.pyi]
2484
2485 [case testTypeVarTupleAgainstParamSpecActualPrefix]
2486 from typing import Generic, TypeVar, TypeVarTuple, Unpack, Callable, Tuple, List
2487 from typing_extensions import ParamSpec, Concatenate
2488
2489 R = TypeVar("R")
2490 P = ParamSpec("P")
2491 T = TypeVar("T")
2492
2493 class CM(Generic[R]): ...
2494 def cm(fn: Callable[Concatenate[T, P], R]) -> Callable[Concatenate[List[T], P], CM[R]]: ...
2495
2496 Ts = TypeVarTuple("Ts")
2497 @cm
2498 def test(x: T, *args: Unpack[Ts]) -> Tuple[T, Unpack[Ts]]: ...
2499
2500 reveal_type(test) # N: Revealed type is "def [T, Ts] (builtins.list[T`2], *args: Unpack[Ts`-2]) -> __main__.CM[tuple[T`2, Unpack[Ts`-2]]]"
2501 [builtins fixtures/tuple.pyi]
2502
2503 [case testMixingTypeVarTupleAndParamSpec]
2504 from typing import Generic, ParamSpec, TypeVarTuple, Unpack, Callable, TypeVar
2505
2506 P = ParamSpec("P")
2507 Ts = TypeVarTuple("Ts")
2508
2509 class A(Generic[P, Unpack[Ts]]): ...
2510 class B(Generic[Unpack[Ts], P]): ...
2511
2512 a: A[[int, str], int, str]
2513 reveal_type(a) # N: Revealed type is "__main__.A[[builtins.int, builtins.str], builtins.int, builtins.str]"
2514 b: B[int, str, [int, str]]
2515 reveal_type(b) # N: Revealed type is "__main__.B[builtins.int, builtins.str, [builtins.int, builtins.str]]"
2516
2517 x: A[int, str, [int, str]] # E: Cannot use "[int, str]" for TypeVarTuple, only for ParamSpec \
2518 # E: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "int"
2519 reveal_type(x) # N: Revealed type is "__main__.A[Any, Unpack[builtins.tuple[Any, ...]]]"
2520 y: B[[int, str], int, str] # E: Cannot use "[int, str]" for TypeVarTuple, only for ParamSpec \
2521 # E: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "str"
2522 reveal_type(y) # N: Revealed type is "__main__.B[Unpack[builtins.tuple[Any, ...]], Any]"
2523
2524 R = TypeVar("R")
2525 class C(Generic[P, R]):
2526 fn: Callable[P, None]
2527
2528 c: C[int, str] # E: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "int"
2529 reveal_type(c.fn) # N: Revealed type is "def (*Any, **Any)"
2530 [builtins fixtures/tuple.pyi]
2531
2532 [case testTypeVarTupleInstanceOverlap]
2533 # flags: --strict-equality
2534 from typing import TypeVarTuple, Unpack, Generic
2535
2536 Ts = TypeVarTuple("Ts")
2537
2538 class Foo(Generic[Unpack[Ts]]):
2539 pass
2540
2541 x1: Foo[Unpack[tuple[int, ...]]]
2542 y1: Foo[Unpack[tuple[str, ...]]]
2543 x1 is y1 # E: Non-overlapping identity check (left operand type: "Foo[Unpack[tuple[int, ...]]]", right operand type: "Foo[Unpack[tuple[str, ...]]]")
2544
2545 x2: Foo[Unpack[tuple[int, ...]]]
2546 y2: Foo[Unpack[tuple[int, ...]]]
2547 x2 is y2
2548
2549 x3: Foo[Unpack[tuple[int, ...]]]
2550 y3: Foo[Unpack[tuple[int, int]]]
2551 x3 is y3
2552
2553 x4: Foo[Unpack[tuple[str, ...]]]
2554 y4: Foo[Unpack[tuple[int, int]]]
2555 x4 is y4 # E: Non-overlapping identity check (left operand type: "Foo[Unpack[tuple[str, ...]]]", right operand type: "Foo[int, int]")
2556 [builtins fixtures/tuple.pyi]
2557
2558 [case testTypeVarTupleErasureNormalized]
2559 from typing import TypeVarTuple, Unpack, Generic, Union
2560 from collections.abc import Callable
2561
2562 Args = TypeVarTuple("Args")
2563
2564 class Built(Generic[Unpack[Args]]):
2565 pass
2566
2567 def example(
2568 fn: Union[Built[Unpack[Args]], Callable[[Unpack[Args]], None]]
2569 ) -> Built[Unpack[Args]]: ...
2570
2571 @example
2572 def command() -> None:
2573 return
2574 reveal_type(command) # N: Revealed type is "__main__.Built[()]"
2575 [builtins fixtures/tuple.pyi]
2576
2577 [case testTypeVarTupleSelfMappedPrefix]
2578 from typing import TypeVarTuple, Generic, Unpack
2579
2580 Ts = TypeVarTuple("Ts")
2581 class Base(Generic[Unpack[Ts]]):
2582 attr: tuple[Unpack[Ts]]
2583
2584 @property
2585 def prop(self) -> tuple[Unpack[Ts]]:
2586 return self.attr
2587
2588 def meth(self) -> tuple[Unpack[Ts]]:
2589 return self.attr
2590
2591 Ss = TypeVarTuple("Ss")
2592 class Derived(Base[str, Unpack[Ss]]):
2593 def test(self) -> None:
2594 reveal_type(self.attr) # N: Revealed type is "tuple[builtins.str, Unpack[Ss`1]]"
2595 reveal_type(self.prop) # N: Revealed type is "tuple[builtins.str, Unpack[Ss`1]]"
2596 reveal_type(self.meth()) # N: Revealed type is "tuple[builtins.str, Unpack[Ss`1]]"
2597 [builtins fixtures/property.pyi]
2598
2599 [case testTypeVarTupleProtocolPrefix]
2600 from typing import Protocol, Unpack, TypeVarTuple
2601
2602 Ts = TypeVarTuple("Ts")
2603 class A(Protocol[Unpack[Ts]]):
2604 def f(self, z: str, *args: Unpack[Ts]) -> None: ...
2605
2606 class C:
2607 def f(self, z: str, x: int) -> None: ...
2608
2609 def f(x: A[Unpack[Ts]]) -> tuple[Unpack[Ts]]: ...
2610
2611 reveal_type(f(C())) # N: Revealed type is "tuple[builtins.int]"
2612 [builtins fixtures/tuple.pyi]
2613
2614 [case testTypeVarTupleHomogeneousCallableNormalized]
2615 from typing import Generic, Unpack, TypeVarTuple
2616
2617 Ts = TypeVarTuple("Ts")
2618 class C(Generic[Unpack[Ts]]):
2619 def foo(self, *args: Unpack[Ts]) -> None: ...
2620
2621 c: C[Unpack[tuple[int, ...]]]
2622 reveal_type(c.foo) # N: Revealed type is "def (*args: builtins.int)"
2623 [builtins fixtures/tuple.pyi]
2624
2625 [case testTypeVarTupleJoinInstanceTypeVar]
2626 from typing import Any, Unpack, TypeVarTuple, TypeVar
2627
2628 T = TypeVar("T")
2629 Ts = TypeVarTuple("Ts")
2630
2631 def join(x: T, y: T) -> T: ...
2632 def test(xs: tuple[Unpack[Ts]], xsi: tuple[int, Unpack[Ts]]) -> None:
2633 a: tuple[Any, ...]
2634 reveal_type(join(xs, a)) # N: Revealed type is "builtins.tuple[Any, ...]"
2635 reveal_type(join(a, xs)) # N: Revealed type is "builtins.tuple[Any, ...]"
2636 aa: tuple[Unpack[tuple[Any, ...]]]
2637 reveal_type(join(xs, aa)) # N: Revealed type is "builtins.tuple[Any, ...]"
2638 reveal_type(join(aa, xs)) # N: Revealed type is "builtins.tuple[Any, ...]"
2639 ai: tuple[int, Unpack[tuple[Any, ...]]]
2640 reveal_type(join(xsi, ai)) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[Any, ...]]]"
2641 reveal_type(join(ai, xsi)) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[Any, ...]]]"
2642 [builtins fixtures/tuple.pyi]
2643
2644 [case testTypeVarTupleInferAgainstAnyCallableSuffix]
2645 # flags: --python-version 3.11
2646 from typing import Any, Callable, TypeVar, TypeVarTuple
2647
2648 Ts = TypeVarTuple("Ts")
2649 R = TypeVar("R")
2650 def deco(func: Callable[[*Ts, int], R]) -> Callable[[*Ts], R]:
2651 ...
2652
2653 untyped: Any
2654 reveal_type(deco(untyped)) # N: Revealed type is "def (*Any) -> Any"
2655 [builtins fixtures/tuple.pyi]
2656
2657 [case testNoCrashOnNonNormalUnpackInCallable]
2658 # flags: --python-version 3.11
2659 from typing import Callable, Unpack, TypeVar
2660
2661 T = TypeVar("T")
2662 def fn(f: Callable[[*tuple[T]], int]) -> Callable[[*tuple[T]], int]: ...
2663
2664 def test(*args: Unpack[tuple[T]]) -> int: ...
2665 reveal_type(fn(test)) # N: Revealed type is "def [T] (T`1) -> builtins.int"
2666 [builtins fixtures/tuple.pyi]
2667
2668 [case testNoGenericTypeVarTupleClassVarAccess]
2669 from typing import Generic, Tuple, TypeVarTuple, Unpack
2670
2671 Ts = TypeVarTuple("Ts")
2672 class C(Generic[Unpack[Ts]]):
2673 x: Tuple[Unpack[Ts]]
2674
2675 reveal_type(C.x) # E: Access to generic instance variables via class is ambiguous \
2676 # N: Revealed type is "builtins.tuple[Any, ...]"
2677
2678 class Bad(C[int, int]):
2679 pass
2680 reveal_type(Bad.x) # E: Access to generic instance variables via class is ambiguous \
2681 # N: Revealed type is "tuple[builtins.int, builtins.int]"
2682 reveal_type(Bad().x) # N: Revealed type is "tuple[builtins.int, builtins.int]"
2683
2684 class Good(C[int, int]):
2685 x = (1, 1)
2686 reveal_type(Good.x) # N: Revealed type is "tuple[builtins.int, builtins.int]"
2687 reveal_type(Good().x) # N: Revealed type is "tuple[builtins.int, builtins.int]"
2688 [builtins fixtures/tuple.pyi]
2689
2690 [case testConstraintsIncludeTupleFallback]
2691 from typing import Generic, TypeVar
2692 from typing_extensions import TypeVarTuple, Unpack
2693
2694 T = TypeVar("T")
2695 Ts = TypeVarTuple("Ts")
2696 _FT = TypeVar("_FT", bound=type)
2697
2698 def identity(smth: _FT) -> _FT:
2699 return smth
2700
2701 @identity
2702 class S(tuple[Unpack[Ts]], Generic[T, Unpack[Ts]]):
2703 def f(self, x: T, /) -> T: ...
2704 [builtins fixtures/tuple.pyi]
2705
2706 [case testNoCrashSubclassingTupleWithTrivialUnpack]
2707 # https://github.com/python/mypy/issues/19105
2708 from typing import Unpack
2709
2710 class A(tuple[Unpack[tuple[int]]]): ...
2711 class B(tuple[Unpack[tuple[()]]]): ...
2712
2713 a: A
2714 reveal_type(tuple(a)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
2715 (x,) = a
2716
2717 b: B
2718 (_,) = b # E: Need more than 0 values to unpack (1 expected)
2719 [builtins fixtures/tuple.pyi]
2720
2721 [case testNoCrashSubclassingTupleWithVariadicUnpack]
2722 # https://github.com/python/mypy/issues/19105
2723 from typing import Unpack
2724
2725 class A(tuple[Unpack[tuple[int, ...]]]): ...
2726
2727 a: A
2728 tuple(a)
2729 (x,) = a
2730 (_,) = a
2731 [builtins fixtures/tuple.pyi]
2732
2733 [case testNoCrashOnUndefinedUnpackInBase]
2734 from typing import TypeVarTuple, Generic, Unpack
2735
2736 Ts = TypeVarTuple("Ts")
2737
2738 class MyTuple(tuple[Unpack[TsWithTypo]], Generic[Unpack[Ts]]): # E: Name "TsWithTypo" is not defined
2739 ...
2740
2741 x: MyTuple[int, str]
2742 reveal_type(x[0]) # N: Revealed type is "Any"
2743 [builtins fixtures/tuple.pyi]
2744
2745 [case testNoCrashOnInvalidUnpackInBase]
2746 from typing import TypeVarTuple, Generic, Unpack, Union
2747
2748 Ts = TypeVarTuple("Ts")
2749
2750 class MyTuple(tuple[Unpack[Union[int, str]]], Generic[Unpack[Ts]]): # E: "int | str" cannot be unpacked (must be tuple or TypeVarTuple)
2751 ...
2752
2753 x: MyTuple[int, str]
2754 reveal_type(x[0]) # N: Revealed type is "Any"
2755 [builtins fixtures/tuple.pyi]
2756
2757 [case testHigherOrderFunctionUnpackTypeVarTupleViaParamSpec_no_verbose_reveal]
2758 from typing import Callable, ParamSpec, TypeVar, TypeVarTuple, Unpack
2759
2760 P = ParamSpec("P")
2761 T = TypeVar("T")
2762 Ts = TypeVarTuple("Ts")
2763
2764 def call(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
2765 return func(*args, **kwargs)
2766
2767
2768 def run(func: Callable[[Unpack[Ts]], T], *args: Unpack[Ts], some_kwarg: str = "asdf") -> T:
2769 raise
2770
2771
2772 def foo() -> str:
2773 return "hello"
2774
2775
2776 # this is a false positive, but it no longer crashes
2777 call(run, foo, some_kwarg="a") # E: Argument 1 to "call" has incompatible type "def [Ts, T] run(func: def (*args: *Ts) -> T, *args: *Ts, some_kwarg: str = ...) -> T"; expected "Callable[[Callable[[], str], str], str]"
2778 [builtins fixtures/tuple.pyi]
2779
2780 [case testTypeVarTuplePassedParameters]
2781 from typing import TypeVarTuple, Generic, Unpack
2782 from typing_extensions import Concatenate
2783
2784 Ts = TypeVarTuple("Ts")
2785
2786 class X(Generic[Unpack[Ts]]):
2787 ...
2788
2789 def c(t: X[Concatenate[int, ...]]) -> None: # E: Cannot use "[int, VarArg(Any), KwArg(Any)]" for TypeVarTuple, only for ParamSpec
2790 reveal_type(t) # N: Revealed type is "__main__.X[Unpack[builtins.tuple[Any, ...]]]"
2791 [builtins fixtures/tuple.pyi]
2792
2793 [case testTypeVarTupleLiteralValueAsTypeArgument]
2794 from typing import TypeVarTuple, Unpack, Callable, TypeVar
2795
2796 Ts = TypeVarTuple('Ts')
2797 T = TypeVar('T')
2798
2799 def func(d: Callable[[Unpack[Ts]], T]) -> T: ...
2800
2801 y = func[1, int] # E: Type application is only supported for generic classes \
2802 # E: Invalid type: try using Literal[1] instead?
2803 [builtins fixtures/tuple.pyi]
2804
2805 [case testTypeVarTupleUnpackWithoutArgsInProtocol]
2806 # https://github.com/python/mypy/issues/21467
2807 from typing import Protocol, Unpack
2808
2809 class C(Protocol[Unpack]): # E: Free type variable expected in Protocol[...]
2810 pass
2811 [builtins fixtures/tuple.pyi]
2812
2813 [case testGenericTupleSubclassConstructor]
2814 from typing import TypeVar, TypeVarTuple, Unpack
2815
2816 T = TypeVar("T")
2817 S = TypeVar("S")
2818 Ts = TypeVarTuple("Ts")
2819
2820 class S1(tuple[T, S]): ...
2821 class S2(tuple[Unpack[Ts]]): ...
2822
2823 reveal_type(S1((1, ""))) # N: Revealed type is "tuple[builtins.int, builtins.str, fallback=__main__.S1[builtins.int, builtins.str]]"
2824 reveal_type(S1((1, "", 2))) # N: Revealed type is "tuple[Never, Never, fallback=__main__.S1[Never, Never]]" \
2825 # E: Argument 1 to "S1" has incompatible type "tuple[int, str, int]"; expected "tuple[Never, Never]"
2826
2827 a1: S1[int, str] = S1((1, ""))
2828 b1: S1[int, str] = S1((1, 1)) # E: Argument 1 to "S1" has incompatible type "tuple[int, int]"; expected "tuple[int, str]"
2829 c1: S1[int, str] = S1((1, "", 2)) # E: Argument 1 to "S1" has incompatible type "tuple[int, str, int]"; expected "tuple[int, str]"
2830
2831 reveal_type(S2(())) # N: Revealed type is "tuple[(), fallback=__main__.S2[()]]"
2832 reveal_type(S2((1, ""))) # N: Revealed type is "tuple[Literal[1]?, Literal['']?, fallback=__main__.S2[Literal[1]?, Literal['']?]]"
2833 reveal_type(S2((1, "", 2))) # N: Revealed type is "tuple[Literal[1]?, Literal['']?, Literal[2]?, fallback=__main__.S2[Literal[1]?, Literal['']?, Literal[2]?]]"
2834
2835 a2: S2[int, str] = S2((1, ""))
2836 b2: S2[int, str] = S2((1, 1)) # E: Argument 1 to "S2" has incompatible type "tuple[int, int]"; expected "tuple[int, str]"
2837 c2: S2[int, str] = S2((1, "", 2)) # E: Argument 1 to "S2" has incompatible type "tuple[int, str, int]"; expected "tuple[int, str]"
2838
2839 t: S2[Unpack[tuple[int, ...]]] = S2((1, 2))
2840 t_bad: S2[Unpack[tuple[int, ...]]] = S2((1, "")) # E: Argument 1 to "S2" has incompatible type "tuple[int, str]"; expected "tuple[int, ...]"
2841 [builtins fixtures/tuple.pyi]
2842
2843 [case testVariadicTupleSubclassEmptyIndex]
2844 from typing import TypeVarTuple, Unpack
2845
2846 Ts = TypeVarTuple("Ts")
2847 class Shape(tuple[Unpack[Ts]]): ...
2848
2849 class Shape0(Shape[()]): ...
2850
2851 x: Shape[()]
2852 y: Shape0
2853
2854 reveal_type(x) # N: Revealed type is "tuple[(), fallback=__main__.Shape[()]]"
2855 reveal_type(y) # N: Revealed type is "tuple[(), fallback=__main__.Shape0]"
2856
2857 def test(t: tuple[int, ...]) -> None: ...
2858 test(x)
2859 test(y)
2860 [builtins fixtures/tuple.pyi]
2861
2862 [case testVariadicTupleSubclassVariadicIndex]
2863 from typing import TypeVarTuple, Unpack
2864
2865 Ts = TypeVarTuple("Ts")
2866 class Shape(tuple[Unpack[Ts]]): ...
2867
2868 class ShapeN(Shape[Unpack[tuple[int, ...]]]): ...
2869
2870 x: Shape[Unpack[tuple[int, ...]]]
2871 y: ShapeN
2872
2873 def test(t: tuple[int, ...]) -> None: ...
2874 test(x)
2875 test(y)
2876 [builtins fixtures/tuple.pyi]
2877
2878 [case testVariadicGenericClassEmptyTupleUnpackIndex]
2879 from typing import Generic, TypeVarTuple, Unpack
2880
2881 Ts = TypeVarTuple("Ts")
2882 class Blah(Generic[Unpack[Ts]]):
2883 pass
2884
2885 x: Blah[()]
2886 y: Blah[Unpack[tuple[()]]]
2887
2888 Empty = tuple[()]
2889 z: Blah[Unpack[Empty]]
2890
2891 reveal_type(x) # N: Revealed type is "__main__.Blah[()]"
2892 reveal_type(y) # N: Revealed type is "__main__.Blah[()]"
2893 reveal_type(z) # N: Revealed type is "__main__.Blah[()]"
2894 [builtins fixtures/tuple.pyi]
2895
2896 [case testVariadicGenericAliasEmptyTupleUnpackIndex]
2897 from typing import Generic, TypeVarTuple, Unpack
2898
2899 Ts = TypeVarTuple("Ts")
2900 Blah = list[tuple[Unpack[Ts]]]
2901
2902 x: Blah[()]
2903 y: Blah[Unpack[tuple[()]]]
2904
2905 Empty = tuple[()]
2906 z: Blah[Unpack[Empty]]
2907
2908 reveal_type(x) # N: Revealed type is "builtins.list[tuple[()]]"
2909 reveal_type(y) # N: Revealed type is "builtins.list[tuple[()]]"
2910 reveal_type(z) # N: Revealed type is "builtins.list[tuple[()]]"
2911 [builtins fixtures/tuple.pyi]
2912
2913 [case testMeetTupleWithUnpackVsInstanceWorks_no_verbose_reveal]
2914 from typing import TypeVar, Unpack, Union
2915
2916 K = TypeVar("K")
2917 Prefix = tuple[Union[K, list[int]], ...]
2918
2919 def g(x: K) -> None:
2920 d: Union[Prefix[K], tuple[Unpack[Prefix[int]], int], None] = None
2921 assert d is not None
2922 reveal_type(d) # N: Revealed type is "tuple[K | list[int], ...] | tuple[*tuple[int | list[int], ...], int]"
2923 [builtins fixtures/tuple.pyi]