mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 22:44:27 +08:00
Get rid of follow_definition and replace it with infer
This commit is contained in:
@@ -14,10 +14,10 @@ from jedi.cache import memoize_method
|
||||
from jedi.evaluate import imports
|
||||
from jedi.evaluate import compiled
|
||||
from jedi.evaluate.imports import ImportName
|
||||
from jedi.evaluate.names import ParamName
|
||||
from jedi.evaluate.context import FunctionExecutionContext, MethodContext
|
||||
from jedi.evaluate.context import FunctionExecutionContext
|
||||
from jedi.evaluate.gradual.typeshed import StubModuleContext
|
||||
from jedi.evaluate.gradual.conversion import name_to_stub, stub_to_actual_context_set
|
||||
from jedi.evaluate.gradual.conversion import name_to_stub, \
|
||||
stub_to_actual_context_set, try_stubs_to_actual_context_set
|
||||
from jedi.api.keywords import KeywordName
|
||||
|
||||
|
||||
@@ -342,26 +342,12 @@ class BaseDefinition(object):
|
||||
if not self._name.is_context_name:
|
||||
return []
|
||||
|
||||
tree_name = self._name.tree_name
|
||||
parent_context = self._name.parent_context
|
||||
# Param names are special because they are not handled by
|
||||
# the evaluator method.
|
||||
if tree_name is None or parent_context is None or isinstance(self._name, ParamName):
|
||||
context_set = self._name.infer()
|
||||
else:
|
||||
|
||||
# TODO remove this paragraph, it's ugly and shouldn't be needed
|
||||
inferred = self._name.infer()
|
||||
if inferred:
|
||||
inferred = next(iter(inferred))
|
||||
if isinstance(inferred, MethodContext):
|
||||
c = inferred.class_context
|
||||
else:
|
||||
c = self._name.parent_context
|
||||
else:
|
||||
c = self._name.parent_context
|
||||
|
||||
context_set = self._evaluator.goto_definitions(c, tree_name)
|
||||
context_set = try_stubs_to_actual_context_set(
|
||||
self._name.infer(),
|
||||
prefer_stub_to_compiled=True,
|
||||
)
|
||||
return [Definition(self._evaluator, d.name) for d in context_set]
|
||||
|
||||
@property
|
||||
@@ -520,11 +506,7 @@ class Completion(BaseDefinition):
|
||||
DeprecationWarning,
|
||||
stacklevel=2
|
||||
)
|
||||
if not self._name.is_context_name:
|
||||
return []
|
||||
|
||||
defs = self._name.infer()
|
||||
return [Definition(self._evaluator, d.name) for d in defs]
|
||||
return self.infer()
|
||||
|
||||
|
||||
class Definition(BaseDefinition):
|
||||
|
||||
@@ -7,14 +7,14 @@ from ..helpers import cwd_at
|
||||
def test_import_empty(Script):
|
||||
""" github #340, return the full word. """
|
||||
completion = Script("import ").completions()[0]
|
||||
definition = completion.follow_definition()[0]
|
||||
definition = completion.infer()[0]
|
||||
assert definition
|
||||
|
||||
|
||||
def check_follow_definition_types(Script, source):
|
||||
# nested import
|
||||
completions = Script(source, path='some_path.py').completions()
|
||||
defs = chain.from_iterable(c.follow_definition() for c in completions)
|
||||
defs = chain.from_iterable(c.infer() for c in completions)
|
||||
return [d.type for d in defs]
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ def test_follow_import_incomplete(Script, environment):
|
||||
itert = jedi.Script("from itertools import ").completions()
|
||||
definitions = [d for d in itert if d.name == 'chain']
|
||||
assert len(definitions) == 1
|
||||
assert [d.type for d in definitions[0].follow_definition()] == ['class']
|
||||
assert [d.type for d in definitions[0].infer()] == ['class']
|
||||
|
||||
# incomplete `from * import` part
|
||||
datetime = check_follow_definition_types(Script, "from datetime import datetim")
|
||||
@@ -40,7 +40,7 @@ def test_follow_import_incomplete(Script, environment):
|
||||
assert set(datetime) == {'class', 'instance'} # py3: builtin and pure py version
|
||||
# os.path check
|
||||
ospath = check_follow_definition_types(Script, "from os.path import abspat")
|
||||
assert ospath == ['function']
|
||||
assert set(ospath) == {'function'}
|
||||
|
||||
# alias
|
||||
alias = check_follow_definition_types(Script, "import io as abcd; abcd")
|
||||
|
||||
Reference in New Issue
Block a user