-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathcheck-isinstance.test
2935 lines (2438 loc) · 78.1 KB
/
check-isinstance.test
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
[case testForcedAssignment]
x = 1 # type: object
y = 1
def f(): x, y # Prevent independent redefinition
y = x # E: Incompatible types in assignment (expression has type "object", variable has type "int")
x = 2
y = x
[builtins fixtures/tuple.pyi]
[case testJoinAny]
from typing import List, Any
x: List[Any]
def foo() -> List[int]: pass
def bar() -> List[str]: pass
if bool():
x = foo()
else:
x = bar()
x * 2
[builtins fixtures/list.pyi]
[case testGeneratorExpressionTypes]
class A: y = 1
x = [A()]
y = [x]
z = [1,2]
z = [a.y for b in y for a in b]
[builtins fixtures/list.pyi]
[case testIsinstanceNestedTuple]
from typing import Union, List, Tuple, Dict
def f(x: Union[int, str, List]) -> None:
if isinstance(x, (str, (int,))):
reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]"
x[1] # E: Value of type "Union[int, str]" is not indexable
else:
reveal_type(x) # N: Revealed type is "builtins.list[Any]"
x[1]
reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.list[Any]]"
if isinstance(x, (str, (list,))):
reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.list[Any]]"
x[1]
reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.list[Any]]"
[builtins fixtures/isinstancelist.pyi]
[case testClassAttributeInitialization]
class A:
x: int
def __init__(self) -> None:
self.y: int
z = self.x
w = self.y
[case testAssignmentSubtypes]
from typing import Union
def foo(x: Union[str, int]):
if isinstance(x, int):
x = 'a'
x + 'a'
z = x
y = [x]
y[0] + 'a'
# TODO: should we allow these two lines?
y + [1] # E: List item 0 has incompatible type "int"; expected "str"
z = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
x: int
y = [x]
[builtins fixtures/isinstancelist.pyi]
[case testFunctionDefaultArgs]
class A: pass
class B(A): y = 1
x = A()
def foo(x: A = B()):
x.y # E: "A" has no attribute "y"
[builtins fixtures/isinstance.pyi]
[case testIsinstanceFancyConditionals]
class A: pass
class B(A):
y = 1
x = A()
if isinstance(x, B):
x.y
while isinstance(x, B):
x.y
while isinstance(x, B):
x.y
x = B()
[builtins fixtures/isinstance.pyi]
[case testSubtypingWithAny]
class A:
y = 1
class B(A):
z = 1
def foo(): pass
x = A()
if int():
x = B()
x.z
x = foo()
x.z # E: "A" has no attribute "z"
x.y
[case testSingleMultiAssignment]
x = 'a'
(x,) = ('a',)
[case testUnionMultiAssignment]
from typing import Union
x: Union[int, str]
if int():
x = 1
x = 'a'
x + 1 # E: Unsupported operand types for + ("str" and "int")
x = 1
(x, y) = ('a', 1)
x + 1 # E: Unsupported operand types for + ("str" and "int")
[builtins fixtures/isinstancelist.pyi]
[case testUnionIfZigzag]
from typing import Union
def f(x: Union[int, str]) -> None:
if 1: # Without this, the assignment below could create a new variable "x" of type "int"
x = 1
if x:
x = 'a'
x = 1
x + 1
x + 1
[builtins fixtures/isinstancelist.pyi]
[case testTwoLoopsUnion]
from typing import Union
def foo() -> Union[int, str]: pass
def bar() -> None:
x = foo()
if isinstance(x, int):
return
while bool():
x + 'a'
while bool():
x = foo()
if bool():
return
x = 'a'
x + 'a'
[builtins fixtures/isinstancelist.pyi]
[case testComplicatedBlocks]
from typing import Union
def foo() -> Union[int, str]: pass
def bar() -> None:
x = foo()
if isinstance(x, int):
return
while bool():
x + 'a'
while bool():
x = foo()
if bool():
return
x = 'a'
x + 'a'
x = foo()
if isinstance(x, int):
return
while bool():
x + 'a'
while bool():
x + 'a' # E: Unsupported operand types for + ("int" and "str") \
# N: Left operand is of type "Union[int, str]"
x = foo()
if bool():
continue
x = 'a'
x = 'a'
x + 'a'
[builtins fixtures/isinstancelist.pyi]
[case testUnionTryExcept]
class A:
y = A()
class B(A):
z = 1
x = A()
def f(): x # Prevent redefinition of x
x = B()
x.z
try:
x.z
x = A()
x = B()
x.z
except:
pass
x.z # E: "A" has no attribute "z"
[case testUnionTryExcept2]
class A:
y = A()
class B(A):
z = 1
x = A()
try:
x.z # E: "A" has no attribute "z"
x = A()
x = B()
x.z
except:
x.z # E: "A" has no attribute "z"
x = B()
x.z
else:
x = B()
x.z
[case testUnionTryExcept3]
class A:
y = A()
class B(A):
z = 1
x = A()
def f(): x # Prevent redefinition of x
x = B()
try:
raise BaseException()
x = A()
except:
pass
x.z
x = B()
try:
x = A()
raise BaseException()
except:
pass
x.z # E: "A" has no attribute "z"
x = B()
try:
pass
except:
x = A()
raise BaseException()
x.z
try:
x = A()
except:
pass
x.z # E: "A" has no attribute "z"
x = B()
try:
pass
except:
x = A()
x.z # E: "A" has no attribute "z"
[builtins fixtures/exception.pyi]
[case testUnionTryExcept4]
class A: pass
class B(A):
z = 1
x = A()
while bool():
try:
x.z # E: "A" has no attribute "z"
x = A()
except:
x = B()
else:
x = B()
x.z
[builtins fixtures/exception.pyi]
[case testUnionTryFinally]
class A: pass
class B(A):
b = 1
x = A()
def f(): x # Prevent redefinition
x = B()
try:
x = A()
x.b # E: "A" has no attribute "b"
x = B()
finally:
x.b # E: "A" has no attribute "b"
x.b
[case testUnionTryFinally2]
class A: pass
class B(A):
b = 1
x = A()
def f(): x # Prevent redefinition
x = B()
try:
x = A()
x = B()
except:
pass
finally:
pass
x.b # E: "A" has no attribute "b"
[case testUnionTryFinally3]
class A: pass
class B(A):
b = 1
x = A()
def f(): x # Prevent redefinition
x = B()
try:
x = A()
x = B()
except:
pass
finally:
x = B()
x.b
[case testUnionTryFinally4]
class A: pass
class B(A):
b = 1
while 2:
x = A()
def f(): x # Prevents redefinition
x = B()
try:
x = A()
x = B()
except:
pass
finally:
x.b # E: "A" has no attribute "b"
if not isinstance(x, B):
break
x.b
[builtins fixtures/isinstancelist.pyi]
[case testUnionTryFinally5]
class A: pass
class B(A):
b = 1
while 2:
x = A()
try:
x = A()
x = B()
finally:
x.b # E: "A" has no attribute "b"
break
x.b
x.b
[case testUnionTryFinally6]
class A: pass
class B(A):
b = 1
def f() -> int:
x = B() # type: A
try:
x = B()
except:
x = A()
# An exception could occur here
x = B()
finally:
return x.b # E: "A" has no attribute "b"
[case testUnionListIsinstance]
from typing import Union, List
def f(x: Union[List[int], List[str], int]) -> None:
if isinstance(x, list):
a = x[0]
if isinstance(a, int):
a + 1
a + 'x' # E: Unsupported operand types for + ("int" and "str")
# type of a?
reveal_type(x) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.list[builtins.str]]"
x + 1 # E: Unsupported operand types for + ("List[int]" and "int") \
# E: Unsupported operand types for + ("List[str]" and "int") \
# N: Left operand is of type "Union[List[int], List[str]]"
else:
x[0] # E: Value of type "int" is not indexable
x + 1
x[0] # E: Value of type "Union[List[int], List[str], int]" is not indexable
x + 1 # E: Unsupported operand types for + ("List[int]" and "int") \
# E: Unsupported operand types for + ("List[str]" and "int") \
# N: Left operand is of type "Union[List[int], List[str], int]"
[builtins fixtures/isinstancelist.pyi]
[case testUnionListIsinstance2]
from typing import Union, List
class A:
a = 1
class B: pass
class C: pass
def g(x: Union[A, B]) -> A: pass
def h(x: C) -> A: pass
def f(x: Union[A, B, C]) -> None:
if isinstance(x, C):
x = h(x)
else:
x = g(x)
x.a
[builtins fixtures/isinstancelist.pyi]
[case testUnionStrictDefnBasic]
from typing import Union
def foo() -> Union[int, str]: pass
x = foo()
if int():
x = 1
x = x + 1
x = foo()
x = x + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
if isinstance(x, str):
x = x + 1 # E: Unsupported operand types for + ("str" and "int")
x = 1
x = x + 1
[builtins fixtures/isinstancelist.pyi]
[case testSubtypeRedefinitionBasic]
from typing import Union
class A: pass
class B(A):
y = 1
x = A()
x.y # E: "A" has no attribute "y"
x = B()
x.y # OK: x is known to be a B
[builtins fixtures/isinstancelist.pyi]
[case testIsInstanceBasic]
from typing import Union
x: Union[int, str]
if isinstance(x, str):
x = x + 1 # E: Unsupported operand types for + ("str" and "int")
x = x + 'a'
else:
x = x + 'a' # E: Unsupported operand types for + ("int" and "str")
x = x + 1
[builtins fixtures/isinstancelist.pyi]
[case testIsInstanceIndexing]
from typing import Union
x: Union[int, str]
j = [x]
if isinstance(j[0], str):
j[0] = j[0] + 'a'
j[0] = j[0] + 1 # E: Unsupported operand types for + ("str" and "int")
else:
j[0] = j[0] + 'a' # E: Unsupported operand types for + ("int" and "str")
j[0] = j[0] + 1
[builtins fixtures/isinstancelist.pyi]
[case testIsInstanceSubClassMember]
from typing import Union
class Animal: pass
class Dog(Animal):
paws = 4 # type: Union[int, str]
def bark(self): pass
class House:
pet = None # type: Animal
h = House()
h.pet = Dog()
while bool():
if isinstance(h.pet, Dog):
if isinstance(h.pet.paws, str):
x = h.pet.paws + 'a'
y = h.pet.paws + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
z = h.pet.paws + 'a' # E: Unsupported operand types for + ("int" and "str") \
# N: Left operand is of type "Union[int, str]"
if isinstance(h.pet.paws, str):
x = h.pet.paws + 'a'
break
y = h.pet.paws + 1
z = h.pet.paws + 'a' # E: Unsupported operand types for + ("int" and "str")
[builtins fixtures/isinstancelist.pyi]
[case testIsInstanceSubClassReset]
class A:
pass
class B(A):
b = 1
class C:
a = A()
x = C()
x.a.b # E: "A" has no attribute "b"
if isinstance(x.a, B):
x.a.b
x = C()
x.a.b # E: "A" has no attribute "b"
[builtins fixtures/isinstance.pyi]
[case testIsinstanceTuple]
from typing import Union
class A: pass
class B:
def method2(self, arg: int):
return 123
class C:
def method2(self, arg: int):
return 456
def method3(self, arg: str):
return 'abc'
v = A() # type: Union[A, B, C]
if isinstance(v, (B, C)):
v.method2(123)
v.method3('xyz') # E: Item "B" of "Union[B, C]" has no attribute "method3"
[builtins fixtures/isinstance.pyi]
[case testIsinstanceNeverWidens]
from typing import Union
class A: pass
class B: pass
class C: pass
a = A() # type: A
assert isinstance(a, (A, B))
reveal_type(a) # N: Revealed type is "__main__.A"
b = A() # type: Union[A, B]
assert isinstance(b, (A, B, C))
reveal_type(b) # N: Revealed type is "Union[__main__.A, __main__.B]"
[builtins fixtures/isinstance.pyi]
[case testMemberAssignmentChanges]
from typing import Union
class Dog:
paws = 1 # type: Union[int, str]
pet = Dog()
pet.paws + 'a' # E: Unsupported operand types for + ("int" and "str") \
# N: Left operand is of type "Union[int, str]"
pet.paws = 'a'
pet.paws + 'a'
pet.paws = 1
pet.paws + 1
[builtins fixtures/isinstancelist.pyi]
[case testIsInstanceSubClassMemberHard]
from typing import Union
class Animal:
pass
class Dog(Animal):
paws = 4 # type: Union[int, str]
def bark(self): pass
class House:
pet = None # type: Animal
h = House()
h.pet = Dog()
if isinstance(h.pet, Dog):
if isinstance(h.pet.paws, str):
for i in [1]:
# TODO: should we allow this if iterable is of length one or zero?
h.pet.paws + 'a' # E: Unsupported operand types for + ("int" and "str") \
# N: Left operand is of type "Union[int, str]"
if bool():
break
h.pet.paws = 1
h.pet.paws + 1
if isinstance(h.pet.paws, str):
h.pet.paws + 'a'
else:
h.pet.paws + 1
[builtins fixtures/isinstancelist.pyi]
[case testIsInstanceReturn]
from typing import Union
def foo() -> None:
x = 1 # type: Union[int, str]
if isinstance(x, int):
return
y = x + 'asdad'
def bar() -> None:
x = 1 # type: Union[int, str]
if isinstance(x, int):
return
else:
pass
y = x + 'asdad'
foo()
[builtins fixtures/isinstancelist.pyi]
[case testIsInstanceBadBreak]
from typing import Union
def foo() -> None:
x: Union[int, str]
if isinstance(x, int):
for z in [1,2]:
break
else:
pass
y = x + 'asdad' # E: Unsupported operand types for + ("int" and "str") \
# N: Left operand is of type "Union[int, str]"
foo()
[builtins fixtures/isinstancelist.pyi]
[case testIsInstanceThreeUnion]
from typing import Union, List
x: Union[int, str, List[int]]
while bool():
if isinstance(x, int):
x + 1
elif isinstance(x, str):
x + 'a'
else:
x + [1]
x + 'a' # E: Unsupported operand types for + ("int" and "str") \
# E: Unsupported operand types for + ("List[int]" and "str") \
# N: Left operand is of type "Union[int, str, List[int]]"
x + [1] # E: Unsupported operand types for + ("int" and "List[int]") \
# E: Unsupported operand types for + ("str" and "List[int]") \
# N: Left operand is of type "Union[int, str, List[int]]"
[builtins fixtures/isinstancelist.pyi]
[case testIsInstanceThreeUnion2]
from typing import Union, List
x: Union[int, str, List[int]]
while bool():
if isinstance(x, int):
x + 1
break
elif isinstance(x, str):
x + 'a'
break
x + [1]
x + 'a' # E: Unsupported operand types for + ("List[int]" and "str")
x + [1] # E: Unsupported operand types for + ("int" and "List[int]") \
# E: Unsupported operand types for + ("str" and "List[int]") \
# N: Left operand is of type "Union[int, str, List[int]]"
[builtins fixtures/isinstancelist.pyi]
[case testIsInstanceThreeUnion3]
from typing import Union, List
while bool():
x: Union[int, str, List[int]]
def f(): x # Prevent redefinition
x = 1
if isinstance(x, int):
x + 1
break
elif isinstance(x, str):
x + 'a'
break
x + [1] # These lines aren't reached because x was an int
x + 'a'
x + [1] # E: Unsupported operand types for + ("int" and "List[int]") \
# E: Unsupported operand types for + ("str" and "List[int]") \
# N: Left operand is of type "Union[int, str, List[int]]"
[builtins fixtures/isinstancelist.pyi]
[case testRemovingTypeRepeatedly]
from typing import Union
def foo() -> Union[int, str]: pass
for i in [1, 2]:
x = foo()
x + 'a' # E: Unsupported operand types for + ("int" and "str") \
# N: Left operand is of type "Union[int, str]"
if isinstance(x, int):
break
x + 'a'
x = foo()
x + 'a' # E: Unsupported operand types for + ("int" and "str") \
# N: Left operand is of type "Union[int, str]"
if isinstance(x, int):
break
x + 'a'
x = foo()
x + 'a' # E: Unsupported operand types for + ("int" and "str") \
# N: Left operand is of type "Union[int, str]"
if isinstance(x, int):
break
x + 'a'
x + 'a' # E: Unsupported operand types for + ("int" and "str") \
# N: Left operand is of type "Union[int, str]"
[builtins fixtures/isinstancelist.pyi]
[case testModifyRepeatedly]
from typing import Union
def foo() -> Union[int, str]: pass
x = foo()
def f(): x # Prevent redefinition
x + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
x + 'a' # E: Unsupported operand types for + ("int" and "str") \
# N: Left operand is of type "Union[int, str]"
x = 1
x + 1
x + 'a' # E: Unsupported operand types for + ("int" and "str")
x = 'a'
x + 1 # E: Unsupported operand types for + ("str" and "int")
x + 'a'
x = foo()
x + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
x + 'a' # E: Unsupported operand types for + ("int" and "str") \
# N: Left operand is of type "Union[int, str]"
[builtins fixtures/isinstancelist.pyi]
[case testModifyLoop]
from typing import Union
def foo() -> Union[int, str]: pass
x = foo()
def f(): x # Prevent redefinition
x + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
x = 'a'
x + 1 # E: Unsupported operand types for + ("str" and "int")
x = 1
x + 1
while bool():
x + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
x = 'a'
[builtins fixtures/isinstancelist.pyi]
[case testModifyLoop2]
from typing import Union
def foo() -> Union[int, str]: pass
x = foo()
def f(): x # Prevent redefinition
x + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
x = 'a'
x + 1 # E: Unsupported operand types for + ("str" and "int")
x = 1
x + 1
for i in [1]:
x = 'a'
x + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
[builtins fixtures/isinstancelist.pyi]
[case testModifyLoop3]
from typing import Union
def foo() -> Union[int, str]: pass
x = foo()
def f(): x # Prevent redefinition
x = 1
while bool():
x + 1
x = 'a'
break
else:
x + 1
x + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
x = 1
for y in [1]:
x + 1
x = 'a'
break
else:
x + 1
x + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
[builtins fixtures/isinstancelist.pyi]
[case testModifyLoopWhile4]
from typing import Union
def foo() -> Union[int, str]: pass
x = foo()
def f(): x # Prevent redefinition
x = 1
while bool():
x + 1
if bool():
x = 'a'
break
else:
x + 1
x = 'a'
x + 'a'
x = 1
while bool():
x + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
if bool():
x = 'a'
continue
else:
x + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
x = 'a'
x + 'a'
[builtins fixtures/isinstancelist.pyi]
[case testModifyLoopFor4]
from typing import Union
def foo() -> Union[int, str]: pass
x = foo()
def f(): x # Prevent redefinition
x = 1
for y in [1]:
x + 1
if bool():
x = 'a'
break
else:
x + 1
x = 'a'
x + 'a'
x = 1
for y in [1]:
x + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
if bool():
x = 'a'
continue
else:
x + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
x = 'a'
x + 'a'
[builtins fixtures/isinstancelist.pyi]
[case testModifyNestedLoop]
from typing import Union
def foo() -> Union[int, str]: pass
x = foo()
def f(): x # Prevent redefinition
x = 1
for y in [1]:
for z in [1]:
break
else:
x = 'a'
break
else:
x + 1
x + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
x = 1
while bool():
while bool():
break
else:
x = 'a'
break
else:
x + 1
x + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
[builtins fixtures/isinstancelist.pyi]
[case testModifyLoopLong]
from typing import Union
class A: a = 1
def foo() -> Union[int, str, A]: pass
def bar() -> None:
x = foo()
x + 1 # E: Unsupported left operand type for + ("A") \
# N: Left operand is of type "Union[int, str, A]" \
# E: Unsupported operand types for + ("str" and "int")
if isinstance(x, A):
x.a
else:
if isinstance(x, int):
x + 1
x + 'a' # E: Unsupported operand types for + ("int" and "str")
else:
x + 'a'
x.a # E: "str" has no attribute "a"
x = A()
if isinstance(x, str):
x + 'a'
else:
while bool():
if isinstance(x, int):
x + 1
else:
x.a
break
while bool():
if isinstance(x, int):