diff --git a/jedi/api/__init__.py b/jedi/api/__init__.py index 74300247..32cfd71d 100644 --- a/jedi/api/__init__.py +++ b/jedi/api/__init__.py @@ -429,6 +429,20 @@ class Script(object): goto_path = self._user_context.get_path_under_cursor() context = self._user_context.get_context() user_stmt = self._parser.user_stmt() + + stmt = self._get_under_cursor_stmt(goto_path) + expression_list = stmt.expression_list() + if len(expression_list) == 0: + return [] + # The reverse tokenizer only generates parses call. + assert len(expression_list) == 1 + call = expression_list[0] + if isinstance(call, pr.Call): + call_path = list(call.generate_call_path()) + else: + # goto_assignments on Operator returns nothing. + return [] + if next(context) in ('class', 'def'): # The cursor is on a class/function name. user_scope = self._parser.user_scope() @@ -448,7 +462,6 @@ class Script(object): and unicode(name_part) == unicode(import_name[0].names[-1]): definitions.append(import_name[0]) else: - stmt = self._get_under_cursor_stmt(goto_path) def test_lhs(): """ @@ -464,20 +477,7 @@ class Script(object): lhs = test_lhs() if lhs is None: - expression_list = stmt.expression_list() - if len(expression_list) == 0: - return [] - # The reverse tokenizer only generates parses call. - assert len(expression_list) == 1 - call = expression_list[0] - if isinstance(call, pr.Call): - call_path = list(call.generate_call_path()) - else: - # goto_assignments on Operator returns nothing. - return [] - - defs = self._evaluator.goto(user_stmt or stmt, - call_path) + defs = self._evaluator.goto(user_stmt or stmt, call_path) definitions = follow_inexistent_imports(defs) else: definitions = [lhs] diff --git a/test/completion/usages.py b/test/completion/usages.py index 408c0fe3..447fa87c 100644 --- a/test/completion/usages.py +++ b/test/completion/usages.py @@ -96,8 +96,7 @@ from import_tree.rename1 import abc #< (0, 32), from import_tree.rename1 import not_existing -# shouldn't work -#< +# Shouldn't work (would raise a NotFoundError, because there's no name.) from not_existing import * # ----------------- diff --git a/test/test_integration_keyword.py b/test/test_integration_keyword.py index 368f9348..25b7a01a 100644 --- a/test/test_integration_keyword.py +++ b/test/test_integration_keyword.py @@ -3,6 +3,7 @@ Test of keywords and ``jedi.keywords`` """ import jedi from jedi import Script, common +import pytest def test_goto_assignments_keyword(): @@ -19,8 +20,9 @@ def test_keyword(): defs = Script("print").goto_definitions() assert [d.doc for d in defs] - defs = Script("import").goto_assignments() - assert len(defs) == 0 + with pytest.raises(jedi.NotFoundError): + Script("import").goto_assignments() + completions = Script("import", 1, 1).completions() assert len(completions) == 0 with common.ignored(jedi.NotFoundError): # TODO shouldn't throw that.