1
0
forked from VimPlug/jedi

ImportName should resolve properly to the module that it was designed to resolve for

This commit is contained in:
Dave Halter
2019-05-07 09:43:55 +02:00
parent 895eae1d54
commit dc15470e0b
3 changed files with 34 additions and 23 deletions
+2 -2
View File
@@ -286,7 +286,7 @@ class Script(object):
if new_name.start_pos is None: if new_name.start_pos is None:
found_builtin = True found_builtin = True
if found_builtin and not isinstance(name, imports.SubModuleName): if found_builtin:
yield name yield name
else: else:
for new_name in new_names: for new_name in new_names:
@@ -305,7 +305,7 @@ class Script(object):
return name.is_import() return name.is_import()
else: else:
def check(name): def check(name):
return isinstance(name, imports.SubModuleName) return False
names = filter_follow_imports(names, check) names = filter_follow_imports(names, check)
names = try_stub_to_actual_names(names, prefer_stub_to_compiled=True) names = try_stub_to_actual_names(names, prefer_stub_to_compiled=True)
+17 -10
View File
@@ -64,15 +64,22 @@ class BaseDefinition(object):
""" """
self.is_keyword = isinstance(self._name, KeywordName) self.is_keyword = isinstance(self._name, KeywordName)
# generate a path to the definition @memoize_method
self._module = name.get_root_context() def _get_module(self):
# This can take a while to complete, because in the worst case of
# imports (consider `import a` completions), we need to load all
# modules starting with a first.
return self._name.get_root_context()
@property
def module_path(self):
"""Shows the file path of a module. e.g. ``/usr/lib/python2.7/os.py``"""
try: try:
py__file__ = self._module.py__file__ py__file__ = self._get_module().py__file__
except AttributeError: except AttributeError:
self.module_path = None return None
else: else:
self.module_path = py__file__() return py__file__()
"""Shows the file path of a module. e.g. ``/usr/lib/python2.7/os.py``"""
@property @property
def name(self): def name(self):
@@ -203,14 +210,14 @@ class BaseDefinition(object):
>>> print(d.module_name) # doctest: +ELLIPSIS >>> print(d.module_name) # doctest: +ELLIPSIS
json json
""" """
return self._module.name.string_name return self._get_module().name.string_name
def in_builtin_module(self): def in_builtin_module(self):
"""Whether this is a builtin module.""" """Whether this is a builtin module."""
if isinstance(self._module, StubModuleContext): if isinstance(self._get_module(), StubModuleContext):
return any(isinstance(context, compiled.CompiledObject) return any(isinstance(context, compiled.CompiledObject)
for context in self._module.non_stub_context_set) for context in self._get_module().non_stub_context_set)
return isinstance(self._module, compiled.CompiledObject) return isinstance(self._get_module(), compiled.CompiledObject)
@property @property
def line(self): def line(self):
+15 -11
View File
@@ -4,6 +4,7 @@ from parso.tree import search_ancestor
from jedi._compatibility import Parameter from jedi._compatibility import Parameter
from jedi.evaluate.base_context import ContextSet from jedi.evaluate.base_context import ContextSet
from jedi.cache import memoize_method
class AbstractNameDefinition(object): class AbstractNameDefinition(object):
@@ -139,25 +140,28 @@ class ImportName(AbstractNameDefinition):
_level = 0 _level = 0
def __init__(self, parent_context, string_name): def __init__(self, parent_context, string_name):
self.parent_context = parent_context self._from_module_context = parent_context
self.string_name = string_name self.string_name = string_name
@property
def parent_context(self):
m = self._from_module_context
import_contexts = self.infer()
if not import_contexts:
return m
# It's almost always possible to find the import or to not find it. The
# importing returns only one context, pretty much always.
return next(iter(import_contexts))
@memoize_method
def infer(self): def infer(self):
from jedi.evaluate.imports import Importer from jedi.evaluate.imports import Importer
return Importer( m = self._from_module_context
self.parent_context.evaluator, return Importer(m.evaluator, [self.string_name], m, level=self._level).follow()
[self.string_name],
self.parent_context,
level=self._level,
).follow()
def goto(self): def goto(self):
return [m.name for m in self.infer()] return [m.name for m in self.infer()]
def get_root_context(self):
# Not sure if this is correct.
return self.parent_context.get_root_context()
@property @property
def api_type(self): def api_type(self):
return 'module' return 'module'