Debian Codegraph @

test-data/unit/check-tuples.test

raw
1 -- Normal assignment and subtyping
2 -- -------------------------------
3
4
5 [case testTupleAssignmentWithTupleTypes]
6 from typing import Tuple
7 t1: Tuple[A]
8 t2: Tuple[B]
9 t3: Tuple[A, A]
10 t4: Tuple[A, B]
11 t5: Tuple[B, A]
12
13 if int():
14 t1 = t2 # E: Incompatible types in assignment (expression has type "tuple[B]", variable has type "tuple[A]")
15 if int():
16 t1 = t3 # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "tuple[A]")
17 if int():
18 t3 = t1 # E: Incompatible types in assignment (expression has type "tuple[A]", variable has type "tuple[A, A]")
19 if int():
20 t3 = t4 # E: Incompatible types in assignment (expression has type "tuple[A, B]", variable has type "tuple[A, A]")
21 if int():
22 t3 = t5 # E: Incompatible types in assignment (expression has type "tuple[B, A]", variable has type "tuple[A, A]")
23
24 # Ok
25 if int():
26 t1 = t1
27 if int():
28 t2 = t2
29 if int():
30 t3 = t3
31 if int():
32 t4 = t4
33 if int():
34 t5 = t5
35
36 class A: pass
37 class B: pass
38 [builtins fixtures/tuple.pyi]
39
40 [case testTupleSubtyping]
41 from typing import Tuple
42 t1: Tuple[A, A]
43 t2: Tuple[A, B]
44 t3: Tuple[B, A]
45
46 if int():
47 t2 = t1 # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "tuple[A, B]")
48 t2 = t3 # E: Incompatible types in assignment (expression has type "tuple[B, A]", variable has type "tuple[A, B]")
49 t3 = t1 # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "tuple[B, A]")
50 t3 = t2 # E: Incompatible types in assignment (expression has type "tuple[A, B]", variable has type "tuple[B, A]")
51
52 t1 = t2
53 t1 = t3
54
55 class A: pass
56 class B(A): pass
57 [builtins fixtures/tuple.pyi]
58
59 [case testTupleCompatibilityWithOtherTypes]
60 # flags: --no-strict-optional
61 from typing import Tuple
62 a, o = None, None # type: (A, object)
63 t = None # type: Tuple[A, A]
64
65 if int():
66 a = t # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "A")
67 if int():
68 t = o # E: Incompatible types in assignment (expression has type "object", variable has type "tuple[A, A]")
69 if int():
70 t = a # E: Incompatible types in assignment (expression has type "A", variable has type "tuple[A, A]")
71 # TODO: callable types + tuples
72
73 # Ok
74 if int():
75 o = t
76 if int():
77 t = None
78
79 class A: pass
80 [builtins fixtures/tuple.pyi]
81
82 [case testNestedTupleTypes]
83 from typing import Tuple
84 t1: Tuple[A, Tuple[A, A]]
85 t2: Tuple[B, Tuple[B, B]]
86
87 if int():
88 t2 = t1 # E: Incompatible types in assignment (expression has type "tuple[A, tuple[A, A]]", variable has type "tuple[B, tuple[B, B]]")
89 if int():
90 t1 = t2
91
92 class A: pass
93 class B(A): pass
94 [builtins fixtures/tuple.pyi]
95
96 [case testNestedTupleTypes2]
97 from typing import Tuple
98 t1: Tuple[A, Tuple[A, A]]
99 t2: Tuple[B, Tuple[B, B]]
100
101 if int():
102 t2 = t1 # E: Incompatible types in assignment (expression has type "tuple[A, tuple[A, A]]", variable has type "tuple[B, tuple[B, B]]")
103 if int():
104 t1 = t2
105
106 class A: pass
107 class B(A): pass
108 [builtins fixtures/tuple.pyi]
109
110 [case testSubtypingWithTupleType]
111 from __future__ import annotations
112 from typing import Any, Tuple
113
114 tuple_aa: tuple[A, A]
115 Tuple_aa: Tuple[A, A]
116
117 tuple_obj: tuple[object, ...]
118 Tuple_obj: Tuple[object, ...]
119
120 tuple_obj_one: tuple[object]
121 Tuple_obj_one: Tuple[object]
122
123 tuple_obj_two: tuple[object, object]
124 Tuple_obj_two: Tuple[object, object]
125
126 tuple_any_implicit: tuple
127 Tuple_any_implicit: Tuple
128
129 tuple_any: tuple[Any, ...]
130 Tuple_any: Tuple[Any, ...]
131
132 tuple_any_one: tuple[Any]
133 Tuple_any_one: Tuple[Any]
134
135 tuple_any_two: tuple[Any, Any]
136 Tuple_any_two: Tuple[Any, Any]
137
138 def takes_tuple_aa(t: tuple[A, A]): ...
139
140 takes_tuple_aa(tuple_aa)
141 takes_tuple_aa(Tuple_aa)
142 takes_tuple_aa(tuple_obj) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[object, ...]"; expected "tuple[A, A]"
143 takes_tuple_aa(Tuple_obj) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[object, ...]"; expected "tuple[A, A]"
144 takes_tuple_aa(tuple_obj_one) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[object]"; expected "tuple[A, A]"
145 takes_tuple_aa(Tuple_obj_one) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[object]"; expected "tuple[A, A]"
146 takes_tuple_aa(tuple_obj_two) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[object, object]"; expected "tuple[A, A]"
147 takes_tuple_aa(Tuple_obj_two) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[object, object]"; expected "tuple[A, A]"
148 takes_tuple_aa(tuple_any_implicit)
149 takes_tuple_aa(Tuple_any_implicit)
150 takes_tuple_aa(tuple_any)
151 takes_tuple_aa(Tuple_any)
152 takes_tuple_aa(tuple_any_one) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[Any]"; expected "tuple[A, A]"
153 takes_tuple_aa(Tuple_any_one) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[Any]"; expected "tuple[A, A]"
154 takes_tuple_aa(tuple_any_two)
155 takes_tuple_aa(Tuple_any_two)
156
157 def takes_tuple_any_implicit(t: tuple): ...
158
159 takes_tuple_any_implicit(tuple_aa)
160 takes_tuple_any_implicit(Tuple_aa)
161 takes_tuple_any_implicit(tuple_obj)
162 takes_tuple_any_implicit(Tuple_obj)
163 takes_tuple_any_implicit(tuple_obj_one)
164 takes_tuple_any_implicit(Tuple_obj_one)
165 takes_tuple_any_implicit(tuple_obj_two)
166 takes_tuple_any_implicit(Tuple_obj_two)
167 takes_tuple_any_implicit(tuple_any_implicit)
168 takes_tuple_any_implicit(Tuple_any_implicit)
169 takes_tuple_any_implicit(tuple_any)
170 takes_tuple_any_implicit(Tuple_any)
171 takes_tuple_any_implicit(tuple_any_one)
172 takes_tuple_any_implicit(Tuple_any_one)
173 takes_tuple_any_implicit(tuple_any_two)
174 takes_tuple_any_implicit(Tuple_any_two)
175
176 def takes_tuple_any_one(t: tuple[Any]): ...
177
178 takes_tuple_any_one(tuple_aa) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[A, A]"; expected "tuple[Any]"
179 takes_tuple_any_one(Tuple_aa) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[A, A]"; expected "tuple[Any]"
180 takes_tuple_any_one(tuple_obj) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[object, ...]"; expected "tuple[Any]"
181 takes_tuple_any_one(Tuple_obj) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[object, ...]"; expected "tuple[Any]"
182 takes_tuple_any_one(tuple_obj_one)
183 takes_tuple_any_one(Tuple_obj_one)
184 takes_tuple_any_one(tuple_obj_two) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[object, object]"; expected "tuple[Any]"
185 takes_tuple_any_one(Tuple_obj_two) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[object, object]"; expected "tuple[Any]"
186 takes_tuple_any_one(tuple_any_implicit)
187 takes_tuple_any_one(Tuple_any_implicit)
188 takes_tuple_any_one(tuple_any)
189 takes_tuple_any_one(Tuple_any)
190 takes_tuple_any_one(tuple_any_one)
191 takes_tuple_any_one(Tuple_any_one)
192 takes_tuple_any_one(tuple_any_two) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[Any, Any]"; expected "tuple[Any]"
193 takes_tuple_any_one(Tuple_any_two) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[Any, Any]"; expected "tuple[Any]"
194
195 class A: pass
196 [builtins fixtures/tuple.pyi]
197
198 [case testSubtypingWithTupleTypeSubclass]
199 from __future__ import annotations
200 from typing import Any, Tuple
201
202 class A: ...
203
204 inst_tuple_aa: Tuple[A, A]
205
206 class tuple_aa_subclass(Tuple[A, A]): ...
207 inst_tuple_aa_subclass: tuple_aa_subclass
208
209 class tuple_any_subclass(Tuple[Any, ...]): ...
210 inst_tuple_any_subclass: tuple_any_subclass
211
212 class tuple_any_one_subclass(Tuple[Any]): ...
213 inst_tuple_any_one_subclass: tuple_any_one_subclass
214
215 class tuple_any_two_subclass(Tuple[Any, Any]): ...
216 inst_tuple_any_two_subclass: tuple_any_two_subclass
217
218 class tuple_obj_subclass(Tuple[object, ...]): ...
219 inst_tuple_obj_subclass: tuple_obj_subclass
220
221 class tuple_obj_one_subclass(Tuple[object]): ...
222 inst_tuple_obj_one_subclass: tuple_obj_one_subclass
223
224 class tuple_obj_two_subclass(Tuple[object, object]): ...
225 inst_tuple_obj_two_subclass: tuple_obj_two_subclass
226
227 def takes_tuple_aa(t: Tuple[A, A]): ...
228
229 takes_tuple_aa(inst_tuple_aa)
230 takes_tuple_aa(inst_tuple_aa_subclass)
231 takes_tuple_aa(inst_tuple_any_subclass)
232 takes_tuple_aa(inst_tuple_any_one_subclass) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple_any_one_subclass"; expected "tuple[A, A]"
233 takes_tuple_aa(inst_tuple_any_two_subclass)
234 takes_tuple_aa(inst_tuple_obj_subclass) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple_obj_subclass"; expected "tuple[A, A]"
235 takes_tuple_aa(inst_tuple_obj_one_subclass) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple_obj_one_subclass"; expected "tuple[A, A]"
236 takes_tuple_aa(inst_tuple_obj_two_subclass) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple_obj_two_subclass"; expected "tuple[A, A]"
237
238 def takes_tuple_aa_subclass(t: tuple_aa_subclass): ...
239
240 takes_tuple_aa_subclass(inst_tuple_aa) # E: Argument 1 to "takes_tuple_aa_subclass" has incompatible type "tuple[A, A]"; expected "tuple_aa_subclass"
241 takes_tuple_aa_subclass(inst_tuple_aa_subclass)
242 takes_tuple_aa_subclass(inst_tuple_any_subclass) # E: Argument 1 to "takes_tuple_aa_subclass" has incompatible type "tuple_any_subclass"; expected "tuple_aa_subclass"
243 takes_tuple_aa_subclass(inst_tuple_any_one_subclass) # E: Argument 1 to "takes_tuple_aa_subclass" has incompatible type "tuple_any_one_subclass"; expected "tuple_aa_subclass"
244 takes_tuple_aa_subclass(inst_tuple_any_two_subclass) # E: Argument 1 to "takes_tuple_aa_subclass" has incompatible type "tuple_any_two_subclass"; expected "tuple_aa_subclass"
245 takes_tuple_aa_subclass(inst_tuple_obj_subclass) # E: Argument 1 to "takes_tuple_aa_subclass" has incompatible type "tuple_obj_subclass"; expected "tuple_aa_subclass"
246 takes_tuple_aa_subclass(inst_tuple_obj_one_subclass) # E: Argument 1 to "takes_tuple_aa_subclass" has incompatible type "tuple_obj_one_subclass"; expected "tuple_aa_subclass"
247 takes_tuple_aa_subclass(inst_tuple_obj_two_subclass) # E: Argument 1 to "takes_tuple_aa_subclass" has incompatible type "tuple_obj_two_subclass"; expected "tuple_aa_subclass"
248
249 [builtins fixtures/tuple.pyi]
250
251 [case testTupleInitializationWithNone]
252 # flags: --no-strict-optional
253 from typing import Tuple
254 t = None # type: Tuple[A, A]
255 t = None
256 class A: pass
257 [builtins fixtures/tuple.pyi]
258
259
260 -- Tuple expressions
261 -- -----------------
262
263
264 [case testTupleExpressions]
265 # flags: --no-strict-optional
266 from typing import Tuple
267 t1 = None # type: tuple
268 t2 = None # type: Tuple[A]
269 t3 = None # type: Tuple[A, B]
270
271 a, b, c = None, None, None # type: (A, B, C)
272
273 if int():
274 t2 = () # E: Incompatible types in assignment (expression has type "tuple[()]", variable has type "tuple[A]")
275 if int():
276 t2 = (a, a) # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "tuple[A]")
277 if int():
278 t3 = (a, a) # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "tuple[A, B]")
279 if int():
280 t3 = (b, b) # E: Incompatible types in assignment (expression has type "tuple[B, B]", variable has type "tuple[A, B]")
281 if int():
282 t3 = (a, b, a) # E: Incompatible types in assignment (expression has type "tuple[A, B, A]", variable has type "tuple[A, B]")
283
284 t1 = ()
285 t1 = (a,)
286 t2 = (a,)
287 t3 = (a, b)
288 t3 = (a, c)
289 t3 = (None, None)
290
291 class A: pass
292 class B: pass
293 class C(B): pass
294 [builtins fixtures/tuple.pyi]
295
296 [case testVoidValueInTuple]
297 import typing
298 def f() -> None: pass
299
300 (None, f()) # E: "f" does not return a value (it only ever returns None)
301 (f(), None) # E: "f" does not return a value (it only ever returns None)
302 [builtins fixtures/tuple.pyi]
303
304
305 -- Indexing
306 -- --------
307
308
309 [case testIndexingTuples]
310 from typing import Tuple
311 t1: Tuple[A, B]
312 t2: Tuple[A]
313 t3: Tuple[A, B, C, D, E]
314 a: A
315 b: B
316 x: Tuple[A, B, C]
317 y: Tuple[A, C, E]
318 n = 0
319
320 if int():
321 a = t1[1] # E: Incompatible types in assignment (expression has type "B", variable has type "A")
322 if int():
323 b = t1[0] # E: Incompatible types in assignment (expression has type "A", variable has type "B")
324 t1[2] # E: Tuple index out of range
325 t1[3] # E: Tuple index out of range
326 t2[1] # E: Tuple index out of range
327 reveal_type(t1[n]) # N: Revealed type is "__main__.A | __main__.B"
328 reveal_type(t3[n:]) # N: Revealed type is "builtins.tuple[__main__.A | __main__.B | __main__.C | __main__.D | __main__.E, ...]"
329 if int():
330 b = t1[(0)] # E: Incompatible types in assignment (expression has type "A", variable has type "B")
331
332 if int():
333 a = t1[0]
334 if int():
335 b = t1[1]
336 if int():
337 b = t1[-1]
338 if int():
339 a = t1[(0)]
340 if int():
341 b = t1[+1]
342 if int():
343 x = t3[0:3] # type (A, B, C)
344 if int():
345 y = t3[0:+5:2] # type (A, C, E)
346 if int():
347 x = t3[:-2] # type (A, B, C)
348
349 class A: pass
350 class B: pass
351 class C: pass
352 class D: pass
353 class E: pass
354 [builtins fixtures/tuple.pyi]
355
356 [case testIndexingTuplesWithNegativeIntegers]
357 from typing import Tuple
358 t1: Tuple[A, B]
359 t2: Tuple[A]
360 a: A
361 b: B
362
363 if int():
364 a = t1[-1] # E: Incompatible types in assignment (expression has type "B", variable has type "A")
365 if int():
366 b = t1[-2] # E: Incompatible types in assignment (expression has type "A", variable has type "B")
367 t1[-3] # E: Tuple index out of range
368 t1[-4] # E: Tuple index out of range
369 if int():
370 b = t2[(-1)] # E: Incompatible types in assignment (expression has type "A", variable has type "B")
371
372 if int():
373 a = t1[-2]
374 if int():
375 b = t1[-1]
376 if int():
377 a = t2[(-1)]
378
379 class A: pass
380 class B: pass
381 [builtins fixtures/tuple.pyi]
382
383 [case testAssigningToTupleItems]
384 from typing import Tuple
385
386 class A: pass
387 class B: pass
388
389 t: Tuple[A, B]
390 n = 0
391
392 t[0] = A() # E: Unsupported target for indexed assignment ("tuple[A, B]")
393 t[2] = A() # E: Unsupported target for indexed assignment ("tuple[A, B]")
394 t[n] = A() # E: Unsupported target for indexed assignment ("tuple[A, B]")
395 [builtins fixtures/tuple.pyi]
396
397
398 -- Multiple assignment
399 -- -------------------
400
401
402 [case testMultipleAssignmentWithTuples]
403 # flags: --no-strict-optional
404 from typing import Tuple
405 t1 = None # type: Tuple[A, B]
406 t2 = None # type: Tuple[A, B, A]
407 a, b = None, None # type: (A, B)
408 (a1, b1) = None, None # type: Tuple[A, B]
409
410 reveal_type(a1) # N: Revealed type is "__main__.A"
411 reveal_type(b1) # N: Revealed type is "__main__.B"
412
413 if int():
414 a, a = t1 # E: Incompatible types in assignment (expression has type "B", variable has type "A")
415 if int():
416 b, b = t1 # E: Incompatible types in assignment (expression has type "A", variable has type "B")
417 if int():
418 a, b, b = t2 # E: Incompatible types in assignment (expression has type "A", variable has type "B")
419
420 if int():
421 a, b = t1
422 if int():
423 a, b, a1 = t2
424
425 class A: pass
426 class B: pass
427 [builtins fixtures/tuple.pyi]
428
429 [case testMultipleAssignmentWithOverloadReinferredAsHomogeneousTuple]
430 from typing import Any, TypeVar, overload
431
432 T = TypeVar("T")
433
434 @overload
435 def f(value: T) -> tuple[T, int]: ...
436 @overload
437 def f(*values: object) -> tuple[Any, ...]: ...
438 def f(*values: object, **kwargs: object) -> object: ...
439
440 def g() -> None:
441 first: str
442 first, second = f(1) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
443 reveal_type(second) # N: Revealed type is "builtins.int"
444
445 last: str
446 _, last = f(1) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
447 [builtins fixtures/tuple.pyi]
448
449 [case testMultipleAssignmentWithOverloadReinferredAsNonTuple]
450 from typing import TypeVar, overload
451
452 T = TypeVar("T")
453
454 @overload
455 def f(value: T) -> tuple[T]: ...
456 @overload
457 def f(*values: object) -> int: ...
458 def f(*values: object, **kwargs: object) -> object: ...
459
460 def g() -> None:
461 value: str
462 (value,) = f(1) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
463 [builtins fixtures/tuple.pyi]
464
465 [case testMultipleAssignmentWithSquareBracketTuples]
466 # flags: --no-strict-optional
467 from typing import Tuple
468
469 def avoid_confusing_test_parser() -> None:
470 t1 = None # type: Tuple[A, B]
471 t2 = None # type: Tuple[A, B, A]
472 [a, b] = None, None # type: (A, B)
473 [a1, b1] = None, None # type: Tuple[A, B]
474
475 reveal_type(a) # N: Revealed type is "__main__.A"
476 reveal_type(b) # N: Revealed type is "__main__.B"
477 reveal_type(a1) # N: Revealed type is "__main__.A"
478 reveal_type(b1) # N: Revealed type is "__main__.B"
479
480 if int():
481 [a, a] = t1 # E: Incompatible types in assignment (expression has type "B", variable has type "A")
482 [b, b] = t1 # E: Incompatible types in assignment (expression has type "A", variable has type "B")
483 [a, b, b] = t2 # E: Incompatible types in assignment (expression has type "A", variable has type "B")
484
485 [a, b] = t1
486 [a, b, a1] = t2
487
488 [a2, b2] = t1
489 reveal_type(a2) # N: Revealed type is "__main__.A"
490 reveal_type(b2) # N: Revealed type is "__main__.B"
491
492 class A: pass
493 class B: pass
494 [builtins fixtures/tuple.pyi]
495
496 [case testMultipleAssignmentWithInvalidNumberOfValues]
497 from typing import Tuple
498 t1: Tuple[A, A, A]
499 a: A
500
501 a, a = t1 # E: Too many values to unpack (2 expected, 3 provided)
502 a, a, a, a = t1 # E: Need more than 3 values to unpack (4 expected)
503
504 a, a, a = t1
505
506 class A: pass
507 [builtins fixtures/tuple.pyi]
508
509 [case testMultipleAssignmentWithTupleExpressionRvalue]
510 a: A
511 b: B
512
513 if int():
514 a, b = a, a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
515 if int():
516 a, b = b, a \
517 # E: Incompatible types in assignment (expression has type "B", variable has type "A") \
518 # E: Incompatible types in assignment (expression has type "A", variable has type "B")
519
520 if int():
521 a, b = a, b
522 if int():
523 a, a = a, a
524
525 class A: pass
526 class B: pass
527 [builtins fixtures/tuple.pyi]
528
529 [case testSubtypingInMultipleAssignment]
530 a: A
531 b: B
532
533 if int():
534 b, b = a, b # E: Incompatible types in assignment (expression has type "A", variable has type "B")
535 if int():
536 b, b = b, a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
537
538 if int():
539 a, b = b, b
540 if int():
541 b, a = b, b
542
543 class A: pass
544 class B(A): pass
545 [builtins fixtures/tuple.pyi]
546
547 [case testInitializationWithMultipleValues]
548 # flags: --no-strict-optional
549 a, b = None, None # type: (A, B)
550
551 a1, b1 = a, a # type: (A, B) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
552 a2, b2 = b, b # type: (A, B) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
553 a3, b3 = a # type: (A, B) # E: "A" object is not iterable
554 a4, b4 = None # type: (A, B) # E: "None" has no attribute "__iter__" (not iterable)
555 a5, b5 = a, b, a # type: (A, B) # E: Too many values to unpack (2 expected, 3 provided)
556
557 ax, bx = a, b # type: (A, B)
558
559 class A: pass
560 class B: pass
561 [builtins fixtures/tuple.pyi]
562
563 [case testMultipleAssignmentWithNonTupleRvalue]
564 a: A
565 b: B
566 def f(): pass
567
568 a, b = None # E: "None" object is not iterable
569 a, b = a # E: "A" object is not iterable
570 a, b = f # E: "Callable[[], Any]" object is not iterable
571
572 class A: pass
573 class B: pass
574 [builtins fixtures/tuple.pyi]
575
576 [case testMultipleAssignmentWithIndexedLvalues]
577 a: A
578 b: B
579 aa: AA
580 bb: BB
581
582 a[a], b[b] = a, bb # E: Incompatible types in assignment (expression has type "A", target has type "AA")
583 a[a], b[b] = aa, b # E: Incompatible types in assignment (expression has type "B", target has type "BB")
584 a[aa], b[b] = aa, bb # E: Invalid index type "AA" for "A"; expected type "A"
585 a[a], b[bb] = aa, bb # E: Invalid index type "BB" for "B"; expected type "B"
586 a[a], b[b] = aa, bb
587
588 class A:
589 def __setitem__(self, x: 'A', y: 'AA') -> None: pass
590 class B:
591 def __setitem__(self, x: 'B', y: 'BB') -> None: pass
592
593 class AA: pass
594 class BB: pass
595 [builtins fixtures/tuple.pyi]
596
597 [case testMultipleDeclarationWithParentheses]
598 # flags: --no-strict-optional
599 (a, b) = (None, None) # type: int, str
600 if int():
601 a = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
602 b = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
603 if int():
604 a = 1
605 b = ''
606 [builtins fixtures/tuple.pyi]
607
608 [case testMultipleAssignmentWithExtraParentheses]
609 a: A
610 b: B
611
612 if int():
613 (a, b) = (a, a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
614 if int():
615 (a, b) = (b, b) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
616 if int():
617 ((a), (b)) = ((a), (a)) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
618 if int():
619 ((a), (b)) = ((b), (b)) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
620 if int():
621 [a, b] = a, a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
622 if int():
623 [a, b] = b, b # E: Incompatible types in assignment (expression has type "B", variable has type "A")
624
625 if int():
626 (a, b) = (a, b)
627 if int():
628 ((a), (b)) = ((a), (b))
629 if int():
630 [a, b] = a, b
631
632 class A: pass
633 class B: pass
634 [builtins fixtures/tuple.pyi]
635
636 [case testMultipleAssignmentUsingSingleTupleType]
637 # flags: --no-strict-optional
638 from typing import Tuple
639 a, b = None, None # type: Tuple[int, str]
640 if int():
641 a = 1
642 if int():
643 b = ''
644 if int():
645 a = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
646 if int():
647 b = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
648 [builtins fixtures/tuple.pyi]
649
650 [case testMultipleAssignmentWithMixedVariables]
651 a = b, c = 1, 1
652 x, y = p, q = 1, 1
653 u, v, w = r, s = 1, 1 # E: Need more than 2 values to unpack (3 expected)
654 d, e = f, g, h = 1, 1 # E: Need more than 2 values to unpack (3 expected)
655 [builtins fixtures/tuple.pyi]
656
657 [case testUnpackAssignmentWithStarExpr]
658 a: A
659 b: list[B]
660 if int():
661 (a,) = (*b,) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
662
663 class A: pass
664 class B: pass
665
666
667 -- Assignment to starred expressions
668 -- ---------------------------------
669
670
671 [case testAssignmentToStarMissingAnnotation]
672 from typing import List
673 t = 1, 2
674 a, b, *c = 1, 2 # E: Need type annotation for "c" (hint: "c: list[<type>] = ...")
675 aa, bb, *cc = t # E: Need type annotation for "cc" (hint: "cc: list[<type>] = ...")
676 [builtins fixtures/list.pyi]
677
678 [case testAssignmentToStarAnnotation]
679 # flags: --no-strict-optional
680 from typing import List
681 li, lo = None, None # type: List[int], List[object]
682 a, b, *c = 1, 2 # type: int, int, List[int]
683 if int():
684 c = lo # E: Incompatible types in assignment (expression has type "list[object]", variable has type "list[int]")
685 if int():
686 c = li
687 [builtins fixtures/list.pyi]
688
689 [case testAssignmentToStarCount1]
690 from typing import List
691 ca: List[int]
692 c = [1]
693 if int():
694 a, b, *c = 1, # E: Need more than 1 value to unpack (2 expected)
695 if int():
696 a, b, *c = 1, 2
697 if int():
698 a, b, *c = 1, 2, 3
699 if int():
700 a, b, *c = 1, 2, 3, 4
701 [builtins fixtures/list.pyi]
702
703 [case testAssignmentToStarCount2]
704 from typing import List
705 ca: List[int]
706 t1 = 1,
707 t2 = 1, 2
708 t3 = 1, 2, 3
709 t4 = 1, 2, 3, 4
710 c = [1]
711 if int():
712 a, b, *c = t1 # E: Need more than 1 value to unpack (2 expected)
713 if int():
714 a, b, *c = t2
715 if int():
716 a, b, *c = t3
717 if int():
718 a, b, *c = t4
719 [builtins fixtures/list.pyi]
720
721 [case testAssignmentToStarFromAny]
722 from typing import Any, cast
723 class C: pass
724
725 a, c = cast(Any, 1), C()
726 p, *q = a
727 c = a
728 c = q
729 [case testAssignmentToComplexStar]
730 from typing import List
731 li: List[int]
732 if int():
733 a, *(li) = 1,
734 a, *(b, c) = 1, 2 # E: Need more than 1 value to unpack (2 expected)
735 if int():
736 a, *(b, c) = 1, 2, 3
737 if int():
738 a, *(b, c) = 1, 2, 3, 4 # E: Too many values to unpack (2 expected, 3 provided)
739 [builtins fixtures/list.pyi]
740
741 [case testAssignmentToStarFromTupleType]
742 from typing import List, Tuple
743 li: List[int]
744 la: List[A]
745 ta: Tuple[A, A, A]
746 if int():
747 a, *la = ta
748 if int():
749 a, *li = ta # E: List item 0 has incompatible type "A"; expected "int" \
750 # E: List item 1 has incompatible type "A"; expected "int"
751 if int():
752 a, *na = ta
753 if int():
754 na = la
755 na = a # E: Incompatible types in assignment (expression has type "A", variable has type "list[A]")
756
757 class A: pass
758 [builtins fixtures/list.pyi]
759
760 [case testAssignmentToStarFromTupleInference]
761 from typing import List
762 class A: pass
763 li: List[int]
764 la: List[A]
765 a, *l = A(), A()
766 if int():
767 l = li # E: Incompatible types in assignment (expression has type "list[int]", variable has type "list[A]")
768 if int():
769 l = la
770 [builtins fixtures/list.pyi]
771 [out]
772
773 [case testAssignmentToStarFromListInference]
774 from typing import List
775
776 class A: pass
777
778 li: List[int]
779 la: List[A]
780 a, *l = [A(), A()]
781 if int():
782 l = li # E: Incompatible types in assignment (expression has type "list[int]", variable has type "list[A]")
783 if int():
784 l = la
785 [builtins fixtures/list.pyi]
786 [out]
787
788 [case testAssignmentToStarFromTupleTypeInference]
789 from typing import List, Tuple
790 li: List[int]
791 la: List[A]
792 ta: Tuple[A, A, A]
793 a, *l = ta
794 if int():
795 l = li # E: Incompatible types in assignment (expression has type "list[int]", variable has type "list[A]")
796 if int():
797 l = la
798
799 class A: pass
800 [builtins fixtures/list.pyi]
801 [out]
802
803 [case testAssignmentToStarFromListTypeInference]
804 from typing import List
805 li: List[int]
806 la: List[A]
807 a, *l = la
808 if int():
809 l = li # E: Incompatible types in assignment (expression has type "list[int]", variable has type "list[A]")
810 if int():
811 l = la
812
813 class A: pass
814 [builtins fixtures/list.pyi]
815 [out]
816
817 [case testAssignmentToStarFromIterable]
818 from typing import List, Tuple, Iterable
819
820 class CustomIterable(Iterable[int]): pass
821
822 a: List[int]
823 b: Tuple[int, ...]
824 c: Tuple[int, int, int]
825 d: Iterable[int]
826 e: CustomIterable
827
828 a1, *a2 = a
829 b1, *b2 = b
830 c1, *c2 = c
831 d1, *d2 = d
832 e1, *e2 = e
833
834 reveal_type(a2) # N: Revealed type is "builtins.list[builtins.int]"
835 reveal_type(b2) # N: Revealed type is "builtins.list[builtins.int]"
836 reveal_type(c2) # N: Revealed type is "builtins.list[builtins.int]"
837 reveal_type(d2) # N: Revealed type is "builtins.list[builtins.int]"
838 reveal_type(e2) # N: Revealed type is "builtins.list[builtins.int]"
839 [builtins fixtures/tuple.pyi]
840
841 -- Nested tuple assignment
842 -- ----------------------------
843
844
845 [case testNestedTupleAssignment1]
846 a1: A
847 a2: A
848 b1: B
849 b2: B
850 c1: C
851 c2: C
852
853 if int():
854 a1, (b1, c1) = a2, (b2, c2)
855 if int():
856 a1, (a1, (b1, c1)) = a2, (a2, (b2, c2))
857 if int():
858 a1, (a1, (a1, b1)) = a1, (a1, (a1, c1)) # E: Incompatible types in assignment (expression has type "C", variable has type "B")
859
860 class A: pass
861 class B: pass
862 class C: pass
863 [builtins fixtures/tuple.pyi]
864
865 [case testNestedTupleAssignment2]
866 a1: A
867 a2: A
868 b1: B
869 b2: B
870 c1: C
871 c2: C
872 t = a1, b1
873
874 if int():
875 a2, b2 = t
876 if int():
877 (a2, b2), c2 = t, c1
878 if int():
879 (a2, c2), c2 = t, c1 # E: Incompatible types in assignment (expression has type "B", variable has type "C")
880 if int():
881 t, c2 = (a2, b2), c2
882 if int():
883 t, c2 = (a2, a2), c2 # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "tuple[A, B]")
884 if int():
885 t = a1, a1, a1 # E: Incompatible types in assignment (expression has type "tuple[A, A, A]", variable has type "tuple[A, B]")
886 if int():
887 t = a1 # E: Incompatible types in assignment (expression has type "A", variable has type "tuple[A, B]")
888 if int():
889 a2, a2, a2 = t # E: Need more than 2 values to unpack (3 expected)
890 if int():
891 a2, = t # E: Too many values to unpack (1 expected, 2 provided)
892 if int():
893 a2 = t # E: Incompatible types in assignment (expression has type "tuple[A, B]", variable has type "A")
894
895 class A: pass
896 class B: pass
897 class C: pass
898 [builtins fixtures/tuple.pyi]
899
900
901 -- Error messages
902 -- --------------
903
904
905 [case testTupleErrorMessages]
906 class A:
907 def __add__(self, x: 'A') -> 'A': pass
908 def f(x: 'A') -> None: pass
909
910 a: A
911
912 (a, a) + a # E: Unsupported operand types for + ("tuple[A, A]" and "A")
913 a + (a, a) # E: Unsupported operand types for + ("A" and "tuple[A, A]")
914 f((a, a)) # E: Argument 1 to "f" has incompatible type "tuple[A, A]"; expected "A"
915 (a, a).foo # E: "tuple[A, A]" has no attribute "foo"
916 [builtins fixtures/tuple.pyi]
917
918 [case testLargeTuplesInErrorMessages]
919
920 a: LongTypeName
921 a + (a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a) # Fail
922
923 class LongTypeName:
924 def __add__(self, x: 'LongTypeName') -> 'LongTypeName': pass
925 [builtins fixtures/tuple.pyi]
926 [out]
927 main:3: error: Unsupported operand types for + ("LongTypeName" and "tuple[LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName, LongTypeName]")
928
929
930 -- Tuple methods
931 -- -------------
932
933
934 [case testTupleMethods]
935 from typing import Tuple
936 t: Tuple[int, str]
937 i = 0
938 s = ''
939 b = bool()
940
941 if int():
942 s = t.__len__() # E: Incompatible types in assignment (expression has type "int", variable has type "str")
943 if int():
944 i = t.__str__() # E: Incompatible types in assignment (expression has type "str", variable has type "int")
945 if int():
946 i = s in t # E: Incompatible types in assignment (expression has type "bool", variable has type "int")
947 t.foo # E: "tuple[int, str]" has no attribute "foo"
948
949 if int():
950 i = t.__len__()
951 if int():
952 s = t.__str__()
953 if int():
954 b = s in t
955
956 [file builtins.py]
957 from typing import TypeVar, Generic
958 _T = TypeVar('_T')
959 class object:
960 def __init__(self) -> None: pass
961 class tuple(Generic[_T]):
962 def __len__(self) -> int: pass
963 def __str__(self) -> str: pass
964 def __contains__(self, o: object) -> bool: pass
965 class int: pass
966 class str: pass
967 class bool: pass
968 class type: pass
969 class function: pass
970 class dict: pass
971
972
973 -- For loop over tuple
974 -- -------------------
975
976
977 [case testForLoopOverTuple]
978 import typing
979 t = 1, 2
980 for x in t:
981 x = 1
982 x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
983 [builtins fixtures/for.pyi]
984
985 [case testForLoopOverEmptyTuple]
986 import typing
987 t = ()
988 for x in t: pass # E: Need type annotation for "x"
989 [builtins fixtures/for.pyi]
990
991 [case testForLoopOverNoneValuedTuple]
992 import typing
993 for x in None, None: pass
994 [builtins fixtures/for.pyi]
995
996 [case testForLoopOverTupleAndSubtyping]
997 import typing
998 class A: pass
999 class B(A): pass
1000 for x in B(), A():
1001 x = A()
1002 x = B()
1003 x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "A")
1004 [builtins fixtures/for.pyi]
1005
1006 [case testTupleIterable]
1007 from typing import Iterable, Optional, TypeVar
1008
1009 T = TypeVar("T")
1010
1011 def sum(iterable: Iterable[T], start: Optional[T] = None) -> T: pass
1012
1013 y = 'a'
1014 x = sum((1,2))
1015 if int():
1016 y = x # E: Incompatible types in assignment (expression has type "int", variable has type "str")
1017 [builtins fixtures/tuple.pyi]
1018
1019
1020 -- Tuple as a base type
1021 -- --------------------
1022
1023
1024 [case testTupleBaseClass]
1025 import m
1026 [file m.pyi]
1027 from typing import Tuple
1028 class A(Tuple[int, str]):
1029 def f(self, x: int) -> None:
1030 a, b = 1, ''
1031 if int():
1032 a, b = self
1033 b, a = self # Error
1034 self.f('') # Error
1035 [builtins fixtures/tuple.pyi]
1036 [out]
1037 tmp/m.pyi:7: error: Incompatible types in assignment (expression has type "int", variable has type "str")
1038 tmp/m.pyi:7: error: Incompatible types in assignment (expression has type "str", variable has type "int")
1039 tmp/m.pyi:8: error: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
1040
1041 [case testValidTupleBaseClass2]
1042 from typing import Tuple
1043 class A(Tuple[int, str]): pass
1044
1045 x, y = A()
1046 reveal_type(x) # N: Revealed type is "builtins.int"
1047 reveal_type(y) # N: Revealed type is "builtins.str"
1048
1049 x1 = A()[0] # type: int
1050 x2 = A()[1] # type: int # E: Incompatible types in assignment (expression has type "str", variable has type "int")
1051 A()[2] # E: Tuple index out of range
1052
1053 class B(Tuple[int, ...]): pass
1054
1055 z1 = B()[0] # type: int
1056 z2 = B()[1] # type: str # E: Incompatible types in assignment (expression has type "int", variable has type "str")
1057 B()[100]
1058 [builtins fixtures/tuple.pyi]
1059 [out]
1060
1061 [case testValidTupleBaseClass]
1062 from typing import Tuple
1063 class A(tuple): pass
1064 [builtins fixtures/tuple.pyi]
1065 [out]
1066
1067 [case testTupleBaseClass2]
1068 import m
1069 [file m.pyi]
1070 from typing import Tuple
1071 a: A
1072 class A(Tuple[int, str]): pass
1073 x, y = a
1074 x() # E: "int" not callable
1075 y() # E: "str" not callable
1076 [builtins fixtures/tuple.pyi]
1077 [out]
1078
1079 [case testGenericClassWithTupleBaseClass]
1080 from typing import TypeVar, Generic, Tuple
1081 T = TypeVar('T')
1082 class Test(Generic[T], Tuple[T]): pass
1083 x = Test() # type: Test[int]
1084 reveal_type(x) # N: Revealed type is "tuple[builtins.int, fallback=__main__.Test[builtins.int]]"
1085 [builtins fixtures/tuple.pyi]
1086 [out]
1087
1088
1089 -- Variable-length tuples (Tuple[t, ...] with literal '...')
1090 -- ---------------------------------------------------------
1091
1092
1093 [case testIndexingVariableLengthTuple]
1094 from typing import Tuple
1095 x = () # type: Tuple[str, ...]
1096 n = 5
1097 x[n]() # E: "str" not callable
1098 x[3]() # E: "str" not callable
1099 [builtins fixtures/tuple.pyi]
1100
1101 [case testSubtypingVariableLengthTuple]
1102 from typing import Tuple
1103 class A: pass
1104 class B(A): pass
1105 def fa(t: Tuple[A, ...]) -> None: pass
1106 def fb(t: Tuple[B, ...]) -> None: pass
1107 ta = () # type: Tuple[A, ...]
1108 tb = () # type: Tuple[B, ...]
1109 fa(ta)
1110 fa(tb)
1111 fb(tb)
1112 fb(ta) # E: Argument 1 to "fb" has incompatible type "tuple[A, ...]"; expected "tuple[B, ...]"
1113 [builtins fixtures/tuple.pyi]
1114
1115 [case testSubtypingFixedAndVariableLengthTuples]
1116 from typing import Tuple
1117 class A: pass
1118 class B(A): pass
1119 def fa(t: Tuple[A, ...]) -> None: pass
1120 def fb(t: Tuple[B, ...]) -> None: pass
1121 aa = (A(), A())
1122 ab = (A(), B())
1123 bb = (B(), B())
1124 fa(aa)
1125 fa(ab)
1126 fa(bb)
1127 fb(bb)
1128 fb(ab) # E: Argument 1 to "fb" has incompatible type "tuple[A, B]"; expected "tuple[B, ...]"
1129 fb(aa) # E: Argument 1 to "fb" has incompatible type "tuple[A, A]"; expected "tuple[B, ...]"
1130 [builtins fixtures/tuple.pyi]
1131
1132 [case testSubtypingTupleIsContainer]
1133 from typing import Container
1134 a: Container[str]
1135 a = ()
1136 [typing fixtures/typing-full.pyi]
1137 [builtins fixtures/tuple.pyi]
1138
1139 [case testSubtypingTupleIsSized]
1140 from typing import Sized
1141 a: Sized
1142 a = ()
1143 [typing fixtures/typing-medium.pyi]
1144 [builtins fixtures/tuple.pyi]
1145
1146 [case testTupleWithStarExpr1]
1147
1148 a = (1, 2)
1149 b = (*a, '')
1150 reveal_type(b) # N: Revealed type is "tuple[builtins.int, builtins.int, builtins.str]"
1151 [builtins fixtures/tuple.pyi]
1152
1153 [case testTupleWithStarExpr2]
1154 a = [1]
1155 b = (0, *a)
1156 reveal_type(b) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1157 [builtins fixtures/tuple.pyi]
1158
1159 [case testTupleWithStarExpr2Precise]
1160 # flags: --enable-incomplete-feature=PreciseTupleTypes
1161 a = [1]
1162 b = (0, *a)
1163 reveal_type(b) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.int, ...]]]"
1164 [builtins fixtures/tuple.pyi]
1165
1166 [case testTupleWithStarExpr3]
1167 a = ['']
1168 b = (0, *a)
1169 reveal_type(b) # N: Revealed type is "builtins.tuple[builtins.object, ...]"
1170 c = (*a, '')
1171 reveal_type(c) # N: Revealed type is "builtins.tuple[builtins.str, ...]"
1172 [builtins fixtures/tuple.pyi]
1173
1174 [case testTupleWithStarExpr3Precise]
1175 # flags: --enable-incomplete-feature=PreciseTupleTypes
1176 a = ['']
1177 b = (0, *a)
1178 reveal_type(b) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.str, ...]]]"
1179 c = (*a, '')
1180 reveal_type(c) # N: Revealed type is "tuple[Unpack[builtins.tuple[builtins.str, ...]], builtins.str]"
1181 [builtins fixtures/tuple.pyi]
1182
1183 [case testTupleWithStarExpr4]
1184 a = (1, 1, 'x', 'x')
1185 b = (1, 'x')
1186 a = (0, *b, '')
1187 [builtins fixtures/tuple.pyi]
1188
1189 [case testUnpackSyntaxError]
1190 *foo # E: can't use starred expression here
1191 [builtins fixtures/tuple.pyi]
1192
1193 [case testUnpackBases]
1194 class A: ...
1195 class B: ...
1196 bases = (A, B)
1197 class C(*bases): ... # E: Invalid base class
1198 [builtins fixtures/tuple.pyi]
1199
1200 [case testTupleMeetTupleAny]
1201 from typing import Union, Tuple
1202 class A: pass
1203 class B: pass
1204
1205 def f(x: Union[B, Tuple[A, A]]) -> None:
1206 if isinstance(x, tuple):
1207 reveal_type(x) # N: Revealed type is "tuple[__main__.A, __main__.A]"
1208 else:
1209 reveal_type(x) # N: Revealed type is "__main__.B"
1210
1211 def g(x: Union[str, Tuple[str, str]]) -> None:
1212 if isinstance(x, tuple):
1213 reveal_type(x) # N: Revealed type is "tuple[builtins.str, builtins.str]"
1214 else:
1215 reveal_type(x) # N: Revealed type is "builtins.str"
1216
1217 [builtins fixtures/tuple.pyi]
1218 [out]
1219
1220 [case testTupleMeetTupleAnyComplex]
1221 from typing import Tuple, Union
1222
1223 Pair = Tuple[int, int]
1224 Variant = Union[int, Pair]
1225 def tuplify(v: Variant) -> None:
1226 reveal_type(v) # N: Revealed type is "builtins.int | tuple[builtins.int, builtins.int]"
1227 if not isinstance(v, tuple):
1228 reveal_type(v) # N: Revealed type is "builtins.int"
1229 v = (v, v)
1230 reveal_type(v) # N: Revealed type is "tuple[builtins.int, builtins.int]"
1231 reveal_type(v) # N: Revealed type is "tuple[builtins.int, builtins.int]"
1232 reveal_type(v[0]) # N: Revealed type is "builtins.int"
1233
1234 Pair2 = Tuple[int, str]
1235 Variant2 = Union[int, Pair2]
1236 def tuplify2(v: Variant2) -> None:
1237 if isinstance(v, tuple):
1238 reveal_type(v) # N: Revealed type is "tuple[builtins.int, builtins.str]"
1239 else:
1240 reveal_type(v) # N: Revealed type is "builtins.int"
1241 [builtins fixtures/tuple.pyi]
1242 [out]
1243
1244 [case testTupleMeetTupleAnyAfter]
1245 from typing import Tuple, Union
1246
1247 def good(blah: Union[Tuple[int, int], int]) -> None:
1248 reveal_type(blah) # N: Revealed type is "tuple[builtins.int, builtins.int] | builtins.int"
1249 if isinstance(blah, tuple):
1250 reveal_type(blah) # N: Revealed type is "tuple[builtins.int, builtins.int]"
1251 reveal_type(blah) # N: Revealed type is "tuple[builtins.int, builtins.int] | builtins.int"
1252 [builtins fixtures/tuple.pyi]
1253 [out]
1254
1255 [case testTupleMeetTupleVariable]
1256 from typing import Tuple, TypeVar, Generic, Union
1257 T = TypeVar('T')
1258
1259 class A: pass
1260 class B1(A): pass
1261 class B2(A): pass
1262 class C: pass
1263
1264 x: Tuple[A, ...]
1265 y: Tuple[Union[B1, C], Union[B2, C]]
1266
1267 def g(x: T) -> Tuple[T, T]:
1268 return (x, x)
1269
1270 z = 1
1271 x, y = g(z) # E: Argument 1 to "g" has incompatible type "int"; expected "tuple[B1, B2]"
1272 [builtins fixtures/tuple.pyi]
1273 [out]
1274
1275 [case testFixedTupleJoinVarTuple]
1276 from typing import Tuple, TypeVar
1277
1278 class A: pass
1279 class B(A): pass
1280
1281 fixtup: Tuple[B, B]
1282
1283 T = TypeVar("T")
1284 def join(x: T, y: T) -> T: ...
1285
1286 vartup_b: Tuple[B, ...]
1287 reveal_type(join(fixtup, vartup_b)) # N: Revealed type is "builtins.tuple[__main__.B, ...]"
1288 reveal_type(join(vartup_b, fixtup)) # N: Revealed type is "builtins.tuple[__main__.B, ...]"
1289
1290 vartup_a: Tuple[A, ...]
1291 reveal_type(join(fixtup, vartup_a)) # N: Revealed type is "builtins.tuple[__main__.A, ...]"
1292 reveal_type(join(vartup_a, fixtup)) # N: Revealed type is "builtins.tuple[__main__.A, ...]"
1293
1294 [builtins fixtures/tuple.pyi]
1295 [out]
1296
1297 [case testFixedTupleJoinList]
1298 from typing import Tuple, List, TypeVar
1299
1300 class A: pass
1301 class B(A): pass
1302
1303 fixtup: Tuple[B, B]
1304
1305 T = TypeVar("T")
1306 def join(x: T, y: T) -> T: ...
1307
1308 lst_b: List[B]
1309 reveal_type(join(fixtup, lst_b)) # N: Revealed type is "typing.Sequence[__main__.B]"
1310 reveal_type(join(lst_b, fixtup)) # N: Revealed type is "typing.Sequence[__main__.B]"
1311
1312 lst_a: List[A]
1313 reveal_type(join(fixtup, lst_a)) # N: Revealed type is "typing.Sequence[__main__.A]"
1314 reveal_type(join(lst_a, fixtup)) # N: Revealed type is "typing.Sequence[__main__.A]"
1315
1316 [builtins fixtures/tuple.pyi]
1317 [out]
1318
1319 [case testEmptyTupleJoin]
1320 from typing import Tuple, List, TypeVar
1321
1322 class A: pass
1323
1324 empty = ()
1325
1326 T = TypeVar("T")
1327 def join(x: T, y: T) -> T: ...
1328
1329 fixtup: Tuple[A]
1330 reveal_type(join(fixtup, empty)) # N: Revealed type is "builtins.tuple[__main__.A, ...]"
1331 reveal_type(join(empty, fixtup)) # N: Revealed type is "builtins.tuple[__main__.A, ...]"
1332
1333 vartup: Tuple[A, ...]
1334 reveal_type(join(vartup, empty)) # N: Revealed type is "builtins.tuple[__main__.A, ...]"
1335 reveal_type(join(empty, vartup)) # N: Revealed type is "builtins.tuple[__main__.A, ...]"
1336
1337 lst: List[A]
1338 reveal_type(join(empty, lst)) # N: Revealed type is "typing.Sequence[__main__.A]"
1339 reveal_type(join(lst, empty)) # N: Revealed type is "typing.Sequence[__main__.A]"
1340
1341 [builtins fixtures/tuple.pyi]
1342 [out]
1343
1344 [case testTupleSubclassJoin]
1345 from typing import Tuple, NamedTuple, TypeVar
1346
1347 class NTup(NamedTuple):
1348 a: bool
1349 b: bool
1350
1351 class SubTuple(Tuple[bool]): ...
1352 class SubVarTuple(Tuple[int, ...]): ...
1353
1354 ntup: NTup
1355 subtup: SubTuple
1356 vartup: SubVarTuple
1357
1358 T = TypeVar("T")
1359 def join(x: T, y: T) -> T: ...
1360
1361 reveal_type(join(ntup, vartup)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1362 reveal_type(join(subtup, vartup)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1363
1364 [builtins fixtures/tuple.pyi]
1365 [out]
1366
1367 [case testTupleJoinIrregular]
1368 from typing import Tuple, TypeVar
1369
1370 tup1: Tuple[bool, int]
1371 tup2: Tuple[bool]
1372
1373 T = TypeVar("T")
1374 def join(x: T, y: T) -> T: ...
1375
1376 reveal_type(join(tup1, tup2)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1377 reveal_type(join(tup2, tup1)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1378
1379 reveal_type(join(tup1, ())) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1380 reveal_type(join((), tup1)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1381
1382 reveal_type(join(tup2, ())) # N: Revealed type is "builtins.tuple[builtins.bool, ...]"
1383 reveal_type(join((), tup2)) # N: Revealed type is "builtins.tuple[builtins.bool, ...]"
1384
1385 [builtins fixtures/tuple.pyi]
1386 [out]
1387
1388 [case testTupleSubclassJoinIrregular]
1389 from typing import Tuple, NamedTuple, TypeVar
1390
1391 class NTup1(NamedTuple):
1392 a: bool
1393
1394 class NTup2(NamedTuple):
1395 a: bool
1396 b: bool
1397
1398 class SubTuple(Tuple[bool, int, int]): ...
1399
1400 tup1: NTup1
1401 tup2: NTup2
1402 subtup: SubTuple
1403
1404 T = TypeVar("T")
1405 def join(x: T, y: T) -> T: ...
1406
1407 reveal_type(join(tup1, tup2)) # N: Revealed type is "builtins.tuple[builtins.bool, ...]"
1408 reveal_type(join(tup2, tup1)) # N: Revealed type is "builtins.tuple[builtins.bool, ...]"
1409
1410 reveal_type(join(tup1, subtup)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1411 reveal_type(join(subtup, tup1)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1412
1413 reveal_type(join(tup2, subtup)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1414 reveal_type(join(subtup, tup2)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1415
1416 [builtins fixtures/tuple.pyi]
1417 [out]
1418
1419 [case testTupleWithUndersizedContext]
1420 a = ([1], 'x')
1421 if int():
1422 a = ([], 'x', 1) # E: Incompatible types in assignment (expression has type "tuple[list[Never], str, int]", variable has type "tuple[list[int], str]")
1423 [builtins fixtures/tuple.pyi]
1424
1425 [case testTupleWithOversizedContext]
1426 a = (1, [1], 'x')
1427 if int():
1428 a = (1, []) # E: Incompatible types in assignment (expression has type "tuple[int, list[int]]", variable has type "tuple[int, list[int], str]")
1429 [builtins fixtures/tuple.pyi]
1430
1431 [case testTupleWithoutContext]
1432 a = (1, []) # E: Need type annotation for "a"
1433 [builtins fixtures/tuple.pyi]
1434
1435 [case testTupleWithUnionContext]
1436 from typing import List, Union, Tuple
1437 def f() -> Union[int, Tuple[List[str]]]:
1438 return ([],)
1439 [builtins fixtures/tuple.pyi]
1440
1441 [case testTupleWithVariableSizedTupleContext]
1442 from typing import List, Tuple
1443 def f() -> Tuple[List[str], ...]:
1444 return ([],)
1445 [builtins fixtures/tuple.pyi]
1446
1447 [case testTupleWithoutArgs]
1448 from typing import Tuple
1449 def f(a: Tuple) -> None: pass
1450 f(())
1451 f((1,))
1452 f(('', ''))
1453 f(0) # E: Argument 1 to "f" has incompatible type "int"; expected "tuple[Any, ...]"
1454 [builtins fixtures/tuple.pyi]
1455
1456 [case testTupleSingleton]
1457
1458 from typing import Tuple
1459 def f(a: Tuple[()]) -> None: pass
1460 f(())
1461 f((1,)) # E: Argument 1 to "f" has incompatible type "tuple[int]"; expected "tuple[()]"
1462 f(('', '')) # E: Argument 1 to "f" has incompatible type "tuple[str, str]"; expected "tuple[()]"
1463 f(0) # E: Argument 1 to "f" has incompatible type "int"; expected "tuple[()]"
1464 [builtins fixtures/tuple.pyi]
1465
1466 [case testNonliteralTupleIndex]
1467 t = (0, "")
1468 x = 0
1469 y = ""
1470 reveal_type(t[x]) # N: Revealed type is "builtins.int | builtins.str"
1471 t[y] # E: No overload variant of "__getitem__" of "tuple" matches argument type "str" \
1472 # N: Possible overload variants: \
1473 # N: def __getitem__(self, int, /) -> int | str \
1474 # N: def __getitem__(self, slice[int | None], /) -> tuple[int | str, ...]
1475
1476 [builtins fixtures/tuple.pyi]
1477
1478 [case testNonliteralTupleSlice]
1479 t = (0, "")
1480 x = 0
1481 y = ""
1482 reveal_type(t[x:]) # N: Revealed type is "builtins.tuple[builtins.int | builtins.str, ...]"
1483 t[y:] # E: Invalid index type "slice[str, None, None]" for "tuple[int, str]"; expected type "slice[int | None]"
1484 [builtins fixtures/tuple.pyi]
1485
1486 [case testTupleSliceStepZeroNoCrash]
1487 # This was crashing: https://github.com/python/mypy/issues/18062
1488 # TODO: emit better error when 0 is used for step
1489 ()[::0] # E: Ambiguous slice of a variadic tuple
1490 [builtins fixtures/tuple.pyi]
1491
1492 [case testInferTupleTypeFallbackAgainstInstance]
1493 from typing import TypeVar, Generic, Tuple
1494 T = TypeVar('T')
1495
1496 class Base(Generic[T]): pass
1497 def f(x: Base[T]) -> T: pass
1498
1499 class DT(Tuple[str, str], Base[int]):
1500 pass
1501
1502 reveal_type(f(DT())) # N: Revealed type is "builtins.int"
1503
1504 [builtins fixtures/tuple.pyi]
1505 [out]
1506
1507 [case testTypeTupleClassmethod]
1508 from typing import Tuple, Type
1509
1510 class C(Tuple[int, str]):
1511 @classmethod
1512 def f(cls) -> None: pass
1513
1514 t: Type[C]
1515 t.g() # E: "type[C]" has no attribute "g"
1516 t.f()
1517 [builtins fixtures/classmethod.pyi]
1518
1519 [case testTypeTupleCall]
1520 from typing import Tuple
1521
1522 def foo(o: CallableTuple) -> int:
1523 reveal_type(o) # N: Revealed type is "tuple[builtins.str, builtins.int, fallback=__main__.CallableTuple]"
1524 return o(1, 2)
1525
1526 class CallableTuple(Tuple[str, int]):
1527 def __call__(self, n: int, m: int) -> int:
1528 return n
1529 [builtins fixtures/tuple.pyi]
1530
1531 [case testTypeTupleGenericCall]
1532 from typing import Generic, Tuple, TypeVar
1533
1534 T = TypeVar('T')
1535
1536 def foo(o: CallableTuple[int]) -> int:
1537 reveal_type(o) # N: Revealed type is "tuple[builtins.str, builtins.int, fallback=__main__.CallableTuple[builtins.int]]"
1538 reveal_type(o.count(3)) # N: Revealed type is "builtins.int"
1539 return o(1, 2)
1540
1541 class CallableTuple(Tuple[str, T]):
1542 def __call__(self, n: int, m: int) -> int:
1543 return n
1544 [builtins fixtures/tuple.pyi]
1545
1546 [case testTupleCompatibleWithSequence]
1547 from typing import Sequence
1548 s: Sequence[str]
1549 s = tuple()
1550 reveal_type(s) # N: Revealed type is "builtins.tuple[builtins.str, ...]"
1551
1552 [builtins fixtures/tuple.pyi]
1553
1554 [case testTupleInstanceCompatibleWithIterable]
1555 from typing import Iterable, Tuple
1556 x: Iterable[int] = ()
1557 y: Tuple[int, ...] = (1, 2, 3)
1558 x = y
1559 reveal_type(x) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1560
1561 [builtins fixtures/tuple.pyi]
1562
1563 [case testTupleTypeCompatibleWithIterable]
1564 from typing import Iterable, Tuple
1565 x: Iterable[int] = ()
1566 y: Tuple[int, int] = (1, 2)
1567 x = y
1568 reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int]"
1569 [builtins fixtures/tuple.pyi]
1570
1571 [case testTupleOverlapDifferentTuples]
1572 # flags: --warn-unreachable
1573 from typing import Optional, Tuple
1574 class A: pass
1575 class B: pass
1576
1577 def f1(possibles: Tuple[int, Tuple[A]], x: Optional[Tuple[B]]):
1578 if x in possibles:
1579 reveal_type(x) # E: Statement is unreachable
1580 else:
1581 reveal_type(x) # N: Revealed type is "tuple[__main__.B] | None"
1582
1583 class AA(A): pass
1584
1585 def f2(possibles: Tuple[int, Tuple[A]], x: Optional[Tuple[AA]]):
1586 if x in possibles:
1587 reveal_type(x) # N: Revealed type is "tuple[__main__.AA]"
1588 else:
1589 reveal_type(x) # N: Revealed type is "tuple[__main__.AA] | None"
1590
1591 [builtins fixtures/tuple.pyi]
1592
1593 [case testUnionOfTupleIndex]
1594 from typing import Union, Tuple
1595
1596 tup: Union[Tuple[int, str], Tuple[int, int, str]]
1597 reveal_type(tup[0]) # N: Revealed type is "builtins.int"
1598 reveal_type(tup[1]) # N: Revealed type is "builtins.str | builtins.int"
1599 reveal_type(tup[2]) # E: Tuple index out of range \
1600 # N: Revealed type is "Any | builtins.str"
1601 reveal_type(tup[:]) # N: Revealed type is "tuple[builtins.int, builtins.str] | tuple[builtins.int, builtins.int, builtins.str]"
1602
1603 [builtins fixtures/tuple.pyi]
1604
1605 [case testUnionOfTupleIndexMixed]
1606 from typing import Union, Tuple, List
1607
1608 tup: Union[Tuple[int, str], List[int]]
1609 reveal_type(tup[0]) # N: Revealed type is "builtins.int"
1610 reveal_type(tup[1]) # N: Revealed type is "builtins.str | builtins.int"
1611 reveal_type(tup[2]) # E: Tuple index out of range \
1612 # N: Revealed type is "Any | builtins.int"
1613 reveal_type(tup[:]) # N: Revealed type is "tuple[builtins.int, builtins.str] | builtins.list[builtins.int]"
1614
1615 [builtins fixtures/tuple.pyi]
1616
1617 [case testFixedLengthTupleConcatenation]
1618 a = (1, "foo", 3)
1619 b = ("bar", 7)
1620
1621 reveal_type(a + b) # N: Revealed type is "tuple[builtins.int, builtins.str, builtins.int, builtins.str, builtins.int]"
1622
1623 [builtins fixtures/tuple.pyi]
1624
1625 [case testAssigningWithLongTupleInitializer]
1626 from typing import Tuple
1627
1628 # long initializer assignment with few mismatches
1629 t: Tuple[int, ...] = (1, 2, 3, 4, 5, 6, 7, 8, "str", "str", "str", 11) # E: Incompatible types in assignment (3 tuple items are incompatible) \
1630 # N: Expression tuple item 8 has type "str"; "int" expected; \
1631 # N: Expression tuple item 9 has type "str"; "int" expected; \
1632 # N: Expression tuple item 10 has type "str"; "int" expected;
1633
1634 # long initializer assignment with more mismatches
1635 t1: Tuple[int, ...] = (1, 2, 3, 4, 5, 6, 7, 8, "str", "str", "str", "str") # E: Incompatible types in assignment (4 tuple items are incompatible; 1 items are omitted) \
1636 # N: Expression tuple item 8 has type "str"; "int" expected; \
1637 # N: Expression tuple item 9 has type "str"; "int" expected; \
1638 # N: Expression tuple item 10 has type "str"; "int" expected;
1639
1640 # short tuple initializer assignment
1641 t2: Tuple[int, ...] = (1, 2, "s", 4) # E: Incompatible types in assignment (expression has type "tuple[int, int, str, int]", variable has type "tuple[int, ...]")
1642
1643 # long initializer assignment with few mismatches, no ellipsis
1644 t3: Tuple[int, int, int, int, int, int, int, int, int, int, int, int] = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "str", "str") # E: Incompatible types in assignment (2 tuple items are incompatible) \
1645 # N: Expression tuple item 10 has type "str"; "int" expected; \
1646 # N: Expression tuple item 11 has type "str"; "int" expected;
1647
1648 # long initializer assignment with more mismatches, no ellipsis
1649 t4: Tuple[int, int, int, int, int, int, int, int, int, int, int, int] = (1, 2, 3, 4, 5, 6, 7, 8, "str", "str", "str", "str") # E: Incompatible types in assignment (4 tuple items are incompatible; 1 items are omitted) \
1650 # N: Expression tuple item 8 has type "str"; "int" expected; \
1651 # N: Expression tuple item 9 has type "str"; "int" expected; \
1652 # N: Expression tuple item 10 has type "str"; "int" expected;
1653
1654 # short tuple initializer assignment, no ellipsis
1655 t5: Tuple[int, int] = (1, 2, "s", 4) # E: Incompatible types in assignment (expression has type "tuple[int, int, str, int]", variable has type "tuple[int, int]")
1656
1657 # long initializer assignment with mismatched pairs
1658 t6: Tuple[int, int, int, int, int, int, int, int, int, int, int, int] = (1, 2, 3, 4, 5, 6, 7, 8, "str", "str", "str", "str", 1, 1, 1, 1, 1) # E: Incompatible types in assignment (expression has type "tuple[int, int, ... <15 more items>]", variable has type "tuple[int, int, ... <10 more items>]")
1659
1660 [builtins fixtures/tuple.pyi]
1661
1662 [case testPropertyLongTupleReturnTypeMismatchUnion]
1663 from typing import Tuple, Union
1664 class A:
1665 a: str
1666 b: str
1667 c: str
1668 d: str
1669 e: str
1670 f: str
1671 g: Union[str, int]
1672 h: Union[str, float]
1673 i: Union[str, None]
1674 j: Union[str, None]
1675 k: Union[str, None]
1676 l: Union[str, None]
1677
1678 @property
1679 def x(self) -> Tuple[str, str, str, str, str, str, str, str, str, str, str, str]:
1680 return (
1681 self.a,
1682 self.b,
1683 self.c,
1684 self.d,
1685 self.e,
1686 self.f,
1687 self.g,
1688 self.h,
1689 self.i,
1690 self.j,
1691 self.k,
1692 self.l,
1693 )
1694 [out]
1695 main:18: error: Incompatible return value type (6 tuple items are incompatible; 3 items are omitted)
1696 main:18: note: Expression tuple item 6 has type "str | int"; "str" expected;
1697 main:18: note: Expression tuple item 7 has type "str | float"; "str" expected;
1698 main:18: note: Expression tuple item 8 has type "str | None"; "str" expected;
1699 [builtins fixtures/property.pyi]
1700
1701 [case testPropertyLongTupleReturnTypeMismatchUnionWiderExpected]
1702 from typing import Tuple, Union
1703 class A:
1704 a: str
1705 b: str
1706 c: str
1707 d: str
1708 e: str
1709 f: str
1710 g: str
1711 h: str
1712 i: str
1713 j: str
1714 k: str
1715 l: Union[float, int]
1716
1717 @property
1718 def x(self) -> Tuple[Union[str, int], Union[str, float], int, Union[str, None], Union[str, None], Union[str, None], str, str, str, str, str, str]:
1719 return (
1720 self.a,
1721 self.b,
1722 self.c,
1723 self.d,
1724 self.e,
1725 self.f,
1726 self.g,
1727 self.h,
1728 self.i,
1729 self.j,
1730 self.k,
1731 self.l,
1732 )
1733 [out]
1734 main:18: error: Incompatible return value type (2 tuple items are incompatible)
1735 main:18: note: Expression tuple item 2 has type "str"; "int" expected;
1736 main:18: note: Expression tuple item 11 has type "float | int"; "str" expected;
1737 [builtins fixtures/property.pyi]
1738
1739 [case testTupleWithStarExpr]
1740 from typing import Tuple, List
1741 points = (1, "test") # type: Tuple[int, str]
1742 x, y, z = *points, 0
1743 reveal_type(x) # N: Revealed type is "builtins.int"
1744 reveal_type(y) # N: Revealed type is "builtins.str"
1745 reveal_type(z) # N: Revealed type is "builtins.int"
1746
1747 points2 = [1,2]
1748 x2, y2, z2= *points2, "test"
1749
1750 reveal_type(x2) # N: Revealed type is "builtins.int"
1751 reveal_type(y2) # N: Revealed type is "builtins.int"
1752 reveal_type(z2) # N: Revealed type is "builtins.str"
1753
1754 x3, x4, y3, y4, z3 = *points, *points2, "test"
1755
1756 reveal_type(x3) # N: Revealed type is "builtins.int"
1757 reveal_type(x4) # N: Revealed type is "builtins.str"
1758 reveal_type(y3) # N: Revealed type is "builtins.int"
1759 reveal_type(y4) # N: Revealed type is "builtins.int"
1760 reveal_type(z3) # N: Revealed type is "builtins.str"
1761
1762 x5, x6, y5, y6, z4 = *points2, *points2, "test"
1763
1764 reveal_type(x5) # N: Revealed type is "builtins.int"
1765 reveal_type(x6) # N: Revealed type is "builtins.int"
1766 reveal_type(y5) # N: Revealed type is "builtins.int"
1767 reveal_type(y6) # N: Revealed type is "builtins.int"
1768 reveal_type(z4) # N: Revealed type is "builtins.str"
1769
1770 points3 = ["test1", "test2"]
1771 x7, x8, y7, y8 = *points2, *points3 # E: Contiguous iterable with same type expected
1772
1773 x9, y9, x10, y10, z5 = *points2, 1, *points2 # E: Contiguous iterable with same type expected
1774 [builtins fixtures/tuple.pyi]
1775
1776 [case testAssignEmpty]
1777 () = []
1778
1779 [case testAssignEmptyBogus]
1780 () = 1 # E: "int" object is not iterable
1781 [builtins fixtures/tuple.pyi]
1782
1783 [case testMultiplyTupleByIntegerLiteral]
1784 from typing import Tuple
1785 t = ('',) * 2
1786 reveal_type(t) # N: Revealed type is "tuple[builtins.str, builtins.str]"
1787 t2 = ('',) * -1
1788 reveal_type(t2) # N: Revealed type is "tuple[()]"
1789 t3 = ('', 1) * 2
1790 reveal_type(t3) # N: Revealed type is "tuple[builtins.str, builtins.int, builtins.str, builtins.int]"
1791 def f() -> Tuple[str, ...]:
1792 return ('', )
1793 reveal_type(f() * 2) # N: Revealed type is "builtins.tuple[builtins.str, ...]"
1794 [builtins fixtures/tuple.pyi]
1795
1796 [case testEmptyTupleTypeRepr]
1797 from typing import Tuple
1798
1799 def f() -> Tuple[()]: ...
1800
1801 reveal_type(f) # N: Revealed type is "def () -> tuple[()]"
1802 reveal_type(f()) # N: Revealed type is "tuple[()]"
1803 [builtins fixtures/tuple.pyi]
1804
1805 [case testMultiplyTupleByIntegerLiteralReverse]
1806 from typing import Tuple
1807 t = 2 * ('',)
1808 reveal_type(t) # N: Revealed type is "tuple[builtins.str, builtins.str]"
1809 t2 = -1 * ('',)
1810 reveal_type(t2) # N: Revealed type is "tuple[()]"
1811 t3 = 2 * ('', 1)
1812 reveal_type(t3) # N: Revealed type is "tuple[builtins.str, builtins.int, builtins.str, builtins.int]"
1813 def f() -> Tuple[str, ...]:
1814 return ('', )
1815 reveal_type(2 * f()) # N: Revealed type is "builtins.tuple[builtins.str, ...]"
1816 [builtins fixtures/tuple.pyi]
1817
1818 [case testSingleUndefinedTypeAndTuple]
1819 from typing import Tuple
1820
1821 class Foo:
1822 ...
1823
1824 class Bar(aaaaaaaaaa): # E: Name "aaaaaaaaaa" is not defined
1825 ...
1826
1827 class FooBarTuple(Tuple[Foo, Bar]):
1828 ...
1829 [builtins fixtures/tuple.pyi]
1830
1831 [case testMultipleUndefinedTypeAndTuple]
1832 from typing import Tuple
1833
1834 class Foo(aaaaaaaaaa): # E: Name "aaaaaaaaaa" is not defined
1835 ...
1836
1837 class Bar(aaaaaaaaaa): # E: Name "aaaaaaaaaa" is not defined
1838 ...
1839
1840 class FooBarTuple(Tuple[Foo, Bar]):
1841 ...
1842 [builtins fixtures/tuple.pyi]
1843
1844
1845 [case testTupleOverloadZipAny]
1846 from typing import Any, Iterable, Iterator, Tuple, TypeVar, overload
1847
1848 T = TypeVar("T")
1849
1850 @overload
1851 def zip(__i: Iterable[T]) -> Iterator[Tuple[T]]: ...
1852 @overload
1853 def zip(*i: Iterable[Any]) -> Iterator[Tuple[Any, ...]]: ...
1854 def zip(i): ...
1855
1856 def g(t: Tuple):
1857 reveal_type(zip(*t)) # N: Revealed type is "typing.Iterator[builtins.tuple[Any, ...]]"
1858 reveal_type(zip(t)) # N: Revealed type is "typing.Iterator[tuple[Any]]"
1859 [builtins fixtures/tuple.pyi]
1860
1861 [case testTupleSubclassSlice]
1862 from typing import Tuple
1863
1864 class A: ...
1865
1866 class tuple_aa_subclass(Tuple[A, A]): ...
1867
1868 inst_tuple_aa_subclass: tuple_aa_subclass = tuple_aa_subclass((A(), A()))[:] # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "tuple_aa_subclass")
1869 [builtins fixtures/tuple.pyi]
1870
1871 [case testTuplePassedParameters]
1872 from typing_extensions import Concatenate
1873
1874 def c(t: tuple[Concatenate[int, ...]]) -> None: # E: Cannot use "[int, VarArg(Any), KwArg(Any)]" for tuple, only for ParamSpec
1875 reveal_type(t) # N: Revealed type is "tuple[Any]"
1876 [builtins fixtures/tuple.pyi]