-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
bugmypy got something wrongmypy got something wrongtopic-runtime-semanticsmypy doesn't model runtime semantics correctlymypy doesn't model runtime semantics correctlytopic-variable-scope
Description
Bug Report
A class def containing a nonlocal declaration is flagged as an error, even though this is legal Python.
To Reproduce
X.py:
def f():
a = 0
class C:
nonlocal a
a = 1
mypy output:
error: nonlocal declaration not allowed at module level
Fix
Change one line in SemanticAnalyzer.visit_nonlocal_decl
from:
def visit_nonlocal_decl(self, d: NonlocalDecl) -> None:
self.statement = d
! if not self.is_func_scope():
self.fail("nonlocal declaration not allowed at module level", d)
...
to:
def visit_nonlocal_decl(self, d: NonlocalDecl) -> None:
self.statement = d
! if self.is_module_scope():
self.fail("nonlocal declaration not allowed at module level", d)
...
The error message now disappears. Note, it still appears at the module level, as it is a SyntaxError in Python.
Your Environment
- Mypy version used: 0.931
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-runtime-semanticsmypy doesn't model runtime semantics correctlymypy doesn't model runtime semantics correctlytopic-variable-scope