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 testMultipleAssignmentWithSquareBracketTuples]
430 # flags: --no-strict-optional
431 from typing import Tuple
432
433 def avoid_confusing_test_parser() -> None:
434 t1 = None # type: Tuple[A, B]
435 t2 = None # type: Tuple[A, B, A]
436 [a, b] = None, None # type: (A, B)
437 [a1, b1] = None, None # type: Tuple[A, B]
438
439 reveal_type(a) # N: Revealed type is "__main__.A"
440 reveal_type(b) # N: Revealed type is "__main__.B"
441 reveal_type(a1) # N: Revealed type is "__main__.A"
442 reveal_type(b1) # N: Revealed type is "__main__.B"
443
444 if int():
445 [a, a] = t1 # E: Incompatible types in assignment (expression has type "B", variable has type "A")
446 [b, b] = t1 # E: Incompatible types in assignment (expression has type "A", variable has type "B")
447 [a, b, b] = t2 # E: Incompatible types in assignment (expression has type "A", variable has type "B")
448
449 [a, b] = t1
450 [a, b, a1] = t2
451
452 [a2, b2] = t1
453 reveal_type(a2) # N: Revealed type is "__main__.A"
454 reveal_type(b2) # N: Revealed type is "__main__.B"
455
456 class A: pass
457 class B: pass
458 [builtins fixtures/tuple.pyi]
459
460 [case testMultipleAssignmentWithInvalidNumberOfValues]
461 from typing import Tuple
462 t1: Tuple[A, A, A]
463 a: A
464
465 a, a = t1 # E: Too many values to unpack (2 expected, 3 provided)
466 a, a, a, a = t1 # E: Need more than 3 values to unpack (4 expected)
467
468 a, a, a = t1
469
470 class A: pass
471 [builtins fixtures/tuple.pyi]
472
473 [case testMultipleAssignmentWithTupleExpressionRvalue]
474 a: A
475 b: B
476
477 if int():
478 a, b = a, a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
479 if int():
480 a, b = b, a \
481 # E: Incompatible types in assignment (expression has type "B", variable has type "A") \
482 # E: Incompatible types in assignment (expression has type "A", variable has type "B")
483
484 if int():
485 a, b = a, b
486 if int():
487 a, a = a, a
488
489 class A: pass
490 class B: pass
491 [builtins fixtures/tuple.pyi]
492
493 [case testSubtypingInMultipleAssignment]
494 a: A
495 b: B
496
497 if int():
498 b, b = a, b # E: Incompatible types in assignment (expression has type "A", variable has type "B")
499 if int():
500 b, b = b, a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
501
502 if int():
503 a, b = b, b
504 if int():
505 b, a = b, b
506
507 class A: pass
508 class B(A): pass
509 [builtins fixtures/tuple.pyi]
510
511 [case testInitializationWithMultipleValues]
512 # flags: --no-strict-optional
513 a, b = None, None # type: (A, B)
514
515 a1, b1 = a, a # type: (A, B) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
516 a2, b2 = b, b # type: (A, B) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
517 a3, b3 = a # type: (A, B) # E: "A" object is not iterable
518 a4, b4 = None # type: (A, B) # E: "None" has no attribute "__iter__" (not iterable)
519 a5, b5 = a, b, a # type: (A, B) # E: Too many values to unpack (2 expected, 3 provided)
520
521 ax, bx = a, b # type: (A, B)
522
523 class A: pass
524 class B: pass
525 [builtins fixtures/tuple.pyi]
526
527 [case testMultipleAssignmentWithNonTupleRvalue]
528 a: A
529 b: B
530 def f(): pass
531
532 a, b = None # E: "None" object is not iterable
533 a, b = a # E: "A" object is not iterable
534 a, b = f # E: "Callable[[], Any]" object is not iterable
535
536 class A: pass
537 class B: pass
538 [builtins fixtures/tuple.pyi]
539
540 [case testMultipleAssignmentWithIndexedLvalues]
541 a: A
542 b: B
543 aa: AA
544 bb: BB
545
546 a[a], b[b] = a, bb # E: Incompatible types in assignment (expression has type "A", target has type "AA")
547 a[a], b[b] = aa, b # E: Incompatible types in assignment (expression has type "B", target has type "BB")
548 a[aa], b[b] = aa, bb # E: Invalid index type "AA" for "A"; expected type "A"
549 a[a], b[bb] = aa, bb # E: Invalid index type "BB" for "B"; expected type "B"
550 a[a], b[b] = aa, bb
551
552 class A:
553 def __setitem__(self, x: 'A', y: 'AA') -> None: pass
554 class B:
555 def __setitem__(self, x: 'B', y: 'BB') -> None: pass
556
557 class AA: pass
558 class BB: pass
559 [builtins fixtures/tuple.pyi]
560
561 [case testMultipleDeclarationWithParentheses]
562 # flags: --no-strict-optional
563 (a, b) = (None, None) # type: int, str
564 if int():
565 a = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
566 b = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
567 if int():
568 a = 1
569 b = ''
570 [builtins fixtures/tuple.pyi]
571
572 [case testMultipleAssignmentWithExtraParentheses]
573 a: A
574 b: B
575
576 if int():
577 (a, b) = (a, a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
578 if int():
579 (a, b) = (b, b) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
580 if int():
581 ((a), (b)) = ((a), (a)) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
582 if int():
583 ((a), (b)) = ((b), (b)) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
584 if int():
585 [a, b] = a, a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
586 if int():
587 [a, b] = b, b # E: Incompatible types in assignment (expression has type "B", variable has type "A")
588
589 if int():
590 (a, b) = (a, b)
591 if int():
592 ((a), (b)) = ((a), (b))
593 if int():
594 [a, b] = a, b
595
596 class A: pass
597 class B: pass
598 [builtins fixtures/tuple.pyi]
599
600 [case testMultipleAssignmentUsingSingleTupleType]
601 # flags: --no-strict-optional
602 from typing import Tuple
603 a, b = None, None # type: Tuple[int, str]
604 if int():
605 a = 1
606 if int():
607 b = ''
608 if int():
609 a = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
610 if int():
611 b = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
612 [builtins fixtures/tuple.pyi]
613
614 [case testMultipleAssignmentWithMixedVariables]
615 a = b, c = 1, 1
616 x, y = p, q = 1, 1
617 u, v, w = r, s = 1, 1 # E: Need more than 2 values to unpack (3 expected)
618 d, e = f, g, h = 1, 1 # E: Need more than 2 values to unpack (3 expected)
619 [builtins fixtures/tuple.pyi]
620
621 [case testUnpackAssignmentWithStarExpr]
622 a: A
623 b: list[B]
624 if int():
625 (a,) = (*b,) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
626
627 class A: pass
628 class B: pass
629
630
631 -- Assignment to starred expressions
632 -- ---------------------------------
633
634
635 [case testAssignmentToStarMissingAnnotation]
636 from typing import List
637 t = 1, 2
638 a, b, *c = 1, 2 # E: Need type annotation for "c" (hint: "c: list[<type>] = ...")
639 aa, bb, *cc = t # E: Need type annotation for "cc" (hint: "cc: list[<type>] = ...")
640 [builtins fixtures/list.pyi]
641
642 [case testAssignmentToStarAnnotation]
643 # flags: --no-strict-optional
644 from typing import List
645 li, lo = None, None # type: List[int], List[object]
646 a, b, *c = 1, 2 # type: int, int, List[int]
647 if int():
648 c = lo # E: Incompatible types in assignment (expression has type "list[object]", variable has type "list[int]")
649 if int():
650 c = li
651 [builtins fixtures/list.pyi]
652
653 [case testAssignmentToStarCount1]
654 from typing import List
655 ca: List[int]
656 c = [1]
657 if int():
658 a, b, *c = 1, # E: Need more than 1 value to unpack (2 expected)
659 if int():
660 a, b, *c = 1, 2
661 if int():
662 a, b, *c = 1, 2, 3
663 if int():
664 a, b, *c = 1, 2, 3, 4
665 [builtins fixtures/list.pyi]
666
667 [case testAssignmentToStarCount2]
668 from typing import List
669 ca: List[int]
670 t1 = 1,
671 t2 = 1, 2
672 t3 = 1, 2, 3
673 t4 = 1, 2, 3, 4
674 c = [1]
675 if int():
676 a, b, *c = t1 # E: Need more than 1 value to unpack (2 expected)
677 if int():
678 a, b, *c = t2
679 if int():
680 a, b, *c = t3
681 if int():
682 a, b, *c = t4
683 [builtins fixtures/list.pyi]
684
685 [case testAssignmentToStarFromAny]
686 from typing import Any, cast
687 class C: pass
688
689 a, c = cast(Any, 1), C()
690 p, *q = a
691 c = a
692 c = q
693 [case testAssignmentToComplexStar]
694 from typing import List
695 li: List[int]
696 if int():
697 a, *(li) = 1,
698 a, *(b, c) = 1, 2 # E: Need more than 1 value to unpack (2 expected)
699 if int():
700 a, *(b, c) = 1, 2, 3
701 if int():
702 a, *(b, c) = 1, 2, 3, 4 # E: Too many values to unpack (2 expected, 3 provided)
703 [builtins fixtures/list.pyi]
704
705 [case testAssignmentToStarFromTupleType]
706 from typing import List, Tuple
707 li: List[int]
708 la: List[A]
709 ta: Tuple[A, A, A]
710 if int():
711 a, *la = ta
712 if int():
713 a, *li = ta # E: List item 0 has incompatible type "A"; expected "int" \
714 # E: List item 1 has incompatible type "A"; expected "int"
715 if int():
716 a, *na = ta
717 if int():
718 na = la
719 na = a # E: Incompatible types in assignment (expression has type "A", variable has type "list[A]")
720
721 class A: pass
722 [builtins fixtures/list.pyi]
723
724 [case testAssignmentToStarFromTupleInference]
725 from typing import List
726 class A: pass
727 li: List[int]
728 la: List[A]
729 a, *l = A(), A()
730 if int():
731 l = li # E: Incompatible types in assignment (expression has type "list[int]", variable has type "list[A]")
732 if int():
733 l = la
734 [builtins fixtures/list.pyi]
735 [out]
736
737 [case testAssignmentToStarFromListInference]
738 from typing import List
739
740 class A: pass
741
742 li: List[int]
743 la: List[A]
744 a, *l = [A(), A()]
745 if int():
746 l = li # E: Incompatible types in assignment (expression has type "list[int]", variable has type "list[A]")
747 if int():
748 l = la
749 [builtins fixtures/list.pyi]
750 [out]
751
752 [case testAssignmentToStarFromTupleTypeInference]
753 from typing import List, Tuple
754 li: List[int]
755 la: List[A]
756 ta: Tuple[A, A, A]
757 a, *l = ta
758 if int():
759 l = li # E: Incompatible types in assignment (expression has type "list[int]", variable has type "list[A]")
760 if int():
761 l = la
762
763 class A: pass
764 [builtins fixtures/list.pyi]
765 [out]
766
767 [case testAssignmentToStarFromListTypeInference]
768 from typing import List
769 li: List[int]
770 la: List[A]
771 a, *l = la
772 if int():
773 l = li # E: Incompatible types in assignment (expression has type "list[int]", variable has type "list[A]")
774 if int():
775 l = la
776
777 class A: pass
778 [builtins fixtures/list.pyi]
779 [out]
780
781 [case testAssignmentToStarFromIterable]
782 from typing import List, Tuple, Iterable
783
784 class CustomIterable(Iterable[int]): pass
785
786 a: List[int]
787 b: Tuple[int, ...]
788 c: Tuple[int, int, int]
789 d: Iterable[int]
790 e: CustomIterable
791
792 a1, *a2 = a
793 b1, *b2 = b
794 c1, *c2 = c
795 d1, *d2 = d
796 e1, *e2 = e
797
798 reveal_type(a2) # N: Revealed type is "builtins.list[builtins.int]"
799 reveal_type(b2) # N: Revealed type is "builtins.list[builtins.int]"
800 reveal_type(c2) # N: Revealed type is "builtins.list[builtins.int]"
801 reveal_type(d2) # N: Revealed type is "builtins.list[builtins.int]"
802 reveal_type(e2) # N: Revealed type is "builtins.list[builtins.int]"
803 [builtins fixtures/tuple.pyi]
804
805 -- Nested tuple assignment
806 -- ----------------------------
807
808
809 [case testNestedTupleAssignment1]
810 a1: A
811 a2: A
812 b1: B
813 b2: B
814 c1: C
815 c2: C
816
817 if int():
818 a1, (b1, c1) = a2, (b2, c2)
819 if int():
820 a1, (a1, (b1, c1)) = a2, (a2, (b2, c2))
821 if int():
822 a1, (a1, (a1, b1)) = a1, (a1, (a1, c1)) # E: Incompatible types in assignment (expression has type "C", variable has type "B")
823
824 class A: pass
825 class B: pass
826 class C: pass
827 [builtins fixtures/tuple.pyi]
828
829 [case testNestedTupleAssignment2]
830 a1: A
831 a2: A
832 b1: B
833 b2: B
834 c1: C
835 c2: C
836 t = a1, b1
837
838 if int():
839 a2, b2 = t
840 if int():
841 (a2, b2), c2 = t, c1
842 if int():
843 (a2, c2), c2 = t, c1 # E: Incompatible types in assignment (expression has type "B", variable has type "C")
844 if int():
845 t, c2 = (a2, b2), c2
846 if int():
847 t, c2 = (a2, a2), c2 # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "tuple[A, B]")
848 if int():
849 t = a1, a1, a1 # E: Incompatible types in assignment (expression has type "tuple[A, A, A]", variable has type "tuple[A, B]")
850 if int():
851 t = a1 # E: Incompatible types in assignment (expression has type "A", variable has type "tuple[A, B]")
852 if int():
853 a2, a2, a2 = t # E: Need more than 2 values to unpack (3 expected)
854 if int():
855 a2, = t # E: Too many values to unpack (1 expected, 2 provided)
856 if int():
857 a2 = t # E: Incompatible types in assignment (expression has type "tuple[A, B]", variable has type "A")
858
859 class A: pass
860 class B: pass
861 class C: pass
862 [builtins fixtures/tuple.pyi]
863
864
865 -- Error messages
866 -- --------------
867
868
869 [case testTupleErrorMessages]
870 class A:
871 def __add__(self, x: 'A') -> 'A': pass
872 def f(x: 'A') -> None: pass
873
874 a: A
875
876 (a, a) + a # E: Unsupported operand types for + ("tuple[A, A]" and "A")
877 a + (a, a) # E: Unsupported operand types for + ("A" and "tuple[A, A]")
878 f((a, a)) # E: Argument 1 to "f" has incompatible type "tuple[A, A]"; expected "A"
879 (a, a).foo # E: "tuple[A, A]" has no attribute "foo"
880 [builtins fixtures/tuple.pyi]
881
882 [case testLargeTuplesInErrorMessages]
883
884 a: LongTypeName
885 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
886
887 class LongTypeName:
888 def __add__(self, x: 'LongTypeName') -> 'LongTypeName': pass
889 [builtins fixtures/tuple.pyi]
890 [out]
891 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]")
892
893
894 -- Tuple methods
895 -- -------------
896
897
898 [case testTupleMethods]
899 from typing import Tuple
900 t: Tuple[int, str]
901 i = 0
902 s = ''
903 b = bool()
904
905 if int():
906 s = t.__len__() # E: Incompatible types in assignment (expression has type "int", variable has type "str")
907 if int():
908 i = t.__str__() # E: Incompatible types in assignment (expression has type "str", variable has type "int")
909 if int():
910 i = s in t # E: Incompatible types in assignment (expression has type "bool", variable has type "int")
911 t.foo # E: "tuple[int, str]" has no attribute "foo"
912
913 if int():
914 i = t.__len__()
915 if int():
916 s = t.__str__()
917 if int():
918 b = s in t
919
920 [file builtins.py]
921 from typing import TypeVar, Generic
922 _T = TypeVar('_T')
923 class object:
924 def __init__(self) -> None: pass
925 class tuple(Generic[_T]):
926 def __len__(self) -> int: pass
927 def __str__(self) -> str: pass
928 def __contains__(self, o: object) -> bool: pass
929 class int: pass
930 class str: pass
931 class bool: pass
932 class type: pass
933 class function: pass
934 class dict: pass
935
936
937 -- For loop over tuple
938 -- -------------------
939
940
941 [case testForLoopOverTuple]
942 import typing
943 t = 1, 2
944 for x in t:
945 x = 1
946 x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
947 [builtins fixtures/for.pyi]
948
949 [case testForLoopOverEmptyTuple]
950 import typing
951 t = ()
952 for x in t: pass # E: Need type annotation for "x"
953 [builtins fixtures/for.pyi]
954
955 [case testForLoopOverNoneValuedTuple]
956 import typing
957 for x in None, None: pass
958 [builtins fixtures/for.pyi]
959
960 [case testForLoopOverTupleAndSubtyping]
961 import typing
962 class A: pass
963 class B(A): pass
964 for x in B(), A():
965 x = A()
966 x = B()
967 x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "A")
968 [builtins fixtures/for.pyi]
969
970 [case testTupleIterable]
971 from typing import Iterable, Optional, TypeVar
972
973 T = TypeVar("T")
974
975 def sum(iterable: Iterable[T], start: Optional[T] = None) -> T: pass
976
977 y = 'a'
978 x = sum((1,2))
979 if int():
980 y = x # E: Incompatible types in assignment (expression has type "int", variable has type "str")
981 [builtins fixtures/tuple.pyi]
982
983
984 -- Tuple as a base type
985 -- --------------------
986
987
988 [case testTupleBaseClass]
989 import m
990 [file m.pyi]
991 from typing import Tuple
992 class A(Tuple[int, str]):
993 def f(self, x: int) -> None:
994 a, b = 1, ''
995 if int():
996 a, b = self
997 b, a = self # Error
998 self.f('') # Error
999 [builtins fixtures/tuple.pyi]
1000 [out]
1001 tmp/m.pyi:7: error: Incompatible types in assignment (expression has type "int", variable has type "str")
1002 tmp/m.pyi:7: error: Incompatible types in assignment (expression has type "str", variable has type "int")
1003 tmp/m.pyi:8: error: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
1004
1005 [case testValidTupleBaseClass2]
1006 from typing import Tuple
1007 class A(Tuple[int, str]): pass
1008
1009 x, y = A()
1010 reveal_type(x) # N: Revealed type is "builtins.int"
1011 reveal_type(y) # N: Revealed type is "builtins.str"
1012
1013 x1 = A()[0] # type: int
1014 x2 = A()[1] # type: int # E: Incompatible types in assignment (expression has type "str", variable has type "int")
1015 A()[2] # E: Tuple index out of range
1016
1017 class B(Tuple[int, ...]): pass
1018
1019 z1 = B()[0] # type: int
1020 z2 = B()[1] # type: str # E: Incompatible types in assignment (expression has type "int", variable has type "str")
1021 B()[100]
1022 [builtins fixtures/tuple.pyi]
1023 [out]
1024
1025 [case testValidTupleBaseClass]
1026 from typing import Tuple
1027 class A(tuple): pass
1028 [builtins fixtures/tuple.pyi]
1029 [out]
1030
1031 [case testTupleBaseClass2]
1032 import m
1033 [file m.pyi]
1034 from typing import Tuple
1035 a: A
1036 class A(Tuple[int, str]): pass
1037 x, y = a
1038 x() # E: "int" not callable
1039 y() # E: "str" not callable
1040 [builtins fixtures/tuple.pyi]
1041 [out]
1042
1043 [case testGenericClassWithTupleBaseClass]
1044 from typing import TypeVar, Generic, Tuple
1045 T = TypeVar('T')
1046 class Test(Generic[T], Tuple[T]): pass
1047 x = Test() # type: Test[int]
1048 reveal_type(x) # N: Revealed type is "tuple[builtins.int, fallback=__main__.Test[builtins.int]]"
1049 [builtins fixtures/tuple.pyi]
1050 [out]
1051
1052
1053 -- Variable-length tuples (Tuple[t, ...] with literal '...')
1054 -- ---------------------------------------------------------
1055
1056
1057 [case testIndexingVariableLengthTuple]
1058 from typing import Tuple
1059 x = () # type: Tuple[str, ...]
1060 n = 5
1061 x[n]() # E: "str" not callable
1062 x[3]() # E: "str" not callable
1063 [builtins fixtures/tuple.pyi]
1064
1065 [case testSubtypingVariableLengthTuple]
1066 from typing import Tuple
1067 class A: pass
1068 class B(A): pass
1069 def fa(t: Tuple[A, ...]) -> None: pass
1070 def fb(t: Tuple[B, ...]) -> None: pass
1071 ta = () # type: Tuple[A, ...]
1072 tb = () # type: Tuple[B, ...]
1073 fa(ta)
1074 fa(tb)
1075 fb(tb)
1076 fb(ta) # E: Argument 1 to "fb" has incompatible type "tuple[A, ...]"; expected "tuple[B, ...]"
1077 [builtins fixtures/tuple.pyi]
1078
1079 [case testSubtypingFixedAndVariableLengthTuples]
1080 from typing import Tuple
1081 class A: pass
1082 class B(A): pass
1083 def fa(t: Tuple[A, ...]) -> None: pass
1084 def fb(t: Tuple[B, ...]) -> None: pass
1085 aa = (A(), A())
1086 ab = (A(), B())
1087 bb = (B(), B())
1088 fa(aa)
1089 fa(ab)
1090 fa(bb)
1091 fb(bb)
1092 fb(ab) # E: Argument 1 to "fb" has incompatible type "tuple[A, B]"; expected "tuple[B, ...]"
1093 fb(aa) # E: Argument 1 to "fb" has incompatible type "tuple[A, A]"; expected "tuple[B, ...]"
1094 [builtins fixtures/tuple.pyi]
1095
1096 [case testSubtypingTupleIsContainer]
1097 from typing import Container
1098 a: Container[str]
1099 a = ()
1100 [typing fixtures/typing-full.pyi]
1101 [builtins fixtures/tuple.pyi]
1102
1103 [case testSubtypingTupleIsSized]
1104 from typing import Sized
1105 a: Sized
1106 a = ()
1107 [typing fixtures/typing-medium.pyi]
1108 [builtins fixtures/tuple.pyi]
1109
1110 [case testTupleWithStarExpr1]
1111
1112 a = (1, 2)
1113 b = (*a, '')
1114 reveal_type(b) # N: Revealed type is "tuple[builtins.int, builtins.int, builtins.str]"
1115 [builtins fixtures/tuple.pyi]
1116
1117 [case testTupleWithStarExpr2]
1118 a = [1]
1119 b = (0, *a)
1120 reveal_type(b) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1121 [builtins fixtures/tuple.pyi]
1122
1123 [case testTupleWithStarExpr2Precise]
1124 # flags: --enable-incomplete-feature=PreciseTupleTypes
1125 a = [1]
1126 b = (0, *a)
1127 reveal_type(b) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.int, ...]]]"
1128 [builtins fixtures/tuple.pyi]
1129
1130 [case testTupleWithStarExpr3]
1131 a = ['']
1132 b = (0, *a)
1133 reveal_type(b) # N: Revealed type is "builtins.tuple[builtins.object, ...]"
1134 c = (*a, '')
1135 reveal_type(c) # N: Revealed type is "builtins.tuple[builtins.str, ...]"
1136 [builtins fixtures/tuple.pyi]
1137
1138 [case testTupleWithStarExpr3Precise]
1139 # flags: --enable-incomplete-feature=PreciseTupleTypes
1140 a = ['']
1141 b = (0, *a)
1142 reveal_type(b) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.str, ...]]]"
1143 c = (*a, '')
1144 reveal_type(c) # N: Revealed type is "tuple[Unpack[builtins.tuple[builtins.str, ...]], builtins.str]"
1145 [builtins fixtures/tuple.pyi]
1146
1147 [case testTupleWithStarExpr4]
1148 a = (1, 1, 'x', 'x')
1149 b = (1, 'x')
1150 a = (0, *b, '')
1151 [builtins fixtures/tuple.pyi]
1152
1153 [case testUnpackSyntaxError]
1154 *foo # E: can't use starred expression here
1155 [builtins fixtures/tuple.pyi]
1156
1157 [case testUnpackBases]
1158 class A: ...
1159 class B: ...
1160 bases = (A, B)
1161 class C(*bases): ... # E: Invalid base class
1162 [builtins fixtures/tuple.pyi]
1163
1164 [case testTupleMeetTupleAny]
1165 from typing import Union, Tuple
1166 class A: pass
1167 class B: pass
1168
1169 def f(x: Union[B, Tuple[A, A]]) -> None:
1170 if isinstance(x, tuple):
1171 reveal_type(x) # N: Revealed type is "tuple[__main__.A, __main__.A]"
1172 else:
1173 reveal_type(x) # N: Revealed type is "__main__.B"
1174
1175 def g(x: Union[str, Tuple[str, str]]) -> None:
1176 if isinstance(x, tuple):
1177 reveal_type(x) # N: Revealed type is "tuple[builtins.str, builtins.str]"
1178 else:
1179 reveal_type(x) # N: Revealed type is "builtins.str"
1180
1181 [builtins fixtures/tuple.pyi]
1182 [out]
1183
1184 [case testTupleMeetTupleAnyComplex]
1185 from typing import Tuple, Union
1186
1187 Pair = Tuple[int, int]
1188 Variant = Union[int, Pair]
1189 def tuplify(v: Variant) -> None:
1190 reveal_type(v) # N: Revealed type is "builtins.int | tuple[builtins.int, builtins.int]"
1191 if not isinstance(v, tuple):
1192 reveal_type(v) # N: Revealed type is "builtins.int"
1193 v = (v, v)
1194 reveal_type(v) # N: Revealed type is "tuple[builtins.int, builtins.int]"
1195 reveal_type(v) # N: Revealed type is "tuple[builtins.int, builtins.int]"
1196 reveal_type(v[0]) # N: Revealed type is "builtins.int"
1197
1198 Pair2 = Tuple[int, str]
1199 Variant2 = Union[int, Pair2]
1200 def tuplify2(v: Variant2) -> None:
1201 if isinstance(v, tuple):
1202 reveal_type(v) # N: Revealed type is "tuple[builtins.int, builtins.str]"
1203 else:
1204 reveal_type(v) # N: Revealed type is "builtins.int"
1205 [builtins fixtures/tuple.pyi]
1206 [out]
1207
1208 [case testTupleMeetTupleAnyAfter]
1209 from typing import Tuple, Union
1210
1211 def good(blah: Union[Tuple[int, int], int]) -> None:
1212 reveal_type(blah) # N: Revealed type is "tuple[builtins.int, builtins.int] | builtins.int"
1213 if isinstance(blah, tuple):
1214 reveal_type(blah) # N: Revealed type is "tuple[builtins.int, builtins.int]"
1215 reveal_type(blah) # N: Revealed type is "tuple[builtins.int, builtins.int] | builtins.int"
1216 [builtins fixtures/tuple.pyi]
1217 [out]
1218
1219 [case testTupleMeetTupleVariable]
1220 from typing import Tuple, TypeVar, Generic, Union
1221 T = TypeVar('T')
1222
1223 class A: pass
1224 class B1(A): pass
1225 class B2(A): pass
1226 class C: pass
1227
1228 x: Tuple[A, ...]
1229 y: Tuple[Union[B1, C], Union[B2, C]]
1230
1231 def g(x: T) -> Tuple[T, T]:
1232 return (x, x)
1233
1234 z = 1
1235 x, y = g(z) # E: Argument 1 to "g" has incompatible type "int"; expected "tuple[B1, B2]"
1236 [builtins fixtures/tuple.pyi]
1237 [out]
1238
1239 [case testFixedTupleJoinVarTuple]
1240 from typing import Tuple, TypeVar
1241
1242 class A: pass
1243 class B(A): pass
1244
1245 fixtup: Tuple[B, B]
1246
1247 T = TypeVar("T")
1248 def join(x: T, y: T) -> T: ...
1249
1250 vartup_b: Tuple[B, ...]
1251 reveal_type(join(fixtup, vartup_b)) # N: Revealed type is "builtins.tuple[__main__.B, ...]"
1252 reveal_type(join(vartup_b, fixtup)) # N: Revealed type is "builtins.tuple[__main__.B, ...]"
1253
1254 vartup_a: Tuple[A, ...]
1255 reveal_type(join(fixtup, vartup_a)) # N: Revealed type is "builtins.tuple[__main__.A, ...]"
1256 reveal_type(join(vartup_a, fixtup)) # N: Revealed type is "builtins.tuple[__main__.A, ...]"
1257
1258 [builtins fixtures/tuple.pyi]
1259 [out]
1260
1261 [case testFixedTupleJoinList]
1262 from typing import Tuple, List, TypeVar
1263
1264 class A: pass
1265 class B(A): pass
1266
1267 fixtup: Tuple[B, B]
1268
1269 T = TypeVar("T")
1270 def join(x: T, y: T) -> T: ...
1271
1272 lst_b: List[B]
1273 reveal_type(join(fixtup, lst_b)) # N: Revealed type is "typing.Sequence[__main__.B]"
1274 reveal_type(join(lst_b, fixtup)) # N: Revealed type is "typing.Sequence[__main__.B]"
1275
1276 lst_a: List[A]
1277 reveal_type(join(fixtup, lst_a)) # N: Revealed type is "typing.Sequence[__main__.A]"
1278 reveal_type(join(lst_a, fixtup)) # N: Revealed type is "typing.Sequence[__main__.A]"
1279
1280 [builtins fixtures/tuple.pyi]
1281 [out]
1282
1283 [case testEmptyTupleJoin]
1284 from typing import Tuple, List, TypeVar
1285
1286 class A: pass
1287
1288 empty = ()
1289
1290 T = TypeVar("T")
1291 def join(x: T, y: T) -> T: ...
1292
1293 fixtup: Tuple[A]
1294 reveal_type(join(fixtup, empty)) # N: Revealed type is "builtins.tuple[__main__.A, ...]"
1295 reveal_type(join(empty, fixtup)) # N: Revealed type is "builtins.tuple[__main__.A, ...]"
1296
1297 vartup: Tuple[A, ...]
1298 reveal_type(join(vartup, empty)) # N: Revealed type is "builtins.tuple[__main__.A, ...]"
1299 reveal_type(join(empty, vartup)) # N: Revealed type is "builtins.tuple[__main__.A, ...]"
1300
1301 lst: List[A]
1302 reveal_type(join(empty, lst)) # N: Revealed type is "typing.Sequence[__main__.A]"
1303 reveal_type(join(lst, empty)) # N: Revealed type is "typing.Sequence[__main__.A]"
1304
1305 [builtins fixtures/tuple.pyi]
1306 [out]
1307
1308 [case testTupleSubclassJoin]
1309 from typing import Tuple, NamedTuple, TypeVar
1310
1311 class NTup(NamedTuple):
1312 a: bool
1313 b: bool
1314
1315 class SubTuple(Tuple[bool]): ...
1316 class SubVarTuple(Tuple[int, ...]): ...
1317
1318 ntup: NTup
1319 subtup: SubTuple
1320 vartup: SubVarTuple
1321
1322 T = TypeVar("T")
1323 def join(x: T, y: T) -> T: ...
1324
1325 reveal_type(join(ntup, vartup)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1326 reveal_type(join(subtup, vartup)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1327
1328 [builtins fixtures/tuple.pyi]
1329 [out]
1330
1331 [case testTupleJoinIrregular]
1332 from typing import Tuple, TypeVar
1333
1334 tup1: Tuple[bool, int]
1335 tup2: Tuple[bool]
1336
1337 T = TypeVar("T")
1338 def join(x: T, y: T) -> T: ...
1339
1340 reveal_type(join(tup1, tup2)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1341 reveal_type(join(tup2, tup1)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1342
1343 reveal_type(join(tup1, ())) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1344 reveal_type(join((), tup1)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1345
1346 reveal_type(join(tup2, ())) # N: Revealed type is "builtins.tuple[builtins.bool, ...]"
1347 reveal_type(join((), tup2)) # N: Revealed type is "builtins.tuple[builtins.bool, ...]"
1348
1349 [builtins fixtures/tuple.pyi]
1350 [out]
1351
1352 [case testTupleSubclassJoinIrregular]
1353 from typing import Tuple, NamedTuple, TypeVar
1354
1355 class NTup1(NamedTuple):
1356 a: bool
1357
1358 class NTup2(NamedTuple):
1359 a: bool
1360 b: bool
1361
1362 class SubTuple(Tuple[bool, int, int]): ...
1363
1364 tup1: NTup1
1365 tup2: NTup2
1366 subtup: SubTuple
1367
1368 T = TypeVar("T")
1369 def join(x: T, y: T) -> T: ...
1370
1371 reveal_type(join(tup1, tup2)) # N: Revealed type is "builtins.tuple[builtins.bool, ...]"
1372 reveal_type(join(tup2, tup1)) # N: Revealed type is "builtins.tuple[builtins.bool, ...]"
1373
1374 reveal_type(join(tup1, subtup)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1375 reveal_type(join(subtup, tup1)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1376
1377 reveal_type(join(tup2, subtup)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1378 reveal_type(join(subtup, tup2)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1379
1380 [builtins fixtures/tuple.pyi]
1381 [out]
1382
1383 [case testTupleWithUndersizedContext]
1384 a = ([1], 'x')
1385 if int():
1386 a = ([], 'x', 1) # E: Incompatible types in assignment (expression has type "tuple[list[Never], str, int]", variable has type "tuple[list[int], str]")
1387 [builtins fixtures/tuple.pyi]
1388
1389 [case testTupleWithOversizedContext]
1390 a = (1, [1], 'x')
1391 if int():
1392 a = (1, []) # E: Incompatible types in assignment (expression has type "tuple[int, list[int]]", variable has type "tuple[int, list[int], str]")
1393 [builtins fixtures/tuple.pyi]
1394
1395 [case testTupleWithoutContext]
1396 a = (1, []) # E: Need type annotation for "a"
1397 [builtins fixtures/tuple.pyi]
1398
1399 [case testTupleWithUnionContext]
1400 from typing import List, Union, Tuple
1401 def f() -> Union[int, Tuple[List[str]]]:
1402 return ([],)
1403 [builtins fixtures/tuple.pyi]
1404
1405 [case testTupleWithVariableSizedTupleContext]
1406 from typing import List, Tuple
1407 def f() -> Tuple[List[str], ...]:
1408 return ([],)
1409 [builtins fixtures/tuple.pyi]
1410
1411 [case testTupleWithoutArgs]
1412 from typing import Tuple
1413 def f(a: Tuple) -> None: pass
1414 f(())
1415 f((1,))
1416 f(('', ''))
1417 f(0) # E: Argument 1 to "f" has incompatible type "int"; expected "tuple[Any, ...]"
1418 [builtins fixtures/tuple.pyi]
1419
1420 [case testTupleSingleton]
1421
1422 from typing import Tuple
1423 def f(a: Tuple[()]) -> None: pass
1424 f(())
1425 f((1,)) # E: Argument 1 to "f" has incompatible type "tuple[int]"; expected "tuple[()]"
1426 f(('', '')) # E: Argument 1 to "f" has incompatible type "tuple[str, str]"; expected "tuple[()]"
1427 f(0) # E: Argument 1 to "f" has incompatible type "int"; expected "tuple[()]"
1428 [builtins fixtures/tuple.pyi]
1429
1430 [case testNonliteralTupleIndex]
1431 t = (0, "")
1432 x = 0
1433 y = ""
1434 reveal_type(t[x]) # N: Revealed type is "builtins.int | builtins.str"
1435 t[y] # E: No overload variant of "__getitem__" of "tuple" matches argument type "str" \
1436 # N: Possible overload variants: \
1437 # N: def __getitem__(self, int, /) -> int | str \
1438 # N: def __getitem__(self, slice[int | None], /) -> tuple[int | str, ...]
1439
1440 [builtins fixtures/tuple.pyi]
1441
1442 [case testNonliteralTupleSlice]
1443 t = (0, "")
1444 x = 0
1445 y = ""
1446 reveal_type(t[x:]) # N: Revealed type is "builtins.tuple[builtins.int | builtins.str, ...]"
1447 t[y:] # E: Invalid index type "slice[str, None, None]" for "tuple[int, str]"; expected type "slice[int | None]"
1448 [builtins fixtures/tuple.pyi]
1449
1450 [case testTupleSliceStepZeroNoCrash]
1451 # This was crashing: https://github.com/python/mypy/issues/18062
1452 # TODO: emit better error when 0 is used for step
1453 ()[::0] # E: Ambiguous slice of a variadic tuple
1454 [builtins fixtures/tuple.pyi]
1455
1456 [case testInferTupleTypeFallbackAgainstInstance]
1457 from typing import TypeVar, Generic, Tuple
1458 T = TypeVar('T')
1459
1460 class Base(Generic[T]): pass
1461 def f(x: Base[T]) -> T: pass
1462
1463 class DT(Tuple[str, str], Base[int]):
1464 pass
1465
1466 reveal_type(f(DT())) # N: Revealed type is "builtins.int"
1467
1468 [builtins fixtures/tuple.pyi]
1469 [out]
1470
1471 [case testTypeTupleClassmethod]
1472 from typing import Tuple, Type
1473
1474 class C(Tuple[int, str]):
1475 @classmethod
1476 def f(cls) -> None: pass
1477
1478 t: Type[C]
1479 t.g() # E: "type[C]" has no attribute "g"
1480 t.f()
1481 [builtins fixtures/classmethod.pyi]
1482
1483 [case testTypeTupleCall]
1484 from typing import Tuple
1485
1486 def foo(o: CallableTuple) -> int:
1487 reveal_type(o) # N: Revealed type is "tuple[builtins.str, builtins.int, fallback=__main__.CallableTuple]"
1488 return o(1, 2)
1489
1490 class CallableTuple(Tuple[str, int]):
1491 def __call__(self, n: int, m: int) -> int:
1492 return n
1493 [builtins fixtures/tuple.pyi]
1494
1495 [case testTypeTupleGenericCall]
1496 from typing import Generic, Tuple, TypeVar
1497
1498 T = TypeVar('T')
1499
1500 def foo(o: CallableTuple[int]) -> int:
1501 reveal_type(o) # N: Revealed type is "tuple[builtins.str, builtins.int, fallback=__main__.CallableTuple[builtins.int]]"
1502 reveal_type(o.count(3)) # N: Revealed type is "builtins.int"
1503 return o(1, 2)
1504
1505 class CallableTuple(Tuple[str, T]):
1506 def __call__(self, n: int, m: int) -> int:
1507 return n
1508 [builtins fixtures/tuple.pyi]
1509
1510 [case testTupleCompatibleWithSequence]
1511 from typing import Sequence
1512 s: Sequence[str]
1513 s = tuple()
1514 reveal_type(s) # N: Revealed type is "builtins.tuple[builtins.str, ...]"
1515
1516 [builtins fixtures/tuple.pyi]
1517
1518 [case testTupleInstanceCompatibleWithIterable]
1519 from typing import Iterable, Tuple
1520 x: Iterable[int] = ()
1521 y: Tuple[int, ...] = (1, 2, 3)
1522 x = y
1523 reveal_type(x) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
1524
1525 [builtins fixtures/tuple.pyi]
1526
1527 [case testTupleTypeCompatibleWithIterable]
1528 from typing import Iterable, Tuple
1529 x: Iterable[int] = ()
1530 y: Tuple[int, int] = (1, 2)
1531 x = y
1532 reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int]"
1533 [builtins fixtures/tuple.pyi]
1534
1535 [case testTupleOverlapDifferentTuples]
1536 # flags: --warn-unreachable
1537 from typing import Optional, Tuple
1538 class A: pass
1539 class B: pass
1540
1541 def f1(possibles: Tuple[int, Tuple[A]], x: Optional[Tuple[B]]):
1542 if x in possibles:
1543 reveal_type(x) # E: Statement is unreachable
1544 else:
1545 reveal_type(x) # N: Revealed type is "tuple[__main__.B] | None"
1546
1547 class AA(A): pass
1548
1549 def f2(possibles: Tuple[int, Tuple[A]], x: Optional[Tuple[AA]]):
1550 if x in possibles:
1551 reveal_type(x) # N: Revealed type is "tuple[__main__.AA]"
1552 else:
1553 reveal_type(x) # N: Revealed type is "tuple[__main__.AA] | None"
1554
1555 [builtins fixtures/tuple.pyi]
1556
1557 [case testUnionOfTupleIndex]
1558 from typing import Union, Tuple
1559
1560 tup: Union[Tuple[int, str], Tuple[int, int, str]]
1561 reveal_type(tup[0]) # N: Revealed type is "builtins.int"
1562 reveal_type(tup[1]) # N: Revealed type is "builtins.str | builtins.int"
1563 reveal_type(tup[2]) # E: Tuple index out of range \
1564 # N: Revealed type is "Any | builtins.str"
1565 reveal_type(tup[:]) # N: Revealed type is "tuple[builtins.int, builtins.str] | tuple[builtins.int, builtins.int, builtins.str]"
1566
1567 [builtins fixtures/tuple.pyi]
1568
1569 [case testUnionOfTupleIndexMixed]
1570 from typing import Union, Tuple, List
1571
1572 tup: Union[Tuple[int, str], List[int]]
1573 reveal_type(tup[0]) # N: Revealed type is "builtins.int"
1574 reveal_type(tup[1]) # N: Revealed type is "builtins.str | builtins.int"
1575 reveal_type(tup[2]) # E: Tuple index out of range \
1576 # N: Revealed type is "Any | builtins.int"
1577 reveal_type(tup[:]) # N: Revealed type is "tuple[builtins.int, builtins.str] | builtins.list[builtins.int]"
1578
1579 [builtins fixtures/tuple.pyi]
1580
1581 [case testFixedLengthTupleConcatenation]
1582 a = (1, "foo", 3)
1583 b = ("bar", 7)
1584
1585 reveal_type(a + b) # N: Revealed type is "tuple[builtins.int, builtins.str, builtins.int, builtins.str, builtins.int]"
1586
1587 [builtins fixtures/tuple.pyi]
1588
1589 [case testAssigningWithLongTupleInitializer]
1590 from typing import Tuple
1591
1592 # long initializer assignment with few mismatches
1593 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) \
1594 # N: Expression tuple item 8 has type "str"; "int" expected; \
1595 # N: Expression tuple item 9 has type "str"; "int" expected; \
1596 # N: Expression tuple item 10 has type "str"; "int" expected;
1597
1598 # long initializer assignment with more mismatches
1599 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) \
1600 # N: Expression tuple item 8 has type "str"; "int" expected; \
1601 # N: Expression tuple item 9 has type "str"; "int" expected; \
1602 # N: Expression tuple item 10 has type "str"; "int" expected;
1603
1604 # short tuple initializer assignment
1605 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, ...]")
1606
1607 # long initializer assignment with few mismatches, no ellipsis
1608 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) \
1609 # N: Expression tuple item 10 has type "str"; "int" expected; \
1610 # N: Expression tuple item 11 has type "str"; "int" expected;
1611
1612 # long initializer assignment with more mismatches, no ellipsis
1613 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) \
1614 # N: Expression tuple item 8 has type "str"; "int" expected; \
1615 # N: Expression tuple item 9 has type "str"; "int" expected; \
1616 # N: Expression tuple item 10 has type "str"; "int" expected;
1617
1618 # short tuple initializer assignment, no ellipsis
1619 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]")
1620
1621 # long initializer assignment with mismatched pairs
1622 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>]")
1623
1624 [builtins fixtures/tuple.pyi]
1625
1626 [case testPropertyLongTupleReturnTypeMismatchUnion]
1627 from typing import Tuple, Union
1628 class A:
1629 a: str
1630 b: str
1631 c: str
1632 d: str
1633 e: str
1634 f: str
1635 g: Union[str, int]
1636 h: Union[str, float]
1637 i: Union[str, None]
1638 j: Union[str, None]
1639 k: Union[str, None]
1640 l: Union[str, None]
1641
1642 @property
1643 def x(self) -> Tuple[str, str, str, str, str, str, str, str, str, str, str, str]:
1644 return (
1645 self.a,
1646 self.b,
1647 self.c,
1648 self.d,
1649 self.e,
1650 self.f,
1651 self.g,
1652 self.h,
1653 self.i,
1654 self.j,
1655 self.k,
1656 self.l,
1657 )
1658 [out]
1659 main:18: error: Incompatible return value type (6 tuple items are incompatible; 3 items are omitted)
1660 main:18: note: Expression tuple item 6 has type "str | int"; "str" expected;
1661 main:18: note: Expression tuple item 7 has type "str | float"; "str" expected;
1662 main:18: note: Expression tuple item 8 has type "str | None"; "str" expected;
1663 [builtins fixtures/property.pyi]
1664
1665 [case testPropertyLongTupleReturnTypeMismatchUnionWiderExpected]
1666 from typing import Tuple, Union
1667 class A:
1668 a: str
1669 b: str
1670 c: str
1671 d: str
1672 e: str
1673 f: str
1674 g: str
1675 h: str
1676 i: str
1677 j: str
1678 k: str
1679 l: Union[float, int]
1680
1681 @property
1682 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]:
1683 return (
1684 self.a,
1685 self.b,
1686 self.c,
1687 self.d,
1688 self.e,
1689 self.f,
1690 self.g,
1691 self.h,
1692 self.i,
1693 self.j,
1694 self.k,
1695 self.l,
1696 )
1697 [out]
1698 main:18: error: Incompatible return value type (2 tuple items are incompatible)
1699 main:18: note: Expression tuple item 2 has type "str"; "int" expected;
1700 main:18: note: Expression tuple item 11 has type "float | int"; "str" expected;
1701 [builtins fixtures/property.pyi]
1702
1703 [case testTupleWithStarExpr]
1704 from typing import Tuple, List
1705 points = (1, "test") # type: Tuple[int, str]
1706 x, y, z = *points, 0
1707 reveal_type(x) # N: Revealed type is "builtins.int"
1708 reveal_type(y) # N: Revealed type is "builtins.str"
1709 reveal_type(z) # N: Revealed type is "builtins.int"
1710
1711 points2 = [1,2]
1712 x2, y2, z2= *points2, "test"
1713
1714 reveal_type(x2) # N: Revealed type is "builtins.int"
1715 reveal_type(y2) # N: Revealed type is "builtins.int"
1716 reveal_type(z2) # N: Revealed type is "builtins.str"
1717
1718 x3, x4, y3, y4, z3 = *points, *points2, "test"
1719
1720 reveal_type(x3) # N: Revealed type is "builtins.int"
1721 reveal_type(x4) # N: Revealed type is "builtins.str"
1722 reveal_type(y3) # N: Revealed type is "builtins.int"
1723 reveal_type(y4) # N: Revealed type is "builtins.int"
1724 reveal_type(z3) # N: Revealed type is "builtins.str"
1725
1726 x5, x6, y5, y6, z4 = *points2, *points2, "test"
1727
1728 reveal_type(x5) # N: Revealed type is "builtins.int"
1729 reveal_type(x6) # N: Revealed type is "builtins.int"
1730 reveal_type(y5) # N: Revealed type is "builtins.int"
1731 reveal_type(y6) # N: Revealed type is "builtins.int"
1732 reveal_type(z4) # N: Revealed type is "builtins.str"
1733
1734 points3 = ["test1", "test2"]
1735 x7, x8, y7, y8 = *points2, *points3 # E: Contiguous iterable with same type expected
1736
1737 x9, y9, x10, y10, z5 = *points2, 1, *points2 # E: Contiguous iterable with same type expected
1738 [builtins fixtures/tuple.pyi]
1739
1740 [case testAssignEmpty]
1741 () = []
1742
1743 [case testAssignEmptyBogus]
1744 () = 1 # E: "int" object is not iterable
1745 [builtins fixtures/tuple.pyi]
1746
1747 [case testMultiplyTupleByIntegerLiteral]
1748 from typing import Tuple
1749 t = ('',) * 2
1750 reveal_type(t) # N: Revealed type is "tuple[builtins.str, builtins.str]"
1751 t2 = ('',) * -1
1752 reveal_type(t2) # N: Revealed type is "tuple[()]"
1753 t3 = ('', 1) * 2
1754 reveal_type(t3) # N: Revealed type is "tuple[builtins.str, builtins.int, builtins.str, builtins.int]"
1755 def f() -> Tuple[str, ...]:
1756 return ('', )
1757 reveal_type(f() * 2) # N: Revealed type is "builtins.tuple[builtins.str, ...]"
1758 [builtins fixtures/tuple.pyi]
1759
1760 [case testEmptyTupleTypeRepr]
1761 from typing import Tuple
1762
1763 def f() -> Tuple[()]: ...
1764
1765 reveal_type(f) # N: Revealed type is "def () -> tuple[()]"
1766 reveal_type(f()) # N: Revealed type is "tuple[()]"
1767 [builtins fixtures/tuple.pyi]
1768
1769 [case testMultiplyTupleByIntegerLiteralReverse]
1770 from typing import Tuple
1771 t = 2 * ('',)
1772 reveal_type(t) # N: Revealed type is "tuple[builtins.str, builtins.str]"
1773 t2 = -1 * ('',)
1774 reveal_type(t2) # N: Revealed type is "tuple[()]"
1775 t3 = 2 * ('', 1)
1776 reveal_type(t3) # N: Revealed type is "tuple[builtins.str, builtins.int, builtins.str, builtins.int]"
1777 def f() -> Tuple[str, ...]:
1778 return ('', )
1779 reveal_type(2 * f()) # N: Revealed type is "builtins.tuple[builtins.str, ...]"
1780 [builtins fixtures/tuple.pyi]
1781
1782 [case testSingleUndefinedTypeAndTuple]
1783 from typing import Tuple
1784
1785 class Foo:
1786 ...
1787
1788 class Bar(aaaaaaaaaa): # E: Name "aaaaaaaaaa" is not defined
1789 ...
1790
1791 class FooBarTuple(Tuple[Foo, Bar]):
1792 ...
1793 [builtins fixtures/tuple.pyi]
1794
1795 [case testMultipleUndefinedTypeAndTuple]
1796 from typing import Tuple
1797
1798 class Foo(aaaaaaaaaa): # E: Name "aaaaaaaaaa" is not defined
1799 ...
1800
1801 class Bar(aaaaaaaaaa): # E: Name "aaaaaaaaaa" is not defined
1802 ...
1803
1804 class FooBarTuple(Tuple[Foo, Bar]):
1805 ...
1806 [builtins fixtures/tuple.pyi]
1807
1808
1809 [case testTupleOverloadZipAny]
1810 from typing import Any, Iterable, Iterator, Tuple, TypeVar, overload
1811
1812 T = TypeVar("T")
1813
1814 @overload
1815 def zip(__i: Iterable[T]) -> Iterator[Tuple[T]]: ...
1816 @overload
1817 def zip(*i: Iterable[Any]) -> Iterator[Tuple[Any, ...]]: ...
1818 def zip(i): ...
1819
1820 def g(t: Tuple):
1821 reveal_type(zip(*t)) # N: Revealed type is "typing.Iterator[builtins.tuple[Any, ...]]"
1822 reveal_type(zip(t)) # N: Revealed type is "typing.Iterator[tuple[Any]]"
1823 [builtins fixtures/tuple.pyi]
1824
1825 [case testTupleSubclassSlice]
1826 from typing import Tuple
1827
1828 class A: ...
1829
1830 class tuple_aa_subclass(Tuple[A, A]): ...
1831
1832 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")
1833 [builtins fixtures/tuple.pyi]
1834
1835 [case testTuplePassedParameters]
1836 from typing_extensions import Concatenate
1837
1838 def c(t: tuple[Concatenate[int, ...]]) -> None: # E: Cannot use "[int, VarArg(Any), KwArg(Any)]" for tuple, only for ParamSpec
1839 reveal_type(t) # N: Revealed type is "tuple[Any]"
1840 [builtins fixtures/tuple.pyi]