mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 14:04:26 +08:00
Prefer stubs to Python names when starting to infer
This commit is contained in:
@@ -309,14 +309,18 @@ class BaseDefinition(object):
|
|||||||
if not self._name.is_context_name:
|
if not self._name.is_context_name:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
# First we need to make sure that we have stub names (if possible) that
|
||||||
|
# we can follow. If we don't do that, we can end up with the inferred
|
||||||
|
# results of Python objects instead of stubs.
|
||||||
|
names = convert_names([self._name], prefer_stubs=True)
|
||||||
contexts = convert_contexts(
|
contexts = convert_contexts(
|
||||||
self._name.infer(),
|
ContextSet.from_sets(n.infer() for n in names),
|
||||||
only_stubs=only_stubs,
|
only_stubs=only_stubs,
|
||||||
prefer_stubs=prefer_stubs,
|
prefer_stubs=prefer_stubs,
|
||||||
)
|
)
|
||||||
names = [c.name for c in contexts]
|
resulting_names = [c.name for c in contexts]
|
||||||
return [self if n == self._name else Definition(self._evaluator, n)
|
return [self if n == self._name else Definition(self._evaluator, n)
|
||||||
for n in names]
|
for n in resulting_names]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@memoize_method
|
@memoize_method
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ from test.helpers import root_dir
|
|||||||
'collections.Counter.most_common', True, True],
|
'collections.Counter.most_common', True, True],
|
||||||
|
|
||||||
['from keyword import kwlist; kwlist', 'typing.Sequence', True, True],
|
['from keyword import kwlist; kwlist', 'typing.Sequence', True, True],
|
||||||
#['from keyword import kwlist', 'typing.Sequence', True, True],
|
['from keyword import kwlist', 'typing.Sequence', True, True],
|
||||||
|
|
||||||
['import with_stub', 'with_stub', True, True],
|
['import with_stub', 'with_stub', True, True],
|
||||||
['import with_stub', 'with_stub', True, True],
|
['import with_stub', 'with_stub', True, True],
|
||||||
@@ -35,14 +35,18 @@ from test.helpers import root_dir
|
|||||||
def test_infer_and_goto(Script, code, full_name, has_stub, has_python, way, kwargs):
|
def test_infer_and_goto(Script, code, full_name, has_stub, has_python, way, kwargs):
|
||||||
project = Project(os.path.join(root_dir, 'test', 'completion', 'stub_folder'))
|
project = Project(os.path.join(root_dir, 'test', 'completion', 'stub_folder'))
|
||||||
s = Script(code, _project=project)
|
s = Script(code, _project=project)
|
||||||
|
prefer_stubs = kwargs['prefer_stubs']
|
||||||
|
only_stubs = kwargs['only_stubs']
|
||||||
if way == 'direct':
|
if way == 'direct':
|
||||||
defs = s.goto_definitions(**kwargs)
|
defs = s.goto_definitions(**kwargs)
|
||||||
else:
|
else:
|
||||||
goto_defs = s.goto_assignments()
|
goto_defs = s.goto_assignments(
|
||||||
|
# Prefering stubs when we want to go to python and vice versa
|
||||||
|
prefer_stubs=not (prefer_stubs or only_stubs),
|
||||||
|
follow_imports=True,
|
||||||
|
)
|
||||||
defs = [d for goto_def in goto_defs for d in goto_def.infer(**kwargs)]
|
defs = [d for goto_def in goto_defs for d in goto_def.infer(**kwargs)]
|
||||||
|
|
||||||
only_stubs = kwargs['only_stubs']
|
|
||||||
prefer_stubs = kwargs['prefer_stubs']
|
|
||||||
if not has_stub and only_stubs:
|
if not has_stub and only_stubs:
|
||||||
assert not defs
|
assert not defs
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user