Does not catch variable declared global/nonlocal after appearing in the same scope.
Bug Report
If a program references a variable and then declares it global or nonlocal, mypy does not catch this.
To Reproduce
foo.py:
x = 'x'
from typing import TYPE_CHECKING
def foo() -> None:
x = 42
global x
if TYPE_CHECKING: reveal_type(x)
print(x)
foo()
$ mypy foo.py
foo.py:8: note: Revealed type is 'builtins.str'
$ python3 foo.py
File "foo.py", line 7
global x
^
SyntaxError: name 'x' is assigned to before global declaration
Expected Behavior
mypy should report the error on line 7. This applies to any other usage of 'x',
-
except
(x): int(with no assignment)
which is not a syntax error, as it is not a name binding for 'x'. When you fix mypy to catch these syntax errors, be sure and take this into account. The only difference between(x): intandx: intis in the value ofast3.AnnAssign.simple= 0 and 1, resp.ast3.AnnAssign.simple= 0 in both cases if there is an assignment.
Actual Behavior
No error (see above).
Suggestion
I would guess that the fix belongs in the semantic analyzer. When it encounters a global or nonlocal declaration, it should know that the variable name has already been used or bound.
Don't forget to check for ast3.AnnAssign.simple = 1 and not consider the variable to be bound, or even used, in this case.
Note that ast3.AnnAssign.target is an ast3.Name instance, and ast3.AnnAssign.target.expr_context is an ast3.Store instance. This is a bug in the parser. It should be ast3.Load(), or better yet, None, since the variable is not really used at all.
Your Environment
- Mypy version used: 0.761
- Python version used: 3.8.10
- Operating system and version: Ubuntu (WSL on Windows)
I believe a related issue happens with non-locals defined after the nested scope:
from collections.abc import Callable
def outer (x: int) -> Callable[[], int]:
def inner () -> int:
nonlocal a
a += 1
return a
a = x
return inner
This is perfectly valid Python code (it runs as expected). However, Mypy 0.971 on CPython 3.10.7 reports the following:
nonlocal-example.py:5: error: No binding for nonlocal "a" found
Found 1 error in 1 file (checked 1 source file)