Script.goto_assignments now always needs a call_path. Otherwise it raises a NotFoundError.

This change makes Jedi's behavior more consistent.
This commit is contained in:
Dave Halter
2014-09-04 00:56:58 +02:00
parent fb10199f37
commit e872d9e073
3 changed files with 20 additions and 19 deletions

View File

@@ -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]

View File

@@ -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 *
# -----------------

View File

@@ -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.