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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
|
[case testDefinedInOneBranch]
# flags: --enable-error-code possibly-undefined
if int():
a = 1
else:
x = 2
z = a + 1 # E: Name "a" may be undefined
z = a + 1 # We only report the error on first occurrence.
[case testElif]
# flags: --enable-error-code possibly-undefined
if int():
a = 1
elif int():
a = 2
else:
x = 3
z = a + 1 # E: Name "a" may be undefined
[case testUsedInIf]
# flags: --enable-error-code possibly-undefined
if int():
y = 1
if int():
x = y # E: Name "y" may be undefined
[case testDefinedInAllBranches]
# flags: --enable-error-code possibly-undefined
if int():
a = 1
elif int():
a = 2
else:
a = 3
z = a + 1
[case testOmittedElse]
# flags: --enable-error-code possibly-undefined
if int():
a = 1
z = a + 1 # E: Name "a" may be undefined
[case testUpdatedInIf]
# flags: --enable-error-code possibly-undefined
# Variable a is already defined. Just updating it in an "if" is acceptable.
a = 1
if int():
a = 2
z = a + 1
[case testNestedIf]
# flags: --enable-error-code possibly-undefined
if int():
if int():
a = 1
x = 1
x = x + 1
else:
a = 2
b = a + x # E: Name "x" may be undefined
b = b + 1
else:
b = 2
z = a + b # E: Name "a" may be undefined
[case testVeryNestedIf]
# flags: --enable-error-code possibly-undefined
if int():
if int():
if int():
a = 1
else:
a = 2
x = a
else:
a = 2
b = a
else:
b = 2
z = a + b # E: Name "a" may be undefined
[case testTupleUnpack]
# flags: --enable-error-code possibly-undefined
if int():
(x, y) = (1, 2)
else:
[y, z] = [1, 2]
a = y + x # E: Name "x" may be undefined
a = y + z # E: Name "z" may be undefined
[case testIndexExpr]
# flags: --enable-error-code possibly-undefined
if int():
*x, y = (1, 2)
else:
x = [1, 2]
a = x # No error.
b = y # E: Name "y" may be undefined
[case testRedefined]
# flags: --enable-error-code possibly-undefined
y = 3
if int():
if int():
y = 2
x = y + 2
else:
if int():
y = 2
x = y + 2
x = y + 2
[case testFunction]
# flags: --enable-error-code possibly-undefined
def f0() -> None:
if int():
def some_func() -> None:
pass
some_func() # E: Name "some_func" may be undefined
def f1() -> None:
if int():
def some_func() -> None:
pass
else:
def some_func() -> None:
pass
some_func() # No error.
[case testLambda]
# flags: --enable-error-code possibly-undefined
def f0(b: bool) -> None:
if b:
fn = lambda: 2
y = fn # E: Name "fn" may be undefined
[case testUsedBeforeDefClass]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
def f(x: A): # No error here.
pass
y = A() # E: Name "A" is used before definition
class A: pass
[case testClassScope]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
class C:
x = 0
def f0(self) -> None: pass
def f2(self) -> None:
f0() # No error.
self.f0() # No error.
f0() # E: Name "f0" is used before definition
def f0() -> None: pass
y = x # E: Name "x" is used before definition
x = 1
[case testClassInsideFunction]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
def f() -> None:
class C: pass
c = C() # E: Name "C" is used before definition
class C: pass
[case testUsedBeforeDefFunc]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
foo() # E: Name "foo" is used before definition
def foo(): pass
[case testGenerator]
# flags: --enable-error-code possibly-undefined
if int():
a = 3
s = [a + 1 for a in [1, 2, 3]]
x = a # E: Name "a" may be undefined
[case testScope]
# flags: --enable-error-code possibly-undefined
def foo() -> None:
if int():
y = 2
if int():
y = 3
x = y # E: Name "y" may be undefined
[case testVarDefinedInOuterScopeUpdated]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
def f0() -> None:
global x
y = x
x = 1 # No error.
x = 2
[case testNonlocalVar]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
def f0() -> None:
x = 2
def inner() -> None:
nonlocal x
y = x
x = 1 # No error.
[case testGlobalDeclarationAfterUsage]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
def f0() -> None:
y = x # E: Name "x" is used before definition
global x
x = 1 # No error.
x = 2
[case testVarDefinedInOuterScope]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
def f0() -> None:
global x
y = x # We do not detect such errors right now.
f0()
x = 1
[case testDefinedInOuterScopeNoError]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
def foo() -> None:
bar()
def bar() -> None:
foo()
[case testClassFromOuterScopeRedefined]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
class c: pass
def f0() -> None:
s = c() # E: Name "c" is used before definition
class c: pass
def f1() -> None:
s = c() # No error.
def f2() -> None:
s = c() # E: Name "c" is used before definition
if int():
class c: pass
glob = c()
def f3(x: c = glob) -> None:
glob = 123
[case testVarFromOuterScopeRedefined]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
x = 0
def f0() -> None:
y = x # E: Name "x" is used before definition
x = 0
def f1() -> None:
y = x # No error.
def f2() -> None:
y = x # E: Name "x" is used before definition
global x
def f3() -> None:
global x
y = x # No error.
def f4() -> None:
if int():
x = 0
y = x # E: Name "x" may be undefined
[case testFuncParams]
# flags: --enable-error-code possibly-undefined
def foo(a: int) -> None:
if int():
a = 2
x = a
[case testWhile]
# flags: --enable-error-code possibly-undefined
while int():
a = 1
x = a # E: Name "a" may be undefined
while int():
b = 1
else:
b = 2
y = b # No error.
while True:
c = 1
if int():
break
y = c # No error.
# This while loop doesn't have a `break` inside, so we know that the else must always get executed.
while int():
pass
else:
d = 1
y = d # No error.
while int():
if int():
break
else:
e = 1
# If a while loop has a `break`, it's possible that the else didn't get executed.
y = e # E: Name "e" may be undefined
while int():
while int():
if int():
break
else:
f = 1
else:
g = 2
y = f # E: Name "f" may be undefined
y = g
[case testForLoop]
# flags: --enable-error-code possibly-undefined
for x in [1, 2, 3]:
if x:
x = 1
y = x
else:
z = 2
a = z + y # E: Name "y" may be undefined
[case testReturn]
# flags: --enable-error-code possibly-undefined
def f1() -> int:
if int():
x = 1
else:
return 0
return x
def f2() -> int:
if int():
x = 1
elif int():
return 0
else:
x = 2
return x
def f3() -> int:
if int():
x = 1
elif int():
return 0
else:
y = 2
return x # E: Name "x" may be undefined
def f4() -> int:
if int():
x = 1
elif int():
return 0
else:
return 0
return x
def f5() -> int:
# This is a test against crashes.
if int():
return 1
if int():
return 2
else:
return 3
return 1
def f6() -> int:
if int():
x = 0
return x
return x # E: Name "x" may be undefined
[case testDefinedDifferentBranchUsedBeforeDef]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
def f0() -> None:
if int():
x = 0
else:
y = x # E: Name "x" is used before definition
z = x # E: Name "x" is used before definition
def f1() -> None:
x = 1
if int():
x = 0
else:
y = x # No error.
def f2() -> None:
if int():
x = 0
elif int():
y = x # E: Name "x" is used before definition
else:
y = x # E: Name "x" is used before definition
if int():
z = x # E: Name "x" is used before definition
x = 1
else:
x = 2
w = x # No error.
[case testPossiblyUndefinedLoop]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
def f0() -> None:
first_iter = True
for i in [0, 1]:
if first_iter:
first_iter = False
x = 0
elif int():
# This is technically a false positive but mypy isn't smart enough for this yet.
y = x # E: Name "x" may be undefined
else:
y = x # E: Name "x" may be undefined
if int():
z = x # E: Name "x" may be undefined
x = 1
else:
x = 2
w = x # No error.
def f1() -> None:
while True:
if int():
x = 0
else:
y = x # E: Name "x" may be undefined
z = x # E: Name "x" may be undefined
def f2() -> None:
for i in [0, 1]:
x = i
else:
y = x # E: Name "x" may be undefined
def f3() -> None:
while int():
x = 1
else:
y = x # E: Name "x" may be undefined
def f4() -> None:
while int():
y = x # E: Name "x" may be undefined
x: int = 1
[case testAssert]
# flags: --enable-error-code possibly-undefined
def f1() -> int:
if int():
x = 1
else:
assert False, "something something"
return x
def f2() -> int:
if int():
x = 1
elif int():
assert False
else:
y = 2
return x # E: Name "x" may be undefined
[case testRaise]
# flags: --enable-error-code possibly-undefined
def f1() -> int:
if int():
x = 1
else:
raise BaseException("something something")
return x
def f2() -> int:
if int():
x = 1
elif int():
raise BaseException("something something")
else:
y = 2
return x # E: Name "x" may be undefined
[builtins fixtures/exception.pyi]
[case testContinue]
# flags: --enable-error-code possibly-undefined
def f1() -> int:
while int():
if int():
x = 1
else:
continue
y = x
else:
x = 2
return x
def f2() -> int:
while int():
if int():
x = 1
elif int():
pass
else:
continue
y = x # E: Name "x" may be undefined
return x # E: Name "x" may be undefined
def f3() -> None:
while True:
if int():
x = 2
elif int():
continue
else:
continue
y = x
[case testBreak]
# flags: --enable-error-code possibly-undefined
def f1() -> None:
while int():
if int():
x = 1
else:
break
y = x # No error -- x is always defined.
def f2() -> None:
while int():
if int():
x = 1
elif int():
pass
else:
break
y = x # E: Name "x" may be undefined
def f3() -> None:
while int():
x = 1
while int():
if int():
x = 2
else:
break
y = x
z = x # E: Name "x" may be undefined
[case testTryBasic]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
def f1() -> int:
try:
x = 1
except:
pass
return x # E: Name "x" may be undefined
def f2() -> int:
try:
pass
except:
x = 1
return x # E: Name "x" may be undefined
def f3() -> int:
try:
x = 1
except:
y = x # E: Name "x" may be undefined
return x # E: Name "x" may be undefined
def f4() -> int:
try:
x = 1
except:
return 0
return x
def f5() -> int:
try:
x = 1
except:
raise
return x
def f6() -> None:
try:
pass
except BaseException as exc:
x = exc # No error.
exc = BaseException()
# This case is covered by the other check, not by possibly undefined check.
y = exc # E: Trying to read deleted variable "exc"
def f7() -> int:
try:
if int():
x = 1
assert False
except:
pass
return x # E: Name "x" may be undefined
[builtins fixtures/exception.pyi]
[case testTryMultiExcept]
# flags: --enable-error-code possibly-undefined
def f1() -> int:
try:
x = 1
except BaseException:
x = 2
except:
x = 3
return x
def f2() -> int:
try:
x = 1
except BaseException:
pass
except:
x = 3
return x # E: Name "x" may be undefined
[builtins fixtures/exception.pyi]
[case testTryFinally]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
def f1() -> int:
try:
x = 1
finally:
x = 2
return x
def f2() -> int:
try:
pass
except:
pass
finally:
x = 2
return x
def f3() -> int:
try:
x = 1
except:
pass
finally:
y = x # E: Name "x" may be undefined
return x
def f4() -> int:
try:
x = 0
except BaseException:
raise
finally:
y = x # E: Name "x" may be undefined
return y
def f5() -> int:
try:
if int():
x = 1
else:
return 0
finally:
pass
return x # No error.
def f6() -> int:
try:
if int():
x = 1
else:
return 0
finally:
a = x # E: Name "x" may be undefined
return a
[builtins fixtures/exception.pyi]
[case testTryElse]
# flags: --enable-error-code possibly-undefined
def f1() -> int:
try:
return 0
except BaseException:
x = 1
else:
x = 2
finally:
y = x
return y
def f2() -> int:
try:
pass
except:
x = 1
else:
x = 2
return x
def f3() -> int:
try:
pass
except:
x = 1
else:
pass
return x # E: Name "x" may be undefined
def f4() -> int:
try:
x = 1
except:
x = 2
else:
pass
return x
def f5() -> int:
try:
pass
except:
x = 1
else:
return 1
return x
[builtins fixtures/exception.pyi]
[case testNoReturn]
# flags: --enable-error-code possibly-undefined
from typing import NoReturn
def fail() -> NoReturn:
assert False
def f() -> None:
if int():
x = 1
elif int():
x = 2
y = 3
else:
# This has a NoReturn type, so we can skip it.
fail()
z = y # E: Name "y" may be undefined
z = x
[case testDictComprehension]
# flags: --enable-error-code possibly-undefined
def f() -> None:
for _ in [1, 2]:
key = 2
val = 2
x = (
key, # E: Name "key" may be undefined
val, # E: Name "val" may be undefined
)
d = [(0, "a"), (1, "b")]
{val: key for key, val in d}
[builtins fixtures/dict.pyi]
[case testWithStmt]
# flags: --enable-error-code possibly-undefined
from contextlib import contextmanager
@contextmanager
def ctx(*args):
yield 1
def f() -> None:
if int():
a = b = 1
x = 1
with ctx() as a, ctx(a) as b, ctx(x) as x: # E: Name "x" may be undefined
c = a
c = b
d = a
d = b
[builtins fixtures/tuple.pyi]
[case testUnreachable]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
import typing
def f0() -> None:
if typing.TYPE_CHECKING:
x = 1
elif int():
y = 1
else:
y = 2
a = x
def f1() -> None:
if not typing.TYPE_CHECKING:
pass
else:
z = 1
a = z
def f2() -> None:
if typing.TYPE_CHECKING:
x = 1
else:
y = x
[typing fixtures/typing-medium.pyi]
[case testUsedBeforeDef]
# flags: --enable-error-code used-before-def
def f0() -> None:
x = y # E: Name "y" is used before definition
y: int = 1
def f2() -> None:
if int():
pass
else:
# No used-before-def error.
y = z # E: Name "z" is not defined
def inner2() -> None:
z = 0
def f3() -> None:
if int():
pass
else:
y = z # E: Name "z" is used before definition
z: int = 2
def f4() -> None:
if int():
pass
else:
y = z # E: Name "z" is used before definition
x = z # E: Name "z" is used before definition
z: int = 2
[case testUsedBeforeDefImportsBasicImportNoError]
# flags: --enable-error-code used-before-def --enable-error-code possibly-undefined --disable-error-code no-redef
import foo # type: ignore
a = foo # No error.
foo: int = 1
[case testUsedBeforeDefImportsDotImport]
# flags: --enable-error-code used-before-def --enable-error-code possibly-undefined --disable-error-code no-redef
import x.y # type: ignore
a = y # E: Name "y" is used before definition
y: int = 1
b = x # No error.
x: int = 1
c = x.y # No error.
x: int = 1
[case testUsedBeforeDefImportBasicRename]
# flags: --enable-error-code used-before-def --disable-error-code=no-redef
import x.y as z # type: ignore
from typing import Any
a = z # No error.
z: int = 1
a = x # E: Name "x" is used before definition
x: int = 1
a = y # E: Name "y" is used before definition
y: int = 1
[case testUsedBeforeDefImportFrom]
# flags: --enable-error-code used-before-def --disable-error-code no-redef
from foo import x # type: ignore
a = x # No error.
x: int = 1
[case testUsedBeforeDefImportFromRename]
# flags: --enable-error-code used-before-def --disable-error-code no-redef
from foo import x as y # type: ignore
a = y # No error.
y: int = 1
a = x # E: Name "x" is used before definition
x: int = 1
[case testUsedBeforeDefFunctionDeclarations]
# flags: --enable-error-code used-before-def
def f0() -> None:
def inner() -> None:
pass
inner() # No error.
inner = lambda: None
[case testUsedBeforeDefBuiltinsFunc]
# flags: --enable-error-code used-before-def
def f0() -> None:
s = type(123) # E: Name "type" is used before definition
type = "abc"
a = type
def f1() -> None:
s = type(123)
[case testUsedBeforeDefBuiltinsGlobal]
# flags: --enable-error-code used-before-def
s = type(123)
type = "abc"
a = type
[case testUsedBeforeDefBuiltinsClass]
# flags: --enable-error-code used-before-def
class C:
s = type
type = s
[case testUsedBeforeDefBuiltinsGenerator]
# flags: --enable-error-code used-before-def
def f0() -> None:
_ = [type for type in [type("a"), type(1)]]
[case testUsedBeforeDefBuiltinsMultipass]
# flags: --enable-error-code used-before-def
# When doing multiple passes, mypy resolves references slightly differently.
# In this case, it would refer the earlier `type` call to the range class defined below.
_type = type # No error
_C = C # E: Name "C" is used before definition
class type: pass
class C: pass
[case testUsedBeforeDefImplicitModuleAttrs]
# flags: --enable-error-code used-before-def
a = __name__ # No error.
__name__ = "abc"
[case testUntypedDef]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
def f():
if int():
x = 0
z = y # No used-before-def error because def is untyped.
y = x # No possibly-undefined error because def is untyped.
[case testUntypedDefCheckUntypedDefs]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def --check-untyped-defs
def f():
if int():
x = 0
z = y # E: Name "y" is used before definition
y: int = x # E: Name "x" may be undefined
[case testClassBody]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def
class A:
# The following should not only trigger an error from semantic analyzer, but not the used-before-def check.
y = x + 1 # E: Name "x" is not defined
x = 0
# Same as above but in a loop, which should trigger a possibly-undefined error.
for _ in [1, 2, 3]:
b = a + 1 # E: Name "a" is not defined
a = 0
class B:
if int():
x = 0
else:
# This type of check is not caught by the semantic analyzer. If we ever update it to catch such issues,
# we should make sure that errors are not double-reported.
y = x # E: Name "x" is used before definition
for _ in [1, 2, 3]:
if int():
a = 0
else:
# Same as above but in a loop.
b = a # E: Name "a" may be undefined
[case testUnreachableCausingMissingTypeMap]
# flags: --enable-error-code possibly-undefined --enable-error-code used-before-def --no-warn-unreachable
# Regression test for https://github.com/python/mypy/issues/15958
from typing import Union, NoReturn
def assert_never(__x: NoReturn) -> NoReturn: ...
def foo(x: Union[int, str]) -> None:
if isinstance(x, str):
f = "foo"
elif isinstance(x, int):
f = "bar"
else:
assert_never(x)
f # OK
[builtins fixtures/tuple.pyi]
|