Debian Codegraph @

test-data/unit/check-possibly-undefined.test

raw
1 [case testDefinedInOneBranch]
2 # flags: --enable-error-code possibly-undefined
3 if int():
4 a = 1
5 else:
6 x = 2
7 z = a + 1 # E: Name "a" may be undefined
8 z = a + 1 # We only report the error on first occurrence.
9
10 [case testElif]
11 # flags: --enable-error-code possibly-undefined
12 if int():
13 a = 1
14 elif int():
15 a = 2
16 else:
17 x = 3
18
19 z = a + 1 # E: Name "a" may be undefined
20
21 [case testUsedInIf]
22 # flags: --enable-error-code possibly-undefined
23 if int():
24 y = 1
25 if int():
26 x = y # E: Name "y" may be undefined
27
28 [case testDefinedInAllBranches]
29 # flags: --enable-error-code possibly-undefined
30 if int():
31 a = 1
32 elif int():
33 a = 2
34 else:
35 a = 3
36 z = a + 1
37
38 [case testOmittedElse]
39 # flags: --enable-error-code possibly-undefined
40 if int():
41 a = 1
42 z = a + 1 # E: Name "a" may be undefined
43
44 [case testUpdatedInIf]
45 # flags: --enable-error-code possibly-undefined
46 # Variable a is already defined. Just updating it in an "if" is acceptable.
47 a = 1
48 if int():
49 a = 2
50 z = a + 1
51
52 [case testNestedIf]
53 # flags: --enable-error-code possibly-undefined
54 if int():
55 if int():
56 a = 1
57 x = 1
58 x = x + 1
59 else:
60 a = 2
61 b = a + x # E: Name "x" may be undefined
62 b = b + 1
63 else:
64 b = 2
65 z = a + b # E: Name "a" may be undefined
66
67 [case testVeryNestedIf]
68 # flags: --enable-error-code possibly-undefined
69 if int():
70 if int():
71 if int():
72 a = 1
73 else:
74 a = 2
75 x = a
76 else:
77 a = 2
78 b = a
79 else:
80 b = 2
81 z = a + b # E: Name "a" may be undefined
82
83 [case testTupleUnpack]
84 # flags: --enable-error-code possibly-undefined
85
86 if int():
87 (x, y) = (1, 2)
88 else:
89 [y, z] = [1, 2]
90 a = y + x # E: Name "x" may be undefined
91 a = y + z # E: Name "z" may be undefined
92
93 [case testIndexExpr]
94 # flags: --enable-error-code possibly-undefined
95
96 if int():
97 *x, y = (1, 2)
98 else:
99 x = [1, 2]
100 a = x # No error.
101 b = y # E: Name "y" may be undefined
102
103 [case testRedefined]
104 # flags: --enable-error-code possibly-undefined
105 y = 3
106 if int():
107 if int():
108 y = 2
109 x = y + 2
110 else:
111 if int():
112 y = 2
113 x = y + 2
114
115 x = y + 2
116
117 [case testFunction]
118 # flags: --enable-error-code possibly-undefined
119 def f0() -> None:
120 if int():
121 def some_func() -> None:
122 pass
123
124 some_func() # E: Name "some_func" may be undefined
125
126 def f1() -> None:
127 if int():
128 def some_func() -> None:
129 pass
130 else:
131 def some_func() -> None:
132 pass
133
134 some_func() # No error.
135
136 [case testLambda]
137 # flags: --enable-error-code possibly-undefined
138 def f0(b: bool) -> None:
139 if b:
140 fn = lambda: 2
141 y = fn # E: Name "fn" may be undefined
142
143 [case testUsedBeforeDefClass]
144 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
145 def f(x: A): # No error here.
146 pass
147 y = A() # E: Name "A" is used before definition
148 class A: pass
149
150 [case testClassScope]
151 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
152 class C:
153 x = 0
154 def f0(self) -> None: pass
155
156 def f2(self) -> None:
157 f0() # No error.
158 self.f0() # No error.
159
160 f0() # E: Name "f0" is used before definition
161 def f0() -> None: pass
162 y = x # E: Name "x" is used before definition
163 x = 1
164
165 [case testClassInsideFunction]
166 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
167 def f() -> None:
168 class C: pass
169
170 c = C() # E: Name "C" is used before definition
171 class C: pass
172
173 [case testUsedBeforeDefFunc]
174 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
175 foo() # E: Name "foo" is used before definition
176 def foo(): pass
177 [case testGenerator]
178 # flags: --enable-error-code possibly-undefined
179 if int():
180 a = 3
181 s = [a + 1 for a in [1, 2, 3]]
182 x = a # E: Name "a" may be undefined
183
184 [case testScope]
185 # flags: --enable-error-code possibly-undefined
186 def foo() -> None:
187 if int():
188 y = 2
189
190 if int():
191 y = 3
192 x = y # E: Name "y" may be undefined
193
194 [case testVarDefinedInOuterScopeUpdated]
195 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
196 def f0() -> None:
197 global x
198 y = x
199 x = 1 # No error.
200
201 x = 2
202
203 [case testNonlocalVar]
204 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
205 def f0() -> None:
206 x = 2
207
208 def inner() -> None:
209 nonlocal x
210 y = x
211 x = 1 # No error.
212
213 [case testGlobalDeclarationAfterUsage]
214 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
215 def f0() -> None:
216 y = x # E: Name "x" is used before definition
217 global x
218 x = 1 # No error.
219
220 x = 2
221
222 [case testVarDefinedInOuterScope]
223 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
224 def f0() -> None:
225 global x
226 y = x # We do not detect such errors right now.
227
228 f0()
229 x = 1
230
231 [case testDefinedInOuterScopeNoError]
232 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
233 def foo() -> None:
234 bar()
235
236 def bar() -> None:
237 foo()
238
239 [case testClassFromOuterScopeRedefined]
240 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
241 class c: pass
242
243 def f0() -> None:
244 s = c() # E: Name "c" is used before definition
245 class c: pass
246
247 def f1() -> None:
248 s = c() # No error.
249
250 def f2() -> None:
251 s = c() # E: Name "c" is used before definition
252 if int():
253 class c: pass
254
255 glob = c()
256 def f3(x: c = glob) -> None:
257 glob = 123
258
259 [case testVarFromOuterScopeRedefined]
260 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
261 x = 0
262
263 def f0() -> None:
264 y = x # E: Name "x" is used before definition
265 x = 0
266
267 def f1() -> None:
268 y = x # No error.
269
270 def f2() -> None:
271 y = x # E: Name "x" is used before definition
272 global x
273
274 def f3() -> None:
275 global x
276 y = x # No error.
277
278 def f4() -> None:
279 if int():
280 x = 0
281 y = x # E: Name "x" may be undefined
282
283 [case testFuncParams]
284 # flags: --enable-error-code possibly-undefined
285 def foo(a: int) -> None:
286 if int():
287 a = 2
288 x = a
289
290 [case testWhile]
291 # flags: --enable-error-code possibly-undefined
292 while int():
293 a = 1
294
295 x = a # E: Name "a" may be undefined
296
297 while int():
298 b = 1
299 else:
300 b = 2
301
302 y = b # No error.
303
304 while True:
305 c = 1
306 if int():
307 break
308 y = c # No error.
309
310 # This while loop doesn't have a `break` inside, so we know that the else must always get executed.
311 while int():
312 pass
313 else:
314 d = 1
315 y = d # No error.
316
317 while int():
318 if int():
319 break
320 else:
321 e = 1
322 # If a while loop has a `break`, it's possible that the else didn't get executed.
323 y = e # E: Name "e" may be undefined
324
325 while int():
326 while int():
327 if int():
328 break
329 else:
330 f = 1
331 else:
332 g = 2
333
334 y = f # E: Name "f" may be undefined
335 y = g
336
337 [case testForLoop]
338 # flags: --enable-error-code possibly-undefined
339 for x in [1, 2, 3]:
340 if x:
341 x = 1
342 y = x
343 else:
344 z = 2
345
346 a = z + y # E: Name "y" may be undefined
347
348 [case testReturn]
349 # flags: --enable-error-code possibly-undefined
350 def f1() -> int:
351 if int():
352 x = 1
353 else:
354 return 0
355 return x
356
357 def f2() -> int:
358 if int():
359 x = 1
360 elif int():
361 return 0
362 else:
363 x = 2
364 return x
365
366 def f3() -> int:
367 if int():
368 x = 1
369 elif int():
370 return 0
371 else:
372 y = 2
373 return x # E: Name "x" may be undefined
374
375 def f4() -> int:
376 if int():
377 x = 1
378 elif int():
379 return 0
380 else:
381 return 0
382 return x
383
384 def f5() -> int:
385 # This is a test against crashes.
386 if int():
387 return 1
388 if int():
389 return 2
390 else:
391 return 3
392 return 1
393
394 def f6() -> int:
395 if int():
396 x = 0
397 return x
398 return x # E: Name "x" may be undefined
399
400 [case testDefinedDifferentBranchUsedBeforeDef]
401 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
402
403 def f0() -> None:
404 if int():
405 x = 0
406 else:
407 y = x # E: Name "x" is used before definition
408 z = x # E: Name "x" is used before definition
409
410 def f1() -> None:
411 x = 1
412 if int():
413 x = 0
414 else:
415 y = x # No error.
416
417 def f2() -> None:
418 if int():
419 x = 0
420 elif int():
421 y = x # E: Name "x" is used before definition
422 else:
423 y = x # E: Name "x" is used before definition
424 if int():
425 z = x # E: Name "x" is used before definition
426 x = 1
427 else:
428 x = 2
429 w = x # No error.
430
431 [case testPossiblyUndefinedLoop]
432 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
433
434 def f0() -> None:
435 first_iter = True
436 for i in [0, 1]:
437 if first_iter:
438 first_iter = False
439 x = 0
440 elif int():
441 # This is technically a false positive but mypy isn't smart enough for this yet.
442 y = x # E: Name "x" may be undefined
443 else:
444 y = x # E: Name "x" may be undefined
445 if int():
446 z = x # E: Name "x" may be undefined
447 x = 1
448 else:
449 x = 2
450 w = x # No error.
451
452 def f1() -> None:
453 while True:
454 if int():
455 x = 0
456 else:
457 y = x # E: Name "x" may be undefined
458 z = x # E: Name "x" may be undefined
459
460 def f2() -> None:
461 for i in [0, 1]:
462 x = i
463 else:
464 y = x # E: Name "x" may be undefined
465
466 def f3() -> None:
467 while int():
468 x = 1
469 else:
470 y = x # E: Name "x" may be undefined
471
472 def f4() -> None:
473 while int():
474 y = x # E: Name "x" may be undefined
475 x: int = 1
476
477 [case testAssert]
478 # flags: --enable-error-code possibly-undefined
479 def f1() -> int:
480 if int():
481 x = 1
482 else:
483 assert False, "something something"
484 return x
485
486 def f2() -> int:
487 if int():
488 x = 1
489 elif int():
490 assert False
491 else:
492 y = 2
493 return x # E: Name "x" may be undefined
494
495 [case testRaise]
496 # flags: --enable-error-code possibly-undefined
497 def f1() -> int:
498 if int():
499 x = 1
500 else:
501 raise BaseException("something something")
502 return x
503
504 def f2() -> int:
505 if int():
506 x = 1
507 elif int():
508 raise BaseException("something something")
509 else:
510 y = 2
511 return x # E: Name "x" may be undefined
512 [builtins fixtures/exception.pyi]
513
514 [case testContinue]
515 # flags: --enable-error-code possibly-undefined
516 def f1() -> int:
517 while int():
518 if int():
519 x = 1
520 else:
521 continue
522 y = x
523 else:
524 x = 2
525 return x
526
527 def f2() -> int:
528 while int():
529 if int():
530 x = 1
531 elif int():
532 pass
533 else:
534 continue
535 y = x # E: Name "x" may be undefined
536 return x # E: Name "x" may be undefined
537
538 def f3() -> None:
539 while True:
540 if int():
541 x = 2
542 elif int():
543 continue
544 else:
545 continue
546 y = x
547
548 [case testBreak]
549 # flags: --enable-error-code possibly-undefined
550 def f1() -> None:
551 while int():
552 if int():
553 x = 1
554 else:
555 break
556 y = x # No error -- x is always defined.
557
558 def f2() -> None:
559 while int():
560 if int():
561 x = 1
562 elif int():
563 pass
564 else:
565 break
566 y = x # E: Name "x" may be undefined
567
568 def f3() -> None:
569 while int():
570 x = 1
571 while int():
572 if int():
573 x = 2
574 else:
575 break
576 y = x
577 z = x # E: Name "x" may be undefined
578
579 [case testTryBasic]
580 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
581 def f1() -> int:
582 try:
583 x = 1
584 except:
585 pass
586 return x # E: Name "x" may be undefined
587
588 def f2() -> int:
589 try:
590 pass
591 except:
592 x = 1
593 return x # E: Name "x" may be undefined
594
595 def f3() -> int:
596 try:
597 x = 1
598 except:
599 y = x # E: Name "x" may be undefined
600 return x # E: Name "x" may be undefined
601
602 def f4() -> int:
603 try:
604 x = 1
605 except:
606 return 0
607 return x
608
609 def f5() -> int:
610 try:
611 x = 1
612 except:
613 raise
614 return x
615
616 def f6() -> None:
617 try:
618 pass
619 except BaseException as exc:
620 x = exc # No error.
621 exc = BaseException()
622 # This case is covered by the other check, not by possibly undefined check.
623 y = exc # E: Trying to read deleted variable "exc"
624
625 def f7() -> int:
626 try:
627 if int():
628 x = 1
629 assert False
630 except:
631 y = x # E: Name "x" may be undefined
632 return x # E: Name "x" may be undefined
633 [builtins fixtures/exception.pyi]
634
635 [case testTryMultiExcept]
636 # flags: --enable-error-code possibly-undefined
637 def f1() -> int:
638 try:
639 x = 1
640 except BaseException:
641 x = 2
642 except:
643 x = 3
644 return x
645
646 def f2() -> int:
647 try:
648 x = 1
649 except BaseException:
650 pass
651 except:
652 x = 3
653 return x # E: Name "x" may be undefined
654 [builtins fixtures/exception.pyi]
655
656 [case testTryFinally]
657 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
658 def f1() -> int:
659 try:
660 x = 1
661 finally:
662 x = 2
663 return x
664
665 def f2() -> int:
666 try:
667 pass
668 except:
669 pass
670 finally:
671 x = 2
672 return x
673
674 def f3() -> int:
675 try:
676 x = 1
677 except:
678 pass
679 finally:
680 y = x # E: Name "x" may be undefined
681 return x # E: Name "x" may be undefined
682
683 def f4() -> int:
684 try:
685 x = 0
686 except BaseException:
687 raise
688 finally:
689 y = x # E: Name "x" may be undefined
690 return y
691
692 def f5() -> int:
693 try:
694 if int():
695 x = 1
696 else:
697 return 0
698 finally:
699 pass
700 return x # No error.
701
702 def f6() -> int:
703 try:
704 if int():
705 x = 1
706 else:
707 return 0
708 finally:
709 a = x # E: Name "x" may be undefined
710 return a
711
712 def f7() -> int:
713 try:
714 if int():
715 x = 1
716 else:
717 raise BaseException
718 return x # No error.
719 finally:
720 y = x # E: Name "x" may be undefined
721 return 0
722 [builtins fixtures/exception.pyi]
723
724 [case testTryElse]
725 # flags: --enable-error-code possibly-undefined
726 def f1() -> int:
727 try:
728 return 0
729 except BaseException:
730 x = 1
731 else:
732 x = 2
733 finally:
734 y = x
735 return y
736
737 def f2() -> int:
738 try:
739 pass
740 except:
741 x = 1
742 else:
743 x = 2
744 return x
745
746 def f3() -> int:
747 try:
748 pass
749 except:
750 x = 1
751 else:
752 pass
753 return x # E: Name "x" may be undefined
754
755 def f4() -> int:
756 try:
757 x = 1
758 except:
759 x = 2
760 else:
761 pass
762 return x
763
764 def f5() -> int:
765 try:
766 pass
767 except:
768 x = 1
769 else:
770 return 1
771 return x
772 [builtins fixtures/exception.pyi]
773
774 [case testNoReturn]
775 # flags: --enable-error-code possibly-undefined
776
777 from typing import NoReturn
778 def fail() -> NoReturn:
779 assert False
780
781 def f() -> None:
782 if int():
783 x = 1
784 elif int():
785 x = 2
786 y = 3
787 else:
788 # This has a NoReturn type, so we can skip it.
789 fail()
790 z = y # E: Name "y" may be undefined
791 z = x
792
793 [case testDictComprehension]
794 # flags: --enable-error-code possibly-undefined
795
796 def f() -> None:
797 for _ in [1, 2]:
798 key = 2
799 val = 2
800
801 x = (
802 key, # E: Name "key" may be undefined
803 val, # E: Name "val" may be undefined
804 )
805
806 d = [(0, "a"), (1, "b")]
807 {val: key for key, val in d}
808 [builtins fixtures/dict.pyi]
809
810 [case testWithStmt]
811 # flags: --enable-error-code possibly-undefined
812 from contextlib import contextmanager
813
814 @contextmanager
815 def ctx(*args):
816 yield 1
817
818 def f() -> None:
819 if int():
820 a = b = 1
821 x = 1
822
823 with ctx() as a, ctx(a) as b, ctx(x) as x: # E: Name "x" may be undefined
824 c = a
825 c = b
826 d = a
827 d = b
828 [builtins fixtures/tuple.pyi]
829
830 [case testUnreachable]
831 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
832 import typing
833
834 def f0() -> None:
835 if typing.TYPE_CHECKING:
836 x = 1
837 elif int():
838 y = 1
839 else:
840 y = 2
841 a = x
842
843 def f1() -> None:
844 if not typing.TYPE_CHECKING:
845 pass
846 else:
847 z = 1
848 a = z
849
850 def f2() -> None:
851 if typing.TYPE_CHECKING:
852 x = 1
853 else:
854 y = x
855 [typing fixtures/typing-medium.pyi]
856
857 [case testUsedBeforeDef]
858 # flags: --enable-error-code used-before-def
859
860 def f0() -> None:
861 x = y # E: Name "y" is used before definition
862 y: int = 1
863
864 def f2() -> None:
865 if int():
866 pass
867 else:
868 # No used-before-def error.
869 y = z # E: Name "z" is not defined
870
871 def inner2() -> None:
872 z = 0
873
874 def f3() -> None:
875 if int():
876 pass
877 else:
878 y = z # E: Name "z" is used before definition
879 z: int = 2
880
881 def f4() -> None:
882 if int():
883 pass
884 else:
885 y = z # E: Name "z" is used before definition
886 x = z # E: Name "z" is used before definition
887 z: int = 2
888
889 [case testUsedBeforeDefImportsBasicImportNoError]
890 # flags: --enable-error-code used-before-def --enable-error-code possibly-undefined --disable-error-code no-redef
891 import foo # type: ignore
892
893 a = foo # No error.
894 foo: int = 1
895
896 [case testUsedBeforeDefImportsDotImport]
897 # flags: --enable-error-code used-before-def --enable-error-code possibly-undefined --disable-error-code no-redef
898 import x.y # type: ignore
899
900 a = y # E: Name "y" is used before definition
901 y: int = 1
902
903 b = x # No error.
904 x: int = 1
905
906 c = x.y # No error.
907 x: int = 1
908
909 [case testUsedBeforeDefImportBasicRename]
910 # flags: --enable-error-code used-before-def --disable-error-code=no-redef
911 import x.y as z # type: ignore
912 from typing import Any
913
914 a = z # No error.
915 z: int = 1
916
917 a = x # E: Name "x" is used before definition
918 x: int = 1
919
920 a = y # E: Name "y" is used before definition
921 y: int = 1
922
923 [case testUsedBeforeDefImportFrom]
924 # flags: --enable-error-code used-before-def --disable-error-code no-redef
925 from foo import x # type: ignore
926
927 a = x # No error.
928 x: int = 1
929
930 [case testUsedBeforeDefImportFromRename]
931 # flags: --enable-error-code used-before-def --disable-error-code no-redef
932 from foo import x as y # type: ignore
933
934 a = y # No error.
935 y: int = 1
936
937 a = x # E: Name "x" is used before definition
938 x: int = 1
939
940 [case testUsedBeforeDefFunctionDeclarations]
941 # flags: --enable-error-code used-before-def
942
943 def f0() -> None:
944 def inner() -> None:
945 pass
946
947 inner() # No error.
948 inner = lambda: None
949
950 [case testUsedBeforeDefBuiltinsFunc]
951 # flags: --enable-error-code used-before-def
952
953 def f0() -> None:
954 s = type(123) # E: Name "type" is used before definition
955 type = "abc"
956 a = type
957
958 def f1() -> None:
959 s = type(123)
960
961 [case testUsedBeforeDefBuiltinsGlobal]
962 # flags: --enable-error-code used-before-def
963
964 s = type(123)
965 type = "abc"
966 a = type
967
968 [case testUsedBeforeDefBuiltinsClass]
969 # flags: --enable-error-code used-before-def
970
971 class C:
972 s = type
973 type = s
974
975 [case testUsedBeforeDefBuiltinsGenerator]
976 # flags: --enable-error-code used-before-def
977
978 def f0() -> None:
979 _ = [type for type in [type("a"), type(1)]]
980
981 [case testUsedBeforeDefBuiltinsMultipass]
982 # flags: --enable-error-code used-before-def
983
984 # When doing multiple passes, mypy resolves references slightly differently.
985 # In this case, it would refer the earlier `type` call to the range class defined below.
986 _type = type # No error
987 _C = C # E: Name "C" is used before definition
988 class type: pass
989 class C: pass
990
991 [case testUsedBeforeDefImplicitModuleAttrs]
992 # flags: --enable-error-code used-before-def
993 a = __name__ # No error.
994 __name__ = "abc"
995
996 [case testUntypedDef]
997 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
998
999 def f():
1000 if int():
1001 x = 0
1002 z = y # No used-before-def error because def is untyped.
1003 y = x # No possibly-undefined error because def is untyped.
1004
1005 [case testUntypedDefCheckUntypedDefs]
1006 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def --check-untyped-defs
1007
1008 def f():
1009 if int():
1010 x = 0
1011 z = y # E: Name "y" is used before definition
1012 y: int = x # E: Name "x" may be undefined
1013
1014 [case testClassBody]
1015 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
1016
1017 class A:
1018 # The following should not only trigger an error from semantic analyzer, but not the used-before-def check.
1019 y = x + 1 # E: Name "x" is not defined
1020 x = 0
1021 # Same as above but in a loop, which should trigger a possibly-undefined error.
1022 for _ in [1, 2, 3]:
1023 b = a + 1 # E: Name "a" is not defined
1024 a = 0
1025
1026
1027 class B:
1028 if int():
1029 x = 0
1030 else:
1031 # This type of check is not caught by the semantic analyzer. If we ever update it to catch such issues,
1032 # we should make sure that errors are not double-reported.
1033 y = x # E: Name "x" is used before definition
1034 for _ in [1, 2, 3]:
1035 if int():
1036 a = 0
1037 else:
1038 # Same as above but in a loop.
1039 b = a # E: Name "a" may be undefined
1040
1041 [case testUnreachableCausingMissingTypeMap]
1042 # flags: --enable-error-code possibly-undefined --enable-error-code used-before-def --no-warn-unreachable
1043 # Regression test for https://github.com/python/mypy/issues/15958
1044 from typing import Union, NoReturn
1045
1046 def assert_never(__x: NoReturn) -> NoReturn: ...
1047
1048 def foo(x: Union[int, str]) -> None:
1049 if isinstance(x, str):
1050 f = "foo"
1051 elif isinstance(x, int):
1052 f = "bar"
1053 else:
1054 assert_never(x)
1055 f # OK
1056 [builtins fixtures/tuple.pyi]
1057
1058 [case testForElseWithBreakInTryExceptContinue]
1059 # flags: --enable-error-code possibly-undefined
1060 # Test for issue where variable defined before break in try block
1061 # was incorrectly reported as undefined when except has continue
1062 def random() -> float: return 0.5
1063
1064 if random():
1065 for i in range(10):
1066 try:
1067 value = random()
1068 break
1069 except Exception:
1070 continue
1071 else:
1072 raise RuntimeError
1073
1074 print(value) # Should not error - value is defined if we broke
1075 else:
1076 value = random()
1077
1078 print(value) # Should not error
1079 [builtins fixtures/for_else_exception.pyi]
1080
1081 [case testForElseWithBreakInTryExceptContinueNoIf]
1082 # flags: --enable-error-code possibly-undefined
1083 # Simpler version without if statement
1084 def random() -> float: return 0.5
1085
1086 for i in range(10):
1087 try:
1088 value = random()
1089 break
1090 except Exception:
1091 continue
1092 else:
1093 raise RuntimeError
1094
1095 print(value) # Should not error
1096 [builtins fixtures/for_else_exception.pyi]
1097
1098 [case testForElseWithBreakInTryExceptPass]
1099 # flags: --enable-error-code possibly-undefined
1100 # Version with pass instead of continue - should also work
1101 def random() -> float: return 0.5
1102
1103 if random():
1104 for i in range(10):
1105 try:
1106 value = random()
1107 break
1108 except Exception:
1109 pass
1110 else:
1111 raise RuntimeError
1112
1113 print(value) # Should not error
1114 else:
1115 value = random()
1116
1117 print(value) # Should not error
1118 [builtins fixtures/for_else_exception.pyi]
1119
1120 [case testForElseWithConditionalDefBeforeBreak]
1121 # flags: --enable-error-code possibly-undefined
1122 # Test that conditional definition before break still works correctly
1123 def random() -> float: return 0.5
1124
1125 if random():
1126 for i in range(10):
1127 try:
1128 if i > 10:
1129 value = random()
1130 break
1131 except Exception:
1132 continue
1133 else:
1134 raise RuntimeError
1135
1136 print(value) # Should not error (though might be undefined at runtime)
1137 else:
1138 value = random()
1139
1140 print(value) # Should not error
1141 [builtins fixtures/for_else_exception.pyi]
1142
1143 [case testForElseDefineInBothBranches]
1144 # flags: --enable-error-code possibly-undefined
1145 # Test that variable defined in both for break and else branches is not undefined
1146 for i in range(10):
1147 if i:
1148 value = i
1149 break
1150 else:
1151 value = 1
1152
1153 print(value) # Should not error - value is defined in all paths
1154 [builtins fixtures/for_else_exception.pyi]
1155
1156 [case testForElseWithWalrusInBreak]
1157 # flags: --enable-error-code possibly-undefined
1158 # Test with walrus operator in if condition before break
1159 def random() -> float: return 0.5
1160
1161 if random():
1162 for i in range(10):
1163 if value := random():
1164 break
1165 else:
1166 raise RuntimeError
1167
1168 print(value) # Should not error - value is defined if we broke
1169 else:
1170 value = random()
1171
1172 print(value) # Should not error
1173 [builtins fixtures/for_else_exception.pyi]
1174
1175 [case testForElseWithWalrusInBreakConditionalRaise]
1176 # flags: --enable-error-code possibly-undefined
1177 # Test with walrus operator in if condition before break
1178 def random() -> float: return 0.5
1179
1180 def f1() -> None:
1181 if random():
1182 for i in range(10):
1183 if value := random():
1184 break
1185 else:
1186 if random() > 0.5: # E: Unsupported left operand type for > ("float")
1187 raise RuntimeError
1188
1189 print(value) # E: Name "value" may be undefined
1190 else:
1191 value = random()
1192
1193 print(value) # Already emitted error
1194
1195
1196 def f2() -> None:
1197 if random():
1198 for i in range(10):
1199 if value := random():
1200 break
1201 else:
1202 if random() > 0.5: # E: Unsupported left operand type for > ("float")
1203 raise RuntimeError
1204 else:
1205 value = random()
1206
1207 print(value) # E: Name "value" may be undefined
1208 [builtins fixtures/for_else_exception.pyi]
1209
1210 [case testOmittedUnrequiredElse]
1211 # flags: --enable-error-code possibly-undefined
1212 from typing import Literal
1213
1214 a: Literal[True]
1215 if a:
1216 w = 1
1217 w + 1
1218
1219 b: bool
1220 if b:
1221 x = 1
1222 elif not b:
1223 x = 2
1224 x + 1
1225
1226 if b:
1227 y = 1
1228 elif not b:
1229 if a:
1230 z = 2
1231 y = z
1232 y + 1