Skip to content

Does not catch variable declared global/nonlocal after appearing in the same scope. #13560

Open
@mrolle45

Description

@mrolle45

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): int and x: int is in the value of ast3.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)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions