-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathcheck-statements.test
More file actions
2414 lines (2011 loc) · 61.9 KB
/
check-statements.test
File metadata and controls
2414 lines (2011 loc) · 61.9 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
-- Return statement
-- ----------------
[case testReturnValue]
import typing
def f() -> 'A':
return A()
def g() -> 'B':
return A()
class A:
pass
class B:
pass
[out]
main:5: error: Incompatible return value type (got "A", expected "B")
[case testReturnSubtype]
import typing
def f() -> 'B':
return A()
def g() -> 'A':
return B()
class A:
pass
class B(A):
pass
[out]
main:3: error: Incompatible return value type (got "A", expected "B")
[case testReturnWithoutAValue]
import typing
def f() -> 'A':
return
def g() -> None:
return
class A:
pass
[out]
main:3: error: Return value expected
[case testReturnNoneInFunctionReturningNone]
import typing
def f() -> None:
return None
def g() -> None:
return f()
[out]
[case testReturnInGenerator]
from typing import Generator
def f() -> Generator[int, None, str]:
yield 1
return "foo"
[out]
[case testEmptyReturnInGenerator]
from typing import Generator
def f() -> Generator[int, None, str]:
yield 1
return # E: Return value expected
[out]
[case testNoReturnInGenerator]
from typing import Generator
def f() -> Generator[int, None, str]: # E: Missing return statement
yield 1
[out]
[case testEmptyReturnInNoneTypedGenerator]
from typing import Generator
def f() -> Generator[int, None, None]:
yield 1
return
[out]
[case testNonEmptyReturnInNoneTypedGenerator]
from typing import Generator
def f() -> Generator[int, None, None]:
yield 1
return 42 # E: No return value expected
[out]
[case testReturnInIterator]
from typing import Iterator
def f() -> Iterator[int]:
yield 1
return "foo" # E: No return value expected
[out]
-- If statement
-- ------------
[case testIfStatement]
a: A
a2: A
a3: A
b: bool
if a:
a = b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
elif a2:
a = b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
elif a3:
a = b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
else:
a = b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
if b:
pass
elif b:
pass
if b:
pass
class A: pass
[builtins fixtures/bool.pyi]
-- Loops
-- -----
[case testWhileStatement]
a: A
b: bool
while a:
a = b # Fail
else:
a = b # Fail
while b:
b = b
class A: pass
[builtins fixtures/bool.pyi]
[out]
main:5: error: Incompatible types in assignment (expression has type "bool", variable has type "A")
main:7: error: Incompatible types in assignment (expression has type "bool", variable has type "A")
[case testForStatement]
class A: pass
a: A
b: object
for a in [A()]:
a = b # E: Incompatible types in assignment (expression has type "object", variable has type "A")
else:
a = b # E: Incompatible types in assignment (expression has type "object", variable has type "A")
[builtins fixtures/list.pyi]
[case testBreakStatement]
import typing
while None:
break
[builtins fixtures/bool.pyi]
[out]
[case testContinueStatement]
import typing
while None:
continue
[builtins fixtures/bool.pyi]
[out]
[case testForStatementTypeComments]
from typing import List, Union
x = [] # type: List[int]
for y in x: # type: str # E: Incompatible types in assignment (expression has type "int", variable has type "str")
pass
for z in x: # type: int
pass
for w in x: # type: Union[int, str]
reveal_type(w) # N: Revealed type is "builtins.int | builtins.str"
for v in x: # type: int, int # E: Syntax error in type annotation # N: Suggestion: Use Tuple[T1, ..., Tn] instead of (T1, ..., Tn)
pass
[builtins fixtures/list.pyi]
[case testForStatementMultipleTypeComments]
from typing import List, Tuple
x = [] # type: List[Tuple[int, int]]
for y in x: # type: int, int # E: Syntax error in type annotation # N: Suggestion: Use Tuple[T1, ..., Tn] instead of (T1, ..., Tn)
pass
for z in x: # type: Tuple[int, int]
pass
for w,v in x: # type: int, str # E: Incompatible types in assignment (expression has type "int", variable has type "str")
pass
for a, b in x: # type: int, int, int # E: Incompatible number of tuple items
pass
[builtins fixtures/list.pyi]
-- Operator assignment
-- -------------------
[case testPlusAssign]
a: A
b: B
c: C
a += b # Fail
b += a # Fail
c += a # Fail
a += c
class A:
def __add__(self, x: 'C') -> 'A': pass
class B:
def __add__(self, x: A) -> 'C': pass
class C: pass
[builtins fixtures/tuple.pyi]
[out]
main:4: error: Unsupported operand types for + ("A" and "B")
main:5: error: Incompatible types in assignment (expression has type "C", variable has type "B")
main:6: error: Unsupported left operand type for + ("C")
[case testMinusAssign]
a: A
b: B
c: C
a -= b # Fail
b -= a # Fail
c -= a # Fail
a -= c
class A:
def __sub__(self, x: 'C') -> 'A': pass
class B:
def __sub__(self, x: A) -> 'C': pass
class C: pass
[builtins fixtures/tuple.pyi]
[out]
main:4: error: Unsupported operand types for - ("A" and "B")
main:5: error: Incompatible types in assignment (expression has type "C", variable has type "B")
main:6: error: Unsupported left operand type for - ("C")
[case testMulAssign]
a: A
c: C
a *= a # Fail
c *= a # Fail
a *= c
class A:
def __mul__(self, x: 'C') -> 'A': pass
class C: pass
[builtins fixtures/tuple.pyi]
[out]
main:3: error: Unsupported operand types for * ("A" and "A")
main:4: error: Unsupported left operand type for * ("C")
[case testMatMulAssign]
a: A
c: C
a @= a # E: Unsupported operand types for @ ("A" and "A")
c @= a # E: Unsupported left operand type for @ ("C")
a @= c
class A:
def __matmul__(self, x: 'C') -> 'A': pass
class C: pass
[builtins fixtures/tuple.pyi]
[case testDivAssign]
a: A
c: C
a /= a # Fail
c /= a # Fail
a /= c
class A:
def __truediv__(self, x: 'C') -> 'A': pass
class C: pass
[builtins fixtures/tuple.pyi]
[out]
main:3: error: Unsupported operand types for / ("A" and "A")
main:4: error: Unsupported left operand type for / ("C")
[case testPowAssign]
a: A
c: C
a **= a # Fail
c **= a # Fail
a **= c
class A:
def __pow__(self, x: 'C') -> 'A': pass
class C: pass
[builtins fixtures/tuple.pyi]
[out]
main:3: error: Unsupported operand types for ** ("A" and "A")
main:4: error: Unsupported left operand type for ** ("C")
[case testSubtypesInOperatorAssignment]
a: A
b: B
b += b
b += a
a += b
class A:
def __add__(self, x: 'A') -> 'B': pass
class B(A): pass
[builtins fixtures/tuple.pyi]
[out]
[case testAdditionalOperatorsInOpAssign]
a: A
c: C
a &= a # Fail
a >>= a # Fail
a //= a # Fail
a &= c
a >>= c
a //= c
class A:
def __and__(self, x: 'C') -> 'A': pass
def __rshift__(self, x: 'C') -> 'A': pass
def __floordiv__(self, x: 'C') -> 'A': pass
class C: pass
[builtins fixtures/tuple.pyi]
[out]
main:3: error: Unsupported operand types for & ("A" and "A")
main:4: error: Unsupported operand types for >> ("A" and "A")
main:5: error: Unsupported operand types for // ("A" and "A")
[case testInplaceOperatorMethods]
import typing
class A:
def __iadd__(self, x: int) -> 'A': pass
def __imul__(self, x: str) -> 'A': pass
def __imatmul__(self, x: str) -> 'A': pass
a = A()
a += 1
a *= ''
a @= ''
a += '' # E: Argument 1 to "__iadd__" of "A" has incompatible type "str"; expected "int"
a *= 1 # E: Argument 1 to "__imul__" of "A" has incompatible type "int"; expected "str"
a @= 1 # E: Argument 1 to "__imatmul__" of "A" has incompatible type "int"; expected "str"
[case testInplaceSetitem]
class A(object):
def __init__(self) -> None:
self.a = [1]
def __iadd__(self, a):
# type: (int) -> A
self.a += [2]
return self
a = A()
b = [a]
b[0] += 1
[builtins fixtures/list.pyi]
[out]
-- Assert statement
-- ----------------
[case testAssert]
import typing
assert None + None # Fail
assert None
[out]
main:2: error: Unsupported left operand type for + ("None")
-- Exception handling
-- ------------------
[case testRaiseStatement]
e: BaseException
f: MyError
a: A
raise a # Fail
raise e
raise f
class A: pass
class MyError(BaseException): pass
[builtins fixtures/exception.pyi]
[out]
main:5: error: Exception must be derived from BaseException
[case testRaiseClassObject]
class A: pass
class MyError(BaseException): pass
def f(): pass
if object():
raise BaseException
if object():
raise MyError
if object():
raise A # E: Exception must be derived from BaseException
if object():
raise object # E: Exception must be derived from BaseException
if object():
raise f # E: Exception must be derived from BaseException
[builtins fixtures/exception.pyi]
[case testRaiseClassObjectCustomInit]
class MyBaseError(BaseException):
def __init__(self, required) -> None:
...
class MyError(Exception):
def __init__(self, required1, required2) -> None:
...
class MyKwError(Exception):
def __init__(self, *, kwonly) -> None:
...
class MyErrorWithDefault(Exception):
def __init__(self, optional=1) -> None:
...
if object():
raise BaseException
if object():
raise Exception
if object():
raise BaseException(1)
if object():
raise Exception(2)
if object():
raise MyBaseError(4)
if object():
raise MyError(5, 6)
if object():
raise MyKwError(kwonly=7)
if object():
raise MyErrorWithDefault(8)
if object():
raise MyErrorWithDefault
if object():
raise MyBaseError # E: Too few arguments for "MyBaseError"
if object():
raise MyError # E: Too few arguments for "MyError"
if object():
raise MyKwError # E: Missing named argument "kwonly" for "MyKwError"
[builtins fixtures/exception.pyi]
[case testRaiseExceptionType]
import typing
x: typing.Type[BaseException]
raise x
[builtins fixtures/exception.pyi]
[case testRaiseNonExceptionTypeFails]
import typing
x = int # type: typing.Type[int]
raise x # E: Exception must be derived from BaseException
[builtins fixtures/exception.pyi]
[case testRaiseUnion]
import typing
x: typing.Union[BaseException, typing.Type[BaseException]]
raise x
[builtins fixtures/exception.pyi]
[case testRaiseNonExceptionUnionFails]
import typing
x: typing.Union[BaseException, int]
raise x # E: Exception must be derived from BaseException
[builtins fixtures/exception.pyi]
[case testRaiseFromStatement]
e: BaseException
f: MyError
a: A
x: BaseException
del x
if object():
raise e from a # E: Exception must be derived from BaseException
if object():
raise e from e
if object():
raise e from f
if object():
raise e from x # E: Trying to read deleted variable "x"
class A: pass
class MyError(BaseException): pass
[builtins fixtures/exception.pyi]
[case testRaiseFromClassobject]
import typing
class A: pass
class MyError(BaseException): pass
def f(): pass
if object():
raise BaseException from BaseException
if object():
raise BaseException from MyError
if object():
raise BaseException from A # E: Exception must be derived from BaseException
if object():
raise BaseException from object # E: Exception must be derived from BaseException
if object():
raise BaseException from f # E: Exception must be derived from BaseException
[builtins fixtures/exception.pyi]
[case testRaiseNotImplementedFails]
if object():
raise NotImplemented # E: Exception must be derived from BaseException; did you mean "NotImplementedError"?
if object():
raise NotImplemented() # E: Exception must be derived from BaseException; did you mean "NotImplementedError"?
[builtins fixtures/notimplemented.pyi]
[case testTryFinallyStatement]
import typing
try:
b = object() # type: A # Fail
finally:
c = object() # type: A # Fail
class A: pass
[out]
main:3: error: Incompatible types in assignment (expression has type "object", variable has type "A")
main:5: error: Incompatible types in assignment (expression has type "object", variable has type "A")
[case testSimpleTryExcept]
try:
pass
except BaseException as e:
a: BaseException
o: object
e = a
e = o # Fail
class A: pass
class B: pass
[builtins fixtures/exception.pyi]
[out]
main:8: error: Incompatible types in assignment (expression has type "object", variable has type "BaseException")
[case testTypeErrorInBlock]
class A: pass
class B: pass
while int():
x: A
if int():
x = object() # E: Incompatible types in assignment (expression has type "object", variable has type "A")
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
[case testTypeErrorInvolvingBaseException]
class A: pass
x: BaseException
a: A
if int():
a = BaseException() # E: Incompatible types in assignment (expression has type "BaseException", variable has type "A")
if int():
a = object() # E: Incompatible types in assignment (expression has type "object", variable has type "A")
if int():
x = object() # E: Incompatible types in assignment (expression has type "object", variable has type "BaseException")
if int():
x = A() # E: Incompatible types in assignment (expression has type "A", variable has type "BaseException")
if int():
x = BaseException()
[builtins fixtures/exception.pyi]
[case testSimpleTryExcept2]
import typing
try:
pass
except BaseException as e:
e = object() # Fail
e = BaseException()
[builtins fixtures/exception.pyi]
[out]
main:5: error: Incompatible types in assignment (expression has type "object", variable has type "BaseException")
[case testBaseClassAsExceptionTypeInExcept]
import typing
class Err(BaseException): pass
try:
pass
except Err as e:
e = BaseException() # E: Incompatible types in assignment (expression has type "BaseException", variable has type "Err")
e = Err()
[builtins fixtures/exception.pyi]
[case testMultipleExceptHandlers]
import typing
class Err(BaseException): pass
try:
pass
except BaseException as e:
pass
except Err as f:
f = BaseException() # E: Incompatible types in assignment (expression has type "BaseException", variable has type "Err")
f = Err()
[builtins fixtures/exception.pyi]
[case testTryExceptStatement]
import typing
class A: pass
class B: pass
class Err(BaseException): pass
try:
a = B() # type: A # E: Incompatible types in assignment (expression has type "B", variable has type "A")
except BaseException as e:
e = A() # E: Incompatible types in assignment (expression has type "A", variable has type "BaseException")
e = Err()
except Err as f:
f = BaseException() # E: Incompatible types in assignment (expression has type "BaseException", variable has type "Err")
f = Err()
[builtins fixtures/exception.pyi]
[case testTryExceptWithinFunction]
import typing
def f() -> None:
try: pass
except BaseException as e:
e = object() # Fail
e = BaseException()
except Err as f:
f = BaseException() # Fail
f = Err()
class Err(BaseException): pass
[builtins fixtures/exception.pyi]
[out]
main:5: error: Incompatible types in assignment (expression has type "object", variable has type "BaseException")
main:8: error: Incompatible types in assignment (expression has type "BaseException", variable has type "Err")
[case testTryExceptFlow]
def f() -> None:
x = 1
try:
pass
except:
raise
x + 'a' # E: Unsupported left operand type for + ("int")
[builtins fixtures/exception.pyi]
[out]
[case testTryWithElse]
import typing
try: pass
except BaseException: pass
else:
object(None) # E: Too many arguments for "object"
[builtins fixtures/exception.pyi]
[case testRedefinedFunctionInTryWithElse]
def f() -> None: pass
try:
pass
except BaseException:
f2 = f
else:
def f2() -> str: pass
try:
pass
except BaseException:
f3 = f
else:
def f3() -> None: pass
[builtins fixtures/exception.pyi]
[out]
main:7: error: Incompatible redefinition (redefinition with type "Callable[[], str]", original type "Callable[[], None]")
[case testExceptWithoutType]
import typing
try:
-None # E: Unsupported operand type for unary - ("None")
except:
~None # E: Unsupported operand type for ~ ("None")
[builtins fixtures/exception.pyi]
[case testRaiseWithoutArgument]
import typing
try:
None
except:
raise
[builtins fixtures/exception.pyi]
[case testExceptWithMultipleTypes]
import typing
class E1(BaseException): pass
class E2(E1): pass
try:
pass
except (E1, E2): pass
except (E1, object): pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
except (object, E2): pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
except (E1, (E2,)): pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
except (E1, E2): pass
except ((E1, E2)): pass
except (((E1, E2))): pass
[builtins fixtures/exception.pyi]
[case testExceptWithTypeType]
import typing
E = BaseException # type: typing.Type[BaseException]
try:
pass
except E:
pass
[builtins fixtures/exception.pyi]
[case testExceptWithMultipleTypes2]
import typing
class E1(BaseException): pass
class E2(E1): pass
try:
pass
except (E1, E2) as e1:
x = e1 # type: E1
y = e1 # type: E2 # E: Incompatible types in assignment (expression has type "E1", variable has type "E2")
except (E2, E1) as e2:
a = e2 # type: E1
b = e2 # type: E2 # E: Incompatible types in assignment (expression has type "E1", variable has type "E2")
except (E1, E2, int) as e3: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
pass
[builtins fixtures/exception.pyi]
[case testExceptWithMultipleTypes3]
import typing
class E1(BaseException): pass
class E1_1(E1): pass
class E1_2(E1): pass
try: pass
except (E1, E1_1, E1_2) as e1:
x = e1 # type: E1
y = e1 # type: E1_1 # E: Incompatible types in assignment (expression has type "E1", variable has type "E1_1")
z = e1 # type: E1_2 # E: Incompatible types in assignment (expression has type "E1", variable has type "E1_2")
except (E1_1, E1_2) as e2:
a = e2 # type: E1
b = e2 # type: E1_1 # E: Incompatible types in assignment (expression has type "E1_1 | E1_2", variable has type "E1_1")
c = e2 # type: E1_2 # E: Incompatible types in assignment (expression has type "E1_1 | E1_2", variable has type "E1_2")
[builtins fixtures/exception.pyi]
[case testExceptWithMultipleTypes4]
from typing import Tuple, Type, Union
class E1(BaseException): pass
class E2(BaseException): pass
class E3(BaseException): pass
def variadic(exc: Tuple[Type[E1], ...]) -> None:
try:
pass
except exc as e:
reveal_type(e) # N: Revealed type is "__main__.E1"
def union(exc: Union[Type[E1], Type[E2]]) -> None:
try:
pass
except exc as e:
reveal_type(e) # N: Revealed type is "__main__.E1 | __main__.E2"
def tuple_in_union(exc: Union[Type[E1], Tuple[Type[E2], Type[E3]]]) -> None:
try:
pass
except exc as e:
reveal_type(e) # N: Revealed type is "__main__.E1 | __main__.E2 | __main__.E3"
def variadic_in_union(exc: Union[Type[E1], Tuple[Type[E2], ...]]) -> None:
try:
pass
except exc as e:
reveal_type(e) # N: Revealed type is "__main__.E1 | __main__.E2"
def nested_union(exc: Union[Type[E1], Union[Type[E2], Type[E3]]]) -> None:
try:
pass
except exc as e:
reveal_type(e) # N: Revealed type is "__main__.E1 | __main__.E2 | __main__.E3"
def error_in_union(exc: Union[Type[E1], int]) -> None:
try:
pass
except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
pass
def error_in_variadic(exc: Tuple[int, ...]) -> None:
try:
pass
except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
pass
[builtins fixtures/tuple.pyi]
[case testExceptWithMultipleTypes5]
from typing import Tuple, Type, Union
class E1(BaseException): pass
class E2(BaseException): pass
class E3(BaseException): pass
def union_in_variadic(exc: Tuple[Union[Type[E1], Type[E2]], ...]) -> None:
try:
pass
except exc as e:
reveal_type(e) # N: Revealed type is "__main__.E1 | __main__.E2"
def nested_union_in_variadic(exc: Tuple[Union[Type[E1], Union[Type[E2], Type[E3]]], ...]) -> None:
try:
pass
except exc as e:
reveal_type(e) # N: Revealed type is "__main__.E1 | __main__.E2 | __main__.E3"
def union_in_tuple(exc: Tuple[Union[Type[E1], Type[E2]], Type[E3]]) -> None:
try:
pass
except exc as e:
reveal_type(e) # N: Revealed type is "__main__.E1 | __main__.E2 | __main__.E3"
def error_in_variadic_union(exc: Tuple[Union[Type[E1], int], ...]) -> None:
try:
pass
except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
pass
def error_in_variadic_nested_union(exc: Tuple[Union[Type[E1], Union[Type[E2], int]], ...]) -> None:
try:
pass
except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
pass
def error_in_tuple_inside_variadic_nested_union(exc: Tuple[Union[Type[E1], Union[Type[E2], Tuple[Type[E3]]]], ...]) -> None:
try:
pass
except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
pass
def error_in_tuple_union(exc: Tuple[Union[Type[E1], Type[E2]], Union[Type[E3], int]]) -> None:
try:
pass
except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
pass
[builtins fixtures/tuple.pyi]
[case testExceptWithAnyTypes]
from typing import Any
E1 = None # type: Any
class E2(BaseException): pass
class NotBaseDerived: pass
try:
pass
except BaseException as e1:
reveal_type(e1) # N: Revealed type is "builtins.BaseException"
except (E1, BaseException) as e2:
reveal_type(e2) # N: Revealed type is "Any | builtins.BaseException"
except (E1, E2) as e3:
reveal_type(e3) # N: Revealed type is "Any | __main__.E2"
except (E1, E2, BaseException) as e4:
reveal_type(e4) # N: Revealed type is "Any | builtins.BaseException"
try: pass
except E1 as e1:
reveal_type(e1) # N: Revealed type is "Any"
except E2 as e2:
reveal_type(e2) # N: Revealed type is "__main__.E2"
except NotBaseDerived as e3: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
pass
except (NotBaseDerived, E1) as e4: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
pass
except (NotBaseDerived, E2) as e5: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
pass
except (NotBaseDerived, E1, E2) as e6: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
pass
except (E1, E2, NotBaseDerived) as e6: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
pass
[builtins fixtures/exception.pyi]
[case testReuseTryExceptionVariable]
import typing
class E1(BaseException): pass
class E2(BaseException): pass
try: pass
except E1 as e: pass
try: pass
except E1 as e: pass
try: pass
except E2 as e: pass
e + 1 # E: Trying to read deleted variable "e" # E: Name "e" is used before definition
e = E1() # E: Assignment to variable "e" outside except: block
[builtins fixtures/exception.pyi]
[case testReuseDefinedTryExceptionVariable]
import typing
class E1(BaseException): pass
class E2(BaseException): pass
e = 1
def f(): e # Prevent redefinition
e = 1
try: pass
except E1 as e: pass
e = 1 # E: Assignment to variable "e" outside except: block
e = E1() # E: Assignment to variable "e" outside except: block
[builtins fixtures/exception.pyi]
[case testExceptionVariableReuseInDeferredNode1]
def f(*a: BaseException) -> int:
x
try: pass
except BaseException as err: pass
try: pass
except BaseException as err: f(err)
return 0
x = f()
[builtins fixtures/exception.pyi]
[case testExceptionVariableReuseInDeferredNode2]
def f(*a: BaseException) -> int:
try: pass
except BaseException as err: pass
x
try: pass
except BaseException as err: f(err)
return 0
x = f()
[builtins fixtures/exception.pyi]
[case testExceptionVariableReuseInDeferredNode3]
def f(*a: BaseException) -> int:
try: pass
except BaseException as err: pass
try: pass
except BaseException as err: f(err)
x
return 0
x = f()
[builtins fixtures/exception.pyi]
[case testExceptionVariableReuseInDeferredNode4]
class EA(BaseException):
a = None # type: int
class EB(BaseException):
b = None # type: str
def f(*arg: BaseException) -> int:
x
try: pass
except EA as err:
f(err)
a = err.a
reveal_type(a)
try: pass
except EB as err:
f(err)
b = err.b
reveal_type(b)
return 0
x = f()
[builtins fixtures/exception.pyi]
[out]
main:11: note: Revealed type is "builtins.int"
main:16: note: Revealed type is "builtins.str"
[case testExceptionVariableReuseInDeferredNode5]
class EA(BaseException):
a = None # type: int
class EB(BaseException):
b = None # type: str
def f(*arg: BaseException) -> int:
try: pass
except EA as err:
f(err)
a = err.a
reveal_type(a)
x
try: pass
except EB as err:
f(err)
b = err.b
reveal_type(b)
return 0
x = f()
[builtins fixtures/exception.pyi]
[out]
main:10: note: Revealed type is "builtins.int"
main:16: note: Revealed type is "builtins.str"
[case testExceptionVariableReuseInDeferredNode6]
class EA(BaseException):
a = None # type: int