mypy
mypy copied to clipboard
mypy gives 'not defined' error for use after global declaration
In version 0.630 and master (with Python 3.7.0), I get this error :
def foo() -> None:
global myglobal
myglobal = 2 # error: Name 'myglobal' is not defined
I thought that I should not get an error. Is this behavior correct?
Mypy doesn't behave correctly, but it may be somewhat difficult to fix in mypy. As a workaround, you can declare the global at module top level:
myglobal: int
def foo() -> None:
global myglobal
myglobal = 2 # OK
Mypy doesn't behave correctly, but it may be somewhat difficult to fix in mypy. As a workaround, you can declare the global at module top level:
myglobal: int def foo() -> None: global myglobal myglobal = 2 # OK
Looks like you don't need global myglobal with this.