From e0485b032e95e2f969514ee78a8381a0e7e50ae4 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 2 Jun 2017 00:00:31 +0200 Subject: [PATCH] Fix some stuff to make parso work again. --- jedi/api/__init__.py | 2 +- jedi/api/helpers.py | 1 + jedi/evaluate/__init__.py | 7 +++---- test/test_evaluate/test_precedence.py | 2 +- test/test_evaluate/test_sys_path.py | 2 +- test/test_parso_integration/test_parser_utils.py | 6 ++++-- 6 files changed, 11 insertions(+), 9 deletions(-) diff --git a/jedi/api/__init__.py b/jedi/api/__init__.py index 0ffce5bf..de4fa3e6 100644 --- a/jedi/api/__init__.py +++ b/jedi/api/__init__.py @@ -458,7 +458,7 @@ def names(source=None, path=None, encoding='utf-8', all_scopes=False, classes.Definition( script._evaluator, TreeNameDefinition( - module_context.create_context(name.parent), + module_context.create_context(name if name.parent.type == 'file_input' else name.parent), name ) ) for name in get_module_names(script._get_module_node(), all_scopes) diff --git a/jedi/api/helpers.py b/jedi/api/helpers.py index 48f5dcba..3732d03c 100644 --- a/jedi/api/helpers.py +++ b/jedi/api/helpers.py @@ -98,6 +98,7 @@ def _get_code_for_stack(code_lines, module_node, position): user_stmt = leaf.parent else: user_stmt = leaf.get_definition() + if user_stmt.parent.type == 'simple_stmt': user_stmt = user_stmt.parent diff --git a/jedi/evaluate/__init__.py b/jedi/evaluate/__init__.py index 8f23ffe9..f0ae92cd 100644 --- a/jedi/evaluate/__init__.py +++ b/jedi/evaluate/__init__.py @@ -449,10 +449,9 @@ class Evaluator(object): if is_simple_name: if name.parent.type == 'classdef' and name.parent.name == name: return [er.ClassContext(self, name.parent, context)] - elif name.parent.type == 'funcdef': + elif name.parent.type == 'funcdef' and name.parent.name == name: return [er.FunctionContext(self, context, name.parent)] - elif name.parent.type == 'file_input': - raise NotImplementedError + if def_.type == 'expr_stmt' and name in def_.get_defined_names(): return self.eval_statement(context, def_, name) elif def_.type == 'for_stmt' and \ @@ -592,7 +591,7 @@ class Evaluator(object): if node_is_context and parser_utils.is_scope(node): scope_node = node else: - if node.parent.type in ('funcdef', 'classdef'): + if node.parent.type in ('funcdef', 'classdef') and node.parent.name == node: # When we're on class/function names/leafs that define the # object itself and not its contents. node = node.parent diff --git a/test/test_evaluate/test_precedence.py b/test/test_evaluate/test_precedence.py index 016305de..f5e07702 100644 --- a/test/test_evaluate/test_precedence.py +++ b/test/test_evaluate/test_precedence.py @@ -12,6 +12,6 @@ import pytest ]) def test_equals(source): script = Script(source) - node = script._get_module_node().children[0].children[0] + node = script._get_module_node().children[0] first, = script._get_module().eval_node(node) assert isinstance(first, CompiledObject) and first.obj is True diff --git a/test/test_evaluate/test_sys_path.py b/test/test_evaluate/test_sys_path.py index 96f4779a..dc61f01e 100644 --- a/test/test_evaluate/test_sys_path.py +++ b/test/test_evaluate/test_sys_path.py @@ -11,7 +11,7 @@ from jedi import Script def test_paths_from_assignment(): def paths(src): script = Script(src) - expr_stmt = script._get_module_node().children[0].children[0] + expr_stmt = script._get_module_node().children[0] return set(sys_path._paths_from_assignment(script._get_module(), expr_stmt)) assert paths('sys.path[0:0] = ["a"]') == set(['a']) diff --git a/test/test_parso_integration/test_parser_utils.py b/test/test_parso_integration/test_parser_utils.py index 6683c1eb..11c62d8e 100644 --- a/test/test_parso_integration/test_parser_utils.py +++ b/test/test_parso_integration/test_parser_utils.py @@ -11,8 +11,10 @@ import pytest class TestCallAndName(): def get_call(self, source): # Get the simple_stmt and then the first one. - simple_stmt = parse(source).children[0] - return simple_stmt.children[0] + node = parse(source).children[0] + if node.type == 'simple_stmt': + return node.children[0] + return node def test_name_and_call_positions(self): name = self.get_call('name\nsomething_else')