Prefer stubs to Python names when starting to infer

This commit is contained in:
Dave Halter
2019-06-13 09:26:50 +02:00
parent a0adff9d36
commit 5a26d4cf8f
2 changed files with 15 additions and 7 deletions

View File

@@ -309,14 +309,18 @@ class BaseDefinition(object):
if not self._name.is_context_name:
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(
self._name.infer(),
ContextSet.from_sets(n.infer() for n in names),
only_stubs=only_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)
for n in names]
for n in resulting_names]
@property
@memoize_method

View File

@@ -25,7 +25,7 @@ from test.helpers import root_dir
'collections.Counter.most_common', 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],
@@ -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):
project = Project(os.path.join(root_dir, 'test', 'completion', 'stub_folder'))
s = Script(code, _project=project)
prefer_stubs = kwargs['prefer_stubs']
only_stubs = kwargs['only_stubs']
if way == 'direct':
defs = s.goto_definitions(**kwargs)
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)]
only_stubs = kwargs['only_stubs']
prefer_stubs = kwargs['prefer_stubs']
if not has_stub and only_stubs:
assert not defs
else: