-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathcheck-tuples.test
More file actions
1840 lines (1475 loc) · 56.4 KB
/
check-tuples.test
File metadata and controls
1840 lines (1475 loc) · 56.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Normal assignment and subtyping
-- -------------------------------
[case testTupleAssignmentWithTupleTypes]
from typing import Tuple
t1: Tuple[A]
t2: Tuple[B]
t3: Tuple[A, A]
t4: Tuple[A, B]
t5: Tuple[B, A]
if int():
t1 = t2 # E: Incompatible types in assignment (expression has type "tuple[B]", variable has type "tuple[A]")
if int():
t1 = t3 # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "tuple[A]")
if int():
t3 = t1 # E: Incompatible types in assignment (expression has type "tuple[A]", variable has type "tuple[A, A]")
if int():
t3 = t4 # E: Incompatible types in assignment (expression has type "tuple[A, B]", variable has type "tuple[A, A]")
if int():
t3 = t5 # E: Incompatible types in assignment (expression has type "tuple[B, A]", variable has type "tuple[A, A]")
# Ok
if int():
t1 = t1
if int():
t2 = t2
if int():
t3 = t3
if int():
t4 = t4
if int():
t5 = t5
class A: pass
class B: pass
[builtins fixtures/tuple.pyi]
[case testTupleSubtyping]
from typing import Tuple
t1: Tuple[A, A]
t2: Tuple[A, B]
t3: Tuple[B, A]
if int():
t2 = t1 # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "tuple[A, B]")
t2 = t3 # E: Incompatible types in assignment (expression has type "tuple[B, A]", variable has type "tuple[A, B]")
t3 = t1 # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "tuple[B, A]")
t3 = t2 # E: Incompatible types in assignment (expression has type "tuple[A, B]", variable has type "tuple[B, A]")
t1 = t2
t1 = t3
class A: pass
class B(A): pass
[builtins fixtures/tuple.pyi]
[case testTupleCompatibilityWithOtherTypes]
# flags: --no-strict-optional
from typing import Tuple
a, o = None, None # type: (A, object)
t = None # type: Tuple[A, A]
if int():
a = t # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "A")
if int():
t = o # E: Incompatible types in assignment (expression has type "object", variable has type "tuple[A, A]")
if int():
t = a # E: Incompatible types in assignment (expression has type "A", variable has type "tuple[A, A]")
# TODO: callable types + tuples
# Ok
if int():
o = t
if int():
t = None
class A: pass
[builtins fixtures/tuple.pyi]
[case testNestedTupleTypes]
from typing import Tuple
t1: Tuple[A, Tuple[A, A]]
t2: Tuple[B, Tuple[B, B]]
if int():
t2 = t1 # E: Incompatible types in assignment (expression has type "tuple[A, tuple[A, A]]", variable has type "tuple[B, tuple[B, B]]")
if int():
t1 = t2
class A: pass
class B(A): pass
[builtins fixtures/tuple.pyi]
[case testNestedTupleTypes2]
from typing import Tuple
t1: Tuple[A, Tuple[A, A]]
t2: Tuple[B, Tuple[B, B]]
if int():
t2 = t1 # E: Incompatible types in assignment (expression has type "tuple[A, tuple[A, A]]", variable has type "tuple[B, tuple[B, B]]")
if int():
t1 = t2
class A: pass
class B(A): pass
[builtins fixtures/tuple.pyi]
[case testSubtypingWithTupleType]
from __future__ import annotations
from typing import Any, Tuple
tuple_aa: tuple[A, A]
Tuple_aa: Tuple[A, A]
tuple_obj: tuple[object, ...]
Tuple_obj: Tuple[object, ...]
tuple_obj_one: tuple[object]
Tuple_obj_one: Tuple[object]
tuple_obj_two: tuple[object, object]
Tuple_obj_two: Tuple[object, object]
tuple_any_implicit: tuple
Tuple_any_implicit: Tuple
tuple_any: tuple[Any, ...]
Tuple_any: Tuple[Any, ...]
tuple_any_one: tuple[Any]
Tuple_any_one: Tuple[Any]
tuple_any_two: tuple[Any, Any]
Tuple_any_two: Tuple[Any, Any]
def takes_tuple_aa(t: tuple[A, A]): ...
takes_tuple_aa(tuple_aa)
takes_tuple_aa(Tuple_aa)
takes_tuple_aa(tuple_obj) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[object, ...]"; expected "tuple[A, A]"
takes_tuple_aa(Tuple_obj) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[object, ...]"; expected "tuple[A, A]"
takes_tuple_aa(tuple_obj_one) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[object]"; expected "tuple[A, A]"
takes_tuple_aa(Tuple_obj_one) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[object]"; expected "tuple[A, A]"
takes_tuple_aa(tuple_obj_two) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[object, object]"; expected "tuple[A, A]"
takes_tuple_aa(Tuple_obj_two) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[object, object]"; expected "tuple[A, A]"
takes_tuple_aa(tuple_any_implicit)
takes_tuple_aa(Tuple_any_implicit)
takes_tuple_aa(tuple_any)
takes_tuple_aa(Tuple_any)
takes_tuple_aa(tuple_any_one) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[Any]"; expected "tuple[A, A]"
takes_tuple_aa(Tuple_any_one) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple[Any]"; expected "tuple[A, A]"
takes_tuple_aa(tuple_any_two)
takes_tuple_aa(Tuple_any_two)
def takes_tuple_any_implicit(t: tuple): ...
takes_tuple_any_implicit(tuple_aa)
takes_tuple_any_implicit(Tuple_aa)
takes_tuple_any_implicit(tuple_obj)
takes_tuple_any_implicit(Tuple_obj)
takes_tuple_any_implicit(tuple_obj_one)
takes_tuple_any_implicit(Tuple_obj_one)
takes_tuple_any_implicit(tuple_obj_two)
takes_tuple_any_implicit(Tuple_obj_two)
takes_tuple_any_implicit(tuple_any_implicit)
takes_tuple_any_implicit(Tuple_any_implicit)
takes_tuple_any_implicit(tuple_any)
takes_tuple_any_implicit(Tuple_any)
takes_tuple_any_implicit(tuple_any_one)
takes_tuple_any_implicit(Tuple_any_one)
takes_tuple_any_implicit(tuple_any_two)
takes_tuple_any_implicit(Tuple_any_two)
def takes_tuple_any_one(t: tuple[Any]): ...
takes_tuple_any_one(tuple_aa) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[A, A]"; expected "tuple[Any]"
takes_tuple_any_one(Tuple_aa) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[A, A]"; expected "tuple[Any]"
takes_tuple_any_one(tuple_obj) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[object, ...]"; expected "tuple[Any]"
takes_tuple_any_one(Tuple_obj) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[object, ...]"; expected "tuple[Any]"
takes_tuple_any_one(tuple_obj_one)
takes_tuple_any_one(Tuple_obj_one)
takes_tuple_any_one(tuple_obj_two) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[object, object]"; expected "tuple[Any]"
takes_tuple_any_one(Tuple_obj_two) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[object, object]"; expected "tuple[Any]"
takes_tuple_any_one(tuple_any_implicit)
takes_tuple_any_one(Tuple_any_implicit)
takes_tuple_any_one(tuple_any)
takes_tuple_any_one(Tuple_any)
takes_tuple_any_one(tuple_any_one)
takes_tuple_any_one(Tuple_any_one)
takes_tuple_any_one(tuple_any_two) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[Any, Any]"; expected "tuple[Any]"
takes_tuple_any_one(Tuple_any_two) # E: Argument 1 to "takes_tuple_any_one" has incompatible type "tuple[Any, Any]"; expected "tuple[Any]"
class A: pass
[builtins fixtures/tuple.pyi]
[case testSubtypingWithTupleTypeSubclass]
from __future__ import annotations
from typing import Any, Tuple
class A: ...
inst_tuple_aa: Tuple[A, A]
class tuple_aa_subclass(Tuple[A, A]): ...
inst_tuple_aa_subclass: tuple_aa_subclass
class tuple_any_subclass(Tuple[Any, ...]): ...
inst_tuple_any_subclass: tuple_any_subclass
class tuple_any_one_subclass(Tuple[Any]): ...
inst_tuple_any_one_subclass: tuple_any_one_subclass
class tuple_any_two_subclass(Tuple[Any, Any]): ...
inst_tuple_any_two_subclass: tuple_any_two_subclass
class tuple_obj_subclass(Tuple[object, ...]): ...
inst_tuple_obj_subclass: tuple_obj_subclass
class tuple_obj_one_subclass(Tuple[object]): ...
inst_tuple_obj_one_subclass: tuple_obj_one_subclass
class tuple_obj_two_subclass(Tuple[object, object]): ...
inst_tuple_obj_two_subclass: tuple_obj_two_subclass
def takes_tuple_aa(t: Tuple[A, A]): ...
takes_tuple_aa(inst_tuple_aa)
takes_tuple_aa(inst_tuple_aa_subclass)
takes_tuple_aa(inst_tuple_any_subclass)
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]"
takes_tuple_aa(inst_tuple_any_two_subclass)
takes_tuple_aa(inst_tuple_obj_subclass) # E: Argument 1 to "takes_tuple_aa" has incompatible type "tuple_obj_subclass"; expected "tuple[A, A]"
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]"
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]"
def takes_tuple_aa_subclass(t: tuple_aa_subclass): ...
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"
takes_tuple_aa_subclass(inst_tuple_aa_subclass)
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"
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"
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"
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"
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"
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"
[builtins fixtures/tuple.pyi]
[case testTupleInitializationWithNone]
# flags: --no-strict-optional
from typing import Tuple
t = None # type: Tuple[A, A]
t = None
class A: pass
[builtins fixtures/tuple.pyi]
-- Tuple expressions
-- -----------------
[case testTupleExpressions]
# flags: --no-strict-optional
from typing import Tuple
t1 = None # type: tuple
t2 = None # type: Tuple[A]
t3 = None # type: Tuple[A, B]
a, b, c = None, None, None # type: (A, B, C)
if int():
t2 = () # E: Incompatible types in assignment (expression has type "tuple[()]", variable has type "tuple[A]")
if int():
t2 = (a, a) # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "tuple[A]")
if int():
t3 = (a, a) # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "tuple[A, B]")
if int():
t3 = (b, b) # E: Incompatible types in assignment (expression has type "tuple[B, B]", variable has type "tuple[A, B]")
if int():
t3 = (a, b, a) # E: Incompatible types in assignment (expression has type "tuple[A, B, A]", variable has type "tuple[A, B]")
t1 = ()
t1 = (a,)
t2 = (a,)
t3 = (a, b)
t3 = (a, c)
t3 = (None, None)
class A: pass
class B: pass
class C(B): pass
[builtins fixtures/tuple.pyi]
[case testVoidValueInTuple]
import typing
def f() -> None: pass
(None, f()) # E: "f" does not return a value (it only ever returns None)
(f(), None) # E: "f" does not return a value (it only ever returns None)
[builtins fixtures/tuple.pyi]
-- Indexing
-- --------
[case testIndexingTuples]
from typing import Tuple
t1: Tuple[A, B]
t2: Tuple[A]
t3: Tuple[A, B, C, D, E]
a: A
b: B
x: Tuple[A, B, C]
y: Tuple[A, C, E]
n = 0
if int():
a = t1[1] # E: Incompatible types in assignment (expression has type "B", variable has type "A")
if int():
b = t1[0] # E: Incompatible types in assignment (expression has type "A", variable has type "B")
t1[2] # E: Tuple index out of range
t1[3] # E: Tuple index out of range
t2[1] # E: Tuple index out of range
reveal_type(t1[n]) # N: Revealed type is "__main__.A | __main__.B"
reveal_type(t3[n:]) # N: Revealed type is "builtins.tuple[__main__.A | __main__.B | __main__.C | __main__.D | __main__.E, ...]"
if int():
b = t1[(0)] # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
a = t1[0]
if int():
b = t1[1]
if int():
b = t1[-1]
if int():
a = t1[(0)]
if int():
b = t1[+1]
if int():
x = t3[0:3] # type (A, B, C)
if int():
y = t3[0:+5:2] # type (A, C, E)
if int():
x = t3[:-2] # type (A, B, C)
class A: pass
class B: pass
class C: pass
class D: pass
class E: pass
[builtins fixtures/tuple.pyi]
[case testIndexingTuplesWithNegativeIntegers]
from typing import Tuple
t1: Tuple[A, B]
t2: Tuple[A]
a: A
b: B
if int():
a = t1[-1] # E: Incompatible types in assignment (expression has type "B", variable has type "A")
if int():
b = t1[-2] # E: Incompatible types in assignment (expression has type "A", variable has type "B")
t1[-3] # E: Tuple index out of range
t1[-4] # E: Tuple index out of range
if int():
b = t2[(-1)] # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
a = t1[-2]
if int():
b = t1[-1]
if int():
a = t2[(-1)]
class A: pass
class B: pass
[builtins fixtures/tuple.pyi]
[case testAssigningToTupleItems]
from typing import Tuple
class A: pass
class B: pass
t: Tuple[A, B]
n = 0
t[0] = A() # E: Unsupported target for indexed assignment ("tuple[A, B]")
t[2] = A() # E: Unsupported target for indexed assignment ("tuple[A, B]")
t[n] = A() # E: Unsupported target for indexed assignment ("tuple[A, B]")
[builtins fixtures/tuple.pyi]
-- Multiple assignment
-- -------------------
[case testMultipleAssignmentWithTuples]
# flags: --no-strict-optional
from typing import Tuple
t1 = None # type: Tuple[A, B]
t2 = None # type: Tuple[A, B, A]
a, b = None, None # type: (A, B)
(a1, b1) = None, None # type: Tuple[A, B]
reveal_type(a1) # N: Revealed type is "__main__.A"
reveal_type(b1) # N: Revealed type is "__main__.B"
if int():
a, a = t1 # E: Incompatible types in assignment (expression has type "B", variable has type "A")
if int():
b, b = t1 # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
a, b, b = t2 # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
a, b = t1
if int():
a, b, a1 = t2
class A: pass
class B: pass
[builtins fixtures/tuple.pyi]
[case testMultipleAssignmentWithSquareBracketTuples]
# flags: --no-strict-optional
from typing import Tuple
def avoid_confusing_test_parser() -> None:
t1 = None # type: Tuple[A, B]
t2 = None # type: Tuple[A, B, A]
[a, b] = None, None # type: (A, B)
[a1, b1] = None, None # type: Tuple[A, B]
reveal_type(a) # N: Revealed type is "__main__.A"
reveal_type(b) # N: Revealed type is "__main__.B"
reveal_type(a1) # N: Revealed type is "__main__.A"
reveal_type(b1) # N: Revealed type is "__main__.B"
if int():
[a, a] = t1 # E: Incompatible types in assignment (expression has type "B", variable has type "A")
[b, b] = t1 # E: Incompatible types in assignment (expression has type "A", variable has type "B")
[a, b, b] = t2 # E: Incompatible types in assignment (expression has type "A", variable has type "B")
[a, b] = t1
[a, b, a1] = t2
[a2, b2] = t1
reveal_type(a2) # N: Revealed type is "__main__.A"
reveal_type(b2) # N: Revealed type is "__main__.B"
class A: pass
class B: pass
[builtins fixtures/tuple.pyi]
[case testMultipleAssignmentWithInvalidNumberOfValues]
from typing import Tuple
t1: Tuple[A, A, A]
a: A
a, a = t1 # E: Too many values to unpack (2 expected, 3 provided)
a, a, a, a = t1 # E: Need more than 3 values to unpack (4 expected)
a, a, a = t1
class A: pass
[builtins fixtures/tuple.pyi]
[case testMultipleAssignmentWithTupleExpressionRvalue]
a: A
b: B
if int():
a, b = a, a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
a, b = b, a \
# E: Incompatible types in assignment (expression has type "B", variable has type "A") \
# E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
a, b = a, b
if int():
a, a = a, a
class A: pass
class B: pass
[builtins fixtures/tuple.pyi]
[case testSubtypingInMultipleAssignment]
a: A
b: B
if int():
b, b = a, b # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
b, b = b, a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
a, b = b, b
if int():
b, a = b, b
class A: pass
class B(A): pass
[builtins fixtures/tuple.pyi]
[case testInitializationWithMultipleValues]
# flags: --no-strict-optional
a, b = None, None # type: (A, B)
a1, b1 = a, a # type: (A, B) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
a2, b2 = b, b # type: (A, B) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
a3, b3 = a # type: (A, B) # E: "A" object is not iterable
a4, b4 = None # type: (A, B) # E: "None" has no attribute "__iter__" (not iterable)
a5, b5 = a, b, a # type: (A, B) # E: Too many values to unpack (2 expected, 3 provided)
ax, bx = a, b # type: (A, B)
class A: pass
class B: pass
[builtins fixtures/tuple.pyi]
[case testMultipleAssignmentWithNonTupleRvalue]
a: A
b: B
def f(): pass
a, b = None # E: "None" object is not iterable
a, b = a # E: "A" object is not iterable
a, b = f # E: "Callable[[], Any]" object is not iterable
class A: pass
class B: pass
[builtins fixtures/tuple.pyi]
[case testMultipleAssignmentWithIndexedLvalues]
a: A
b: B
aa: AA
bb: BB
a[a], b[b] = a, bb # E: Incompatible types in assignment (expression has type "A", target has type "AA")
a[a], b[b] = aa, b # E: Incompatible types in assignment (expression has type "B", target has type "BB")
a[aa], b[b] = aa, bb # E: Invalid index type "AA" for "A"; expected type "A"
a[a], b[bb] = aa, bb # E: Invalid index type "BB" for "B"; expected type "B"
a[a], b[b] = aa, bb
class A:
def __setitem__(self, x: 'A', y: 'AA') -> None: pass
class B:
def __setitem__(self, x: 'B', y: 'BB') -> None: pass
class AA: pass
class BB: pass
[builtins fixtures/tuple.pyi]
[case testMultipleDeclarationWithParentheses]
# flags: --no-strict-optional
(a, b) = (None, None) # type: int, str
if int():
a = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
b = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
if int():
a = 1
b = ''
[builtins fixtures/tuple.pyi]
[case testMultipleAssignmentWithExtraParentheses]
a: A
b: B
if int():
(a, b) = (a, a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
(a, b) = (b, b) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
if int():
((a), (b)) = ((a), (a)) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
((a), (b)) = ((b), (b)) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
if int():
[a, b] = a, a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
[a, b] = b, b # E: Incompatible types in assignment (expression has type "B", variable has type "A")
if int():
(a, b) = (a, b)
if int():
((a), (b)) = ((a), (b))
if int():
[a, b] = a, b
class A: pass
class B: pass
[builtins fixtures/tuple.pyi]
[case testMultipleAssignmentUsingSingleTupleType]
# flags: --no-strict-optional
from typing import Tuple
a, b = None, None # type: Tuple[int, str]
if int():
a = 1
if int():
b = ''
if int():
a = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
if int():
b = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[builtins fixtures/tuple.pyi]
[case testMultipleAssignmentWithMixedVariables]
a = b, c = 1, 1
x, y = p, q = 1, 1
u, v, w = r, s = 1, 1 # E: Need more than 2 values to unpack (3 expected)
d, e = f, g, h = 1, 1 # E: Need more than 2 values to unpack (3 expected)
[builtins fixtures/tuple.pyi]
[case testUnpackAssignmentWithStarExpr]
a: A
b: list[B]
if int():
(a,) = (*b,) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
class A: pass
class B: pass
-- Assignment to starred expressions
-- ---------------------------------
[case testAssignmentToStarMissingAnnotation]
from typing import List
t = 1, 2
a, b, *c = 1, 2 # E: Need type annotation for "c" (hint: "c: list[<type>] = ...")
aa, bb, *cc = t # E: Need type annotation for "cc" (hint: "cc: list[<type>] = ...")
[builtins fixtures/list.pyi]
[case testAssignmentToStarAnnotation]
# flags: --no-strict-optional
from typing import List
li, lo = None, None # type: List[int], List[object]
a, b, *c = 1, 2 # type: int, int, List[int]
if int():
c = lo # E: Incompatible types in assignment (expression has type "list[object]", variable has type "list[int]")
if int():
c = li
[builtins fixtures/list.pyi]
[case testAssignmentToStarCount1]
from typing import List
ca: List[int]
c = [1]
if int():
a, b, *c = 1, # E: Need more than 1 value to unpack (2 expected)
if int():
a, b, *c = 1, 2
if int():
a, b, *c = 1, 2, 3
if int():
a, b, *c = 1, 2, 3, 4
[builtins fixtures/list.pyi]
[case testAssignmentToStarCount2]
from typing import List
ca: List[int]
t1 = 1,
t2 = 1, 2
t3 = 1, 2, 3
t4 = 1, 2, 3, 4
c = [1]
if int():
a, b, *c = t1 # E: Need more than 1 value to unpack (2 expected)
if int():
a, b, *c = t2
if int():
a, b, *c = t3
if int():
a, b, *c = t4
[builtins fixtures/list.pyi]
[case testAssignmentToStarFromAny]
from typing import Any, cast
class C: pass
a, c = cast(Any, 1), C()
p, *q = a
c = a
c = q
[case testAssignmentToComplexStar]
from typing import List
li: List[int]
if int():
a, *(li) = 1,
a, *(b, c) = 1, 2 # E: Need more than 1 value to unpack (2 expected)
if int():
a, *(b, c) = 1, 2, 3
if int():
a, *(b, c) = 1, 2, 3, 4 # E: Too many values to unpack (2 expected, 3 provided)
[builtins fixtures/list.pyi]
[case testAssignmentToStarFromTupleType]
from typing import List, Tuple
li: List[int]
la: List[A]
ta: Tuple[A, A, A]
if int():
a, *la = ta
if int():
a, *li = ta # E: List item 0 has incompatible type "A"; expected "int" \
# E: List item 1 has incompatible type "A"; expected "int"
if int():
a, *na = ta
if int():
na = la
na = a # E: Incompatible types in assignment (expression has type "A", variable has type "list[A]")
class A: pass
[builtins fixtures/list.pyi]
[case testAssignmentToStarFromTupleInference]
from typing import List
class A: pass
li: List[int]
la: List[A]
a, *l = A(), A()
if int():
l = li # E: Incompatible types in assignment (expression has type "list[int]", variable has type "list[A]")
if int():
l = la
[builtins fixtures/list.pyi]
[out]
[case testAssignmentToStarFromListInference]
from typing import List
class A: pass
li: List[int]
la: List[A]
a, *l = [A(), A()]
if int():
l = li # E: Incompatible types in assignment (expression has type "list[int]", variable has type "list[A]")
if int():
l = la
[builtins fixtures/list.pyi]
[out]
[case testAssignmentToStarFromTupleTypeInference]
from typing import List, Tuple
li: List[int]
la: List[A]
ta: Tuple[A, A, A]
a, *l = ta
if int():
l = li # E: Incompatible types in assignment (expression has type "list[int]", variable has type "list[A]")
if int():
l = la
class A: pass
[builtins fixtures/list.pyi]
[out]
[case testAssignmentToStarFromListTypeInference]
from typing import List
li: List[int]
la: List[A]
a, *l = la
if int():
l = li # E: Incompatible types in assignment (expression has type "list[int]", variable has type "list[A]")
if int():
l = la
class A: pass
[builtins fixtures/list.pyi]
[out]
[case testAssignmentToStarFromIterable]
from typing import List, Tuple, Iterable
class CustomIterable(Iterable[int]): pass
a: List[int]
b: Tuple[int, ...]
c: Tuple[int, int, int]
d: Iterable[int]
e: CustomIterable
a1, *a2 = a
b1, *b2 = b
c1, *c2 = c
d1, *d2 = d
e1, *e2 = e
reveal_type(a2) # N: Revealed type is "builtins.list[builtins.int]"
reveal_type(b2) # N: Revealed type is "builtins.list[builtins.int]"
reveal_type(c2) # N: Revealed type is "builtins.list[builtins.int]"
reveal_type(d2) # N: Revealed type is "builtins.list[builtins.int]"
reveal_type(e2) # N: Revealed type is "builtins.list[builtins.int]"
[builtins fixtures/tuple.pyi]
-- Nested tuple assignment
-- ----------------------------
[case testNestedTupleAssignment1]
a1: A
a2: A
b1: B
b2: B
c1: C
c2: C
if int():
a1, (b1, c1) = a2, (b2, c2)
if int():
a1, (a1, (b1, c1)) = a2, (a2, (b2, c2))
if int():
a1, (a1, (a1, b1)) = a1, (a1, (a1, c1)) # E: Incompatible types in assignment (expression has type "C", variable has type "B")
class A: pass
class B: pass
class C: pass
[builtins fixtures/tuple.pyi]
[case testNestedTupleAssignment2]
a1: A
a2: A
b1: B
b2: B
c1: C
c2: C
t = a1, b1
if int():
a2, b2 = t
if int():
(a2, b2), c2 = t, c1
if int():
(a2, c2), c2 = t, c1 # E: Incompatible types in assignment (expression has type "B", variable has type "C")
if int():
t, c2 = (a2, b2), c2
if int():
t, c2 = (a2, a2), c2 # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "tuple[A, B]")
if int():
t = a1, a1, a1 # E: Incompatible types in assignment (expression has type "tuple[A, A, A]", variable has type "tuple[A, B]")
if int():
t = a1 # E: Incompatible types in assignment (expression has type "A", variable has type "tuple[A, B]")
if int():
a2, a2, a2 = t # E: Need more than 2 values to unpack (3 expected)
if int():
a2, = t # E: Too many values to unpack (1 expected, 2 provided)
if int():
a2 = t # E: Incompatible types in assignment (expression has type "tuple[A, B]", variable has type "A")
class A: pass
class B: pass
class C: pass
[builtins fixtures/tuple.pyi]
-- Error messages
-- --------------
[case testTupleErrorMessages]
class A:
def __add__(self, x: 'A') -> 'A': pass
def f(x: 'A') -> None: pass
a: A
(a, a) + a # E: Unsupported operand types for + ("tuple[A, A]" and "A")
a + (a, a) # E: Unsupported operand types for + ("A" and "tuple[A, A]")
f((a, a)) # E: Argument 1 to "f" has incompatible type "tuple[A, A]"; expected "A"
(a, a).foo # E: "tuple[A, A]" has no attribute "foo"
[builtins fixtures/tuple.pyi]
[case testLargeTuplesInErrorMessages]
a: LongTypeName
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
class LongTypeName:
def __add__(self, x: 'LongTypeName') -> 'LongTypeName': pass
[builtins fixtures/tuple.pyi]
[out]
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]")
-- Tuple methods
-- -------------
[case testTupleMethods]
from typing import Tuple
t: Tuple[int, str]
i = 0
s = ''
b = bool()
if int():
s = t.__len__() # E: Incompatible types in assignment (expression has type "int", variable has type "str")
if int():
i = t.__str__() # E: Incompatible types in assignment (expression has type "str", variable has type "int")
if int():
i = s in t # E: Incompatible types in assignment (expression has type "bool", variable has type "int")
t.foo # E: "tuple[int, str]" has no attribute "foo"
if int():
i = t.__len__()
if int():
s = t.__str__()
if int():
b = s in t
[file builtins.py]
from typing import TypeVar, Generic
_T = TypeVar('_T')
class object:
def __init__(self) -> None: pass
class tuple(Generic[_T]):
def __len__(self) -> int: pass
def __str__(self) -> str: pass
def __contains__(self, o: object) -> bool: pass
class int: pass
class str: pass
class bool: pass
class type: pass
class function: pass
class dict: pass
-- For loop over tuple
-- -------------------
[case testForLoopOverTuple]
import typing
t = 1, 2
for x in t:
x = 1
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[builtins fixtures/for.pyi]
[case testForLoopOverEmptyTuple]
import typing
t = ()
for x in t: pass # E: Need type annotation for "x"
[builtins fixtures/for.pyi]
[case testForLoopOverNoneValuedTuple]
import typing
for x in None, None: pass
[builtins fixtures/for.pyi]
[case testForLoopOverTupleAndSubtyping]
import typing
class A: pass
class B(A): pass
for x in B(), A():
x = A()
x = B()
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "A")
[builtins fixtures/for.pyi]
[case testTupleIterable]
from typing import Iterable, Optional, TypeVar
T = TypeVar("T")
def sum(iterable: Iterable[T], start: Optional[T] = None) -> T: pass
y = 'a'
x = sum((1,2))
if int():
y = x # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[builtins fixtures/tuple.pyi]
-- Tuple as a base type
-- --------------------
[case testTupleBaseClass]
import m
[file m.pyi]
from typing import Tuple
class A(Tuple[int, str]):
def f(self, x: int) -> None:
a, b = 1, ''
if int():
a, b = self
b, a = self # Error
self.f('') # Error
[builtins fixtures/tuple.pyi]
[out]