diff --git a/parso/python/normalizer.py b/parso/python/normalizer.py index 9afe50b..f1ededb 100644 --- a/parso/python/normalizer.py +++ b/parso/python/normalizer.py @@ -313,6 +313,10 @@ class ErrorFinder(Normalizer): # f(+x=1) message = "keyword can't be an expression" self._add_syntax_error(message, first) + elif node.type == 'nonlocal_stmt': + if self._context.parent_context is None: + message = "nonlocal declaration not allowed at module level" + self._add_syntax_error(message, node) yield diff --git a/test/normalizer_issue_files/allowed_syntax.py b/test/normalizer_issue_files/allowed_syntax.py index 2db3cce..2ed0d48 100644 --- a/test/normalizer_issue_files/allowed_syntax.py +++ b/test/normalizer_issue_files/allowed_syntax.py @@ -45,3 +45,7 @@ except: pass except ZeroDivisionError: pass + + +class X(): + nonlocal a diff --git a/test/test_python_errors.py b/test/test_python_errors.py index 4f428d0..615a3f3 100644 --- a/test/test_python_errors.py +++ b/test/test_python_errors.py @@ -105,6 +105,7 @@ def test_indentation_errors(code, positions): # SyntaxErrors from Python/symtable.c 'def f(x, x): pass', 'def x(): from math import *', + 'nonlocal a', # IndentationError ' foo',