diff --git a/parso/python/parser.py b/parso/python/parser.py index ebb2969..e0b7e1c 100644 --- a/parso/python/parser.py +++ b/parso/python/parser.py @@ -139,7 +139,8 @@ class Parser(BaseParser): else: last_leaf = None - if typ == ENDMARKER or typ == DEDENT and '\n' not in last_leaf.value: + if self._start_symbol == 'file_input' and \ + (typ == ENDMARKER or typ == DEDENT and '\n' not in last_leaf.value): def reduce_stack(states, newstate): # reduce state = newstate diff --git a/parso/python/tree.py b/parso/python/tree.py index 9c87774..207481c 100644 --- a/parso/python/tree.py +++ b/parso/python/tree.py @@ -73,7 +73,7 @@ class PythonMixin(object): scope = self while scope.parent is not None: parent = scope.parent - if isinstance(scope, (PythonNode, PythonLeaf)) and parent.type != 'simple_stmt': + if isinstance(scope, (PythonNode, PythonLeaf)) and parent.type not in ('simple_stmt', 'file_input'): if scope.type == 'testlist_comp': try: if scope.children[1].type == 'comp_for': diff --git a/test/test_parser.py b/test/test_parser.py index ae0c425..b2a0331 100644 --- a/test/test_parser.py +++ b/test/test_parser.py @@ -184,3 +184,18 @@ def test_dedent_at_end(): suite = module.children[0].children[-1] foobar = suite.children[-1] assert foobar.type == 'name' + + +def test_no_error_nodes(): + def check(node): + assert node.type not in ('error_leaf', 'error_node') + + try: + children = node.children + except AttributeError: + pass + else: + for child in children: + check(child) + + check(parse("if foo:\n bar"))