From 5a26d4cf8f04b487071f8e66e5d686e1c36c7071 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Thu, 13 Jun 2019 09:26:50 +0200 Subject: [PATCH] Prefer stubs to Python names when starting to infer --- jedi/api/classes.py | 10 +++++++--- test/test_evaluate/test_gradual/test_stubs.py | 12 ++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/jedi/api/classes.py b/jedi/api/classes.py index 3d7c20f3..81ef1f29 100644 --- a/jedi/api/classes.py +++ b/jedi/api/classes.py @@ -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 diff --git a/test/test_evaluate/test_gradual/test_stubs.py b/test/test_evaluate/test_gradual/test_stubs.py index c84420aa..1922cfd9 100644 --- a/test/test_evaluate/test_gradual/test_stubs.py +++ b/test/test_evaluate/test_gradual/test_stubs.py @@ -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: