mirror of
https://github.com/davidhalter/jedi.git
synced 2026-03-02 06:56:52 +08:00
Importer now handles follow rest as well.
This commit is contained in:
@@ -100,9 +100,7 @@ class ImportWrapper(pr.Base):
|
|||||||
self._evaluator.find_types(s, rest[0], is_goto=True)
|
self._evaluator.find_types(s, rest[0], is_goto=True)
|
||||||
for s in scopes))
|
for s in scopes))
|
||||||
else:
|
else:
|
||||||
scopes = list(chain.from_iterable(
|
scopes = importer.follow_rest(scopes[0], rest)
|
||||||
self._evaluator.find_types(s, rest[0])
|
|
||||||
for s in scopes))
|
|
||||||
debug.dbg('after import: %s', scopes)
|
debug.dbg('after import: %s', scopes)
|
||||||
if not scopes:
|
if not scopes:
|
||||||
analysis.add(self._evaluator, 'import-error', importer.import_path[-1])
|
analysis.add(self._evaluator, 'import-error', importer.import_path[-1])
|
||||||
@@ -440,12 +438,26 @@ class _Importer(object):
|
|||||||
return []
|
return []
|
||||||
if rest:
|
if rest:
|
||||||
# follow the rest of the import (not FS -> classes, functions)
|
# follow the rest of the import (not FS -> classes, functions)
|
||||||
return []
|
return self.follow_rest(scope, rest)
|
||||||
raise NotImplementedError
|
|
||||||
# old
|
|
||||||
return evaluator.follow_path(iter(rest), [scope], scope)
|
|
||||||
return [scope]
|
return [scope]
|
||||||
|
|
||||||
|
def follow_rest(self, module, rest):
|
||||||
|
# Either os.path or path length is smaller.
|
||||||
|
if len(rest) < 2 or len(self.str_import_path) < 4 \
|
||||||
|
and ('os', 'path') == self.str_import_path[:2] and self.level == 0:
|
||||||
|
# This is a huge exception, we follow a nested import
|
||||||
|
# ``os.path``, because it's a very important one in Python
|
||||||
|
# that is being achieved by messing with ``sys.modules`` in
|
||||||
|
# ``os``.
|
||||||
|
scopes = [module]
|
||||||
|
for r in rest:
|
||||||
|
scopes = list(chain.from_iterable(
|
||||||
|
self._evaluator.find_types(s, r)
|
||||||
|
for s in scopes))
|
||||||
|
return scopes
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
|
||||||
@memoize_default(NO_DEFAULT)
|
@memoize_default(NO_DEFAULT)
|
||||||
def follow_file_system(self):
|
def follow_file_system(self):
|
||||||
# Handle "magic" Flask extension imports:
|
# Handle "magic" Flask extension imports:
|
||||||
@@ -459,22 +471,7 @@ class _Importer(object):
|
|||||||
self.import_path = ('flaskext',) + orig_path[2:]
|
self.import_path = ('flaskext',) + orig_path[2:]
|
||||||
return self._real_follow_file_system()
|
return self._real_follow_file_system()
|
||||||
|
|
||||||
obj, rest = self._real_follow_file_system()
|
return self._real_follow_file_system()
|
||||||
|
|
||||||
# os.path handling
|
|
||||||
if len(self.str_import_path) < 4 \
|
|
||||||
and ('os', 'path') == self.str_import_path[:2] and self.level == 0:
|
|
||||||
# This is a huge exception, we follow a nested import
|
|
||||||
# ``os.path``, because it's a very important one in Python
|
|
||||||
# that is being achieved by messing with ``sys.modules`` in
|
|
||||||
# ``os``.
|
|
||||||
scopes = [obj]
|
|
||||||
for r in rest:
|
|
||||||
scopes = list(chain.from_iterable(
|
|
||||||
self._evaluator.find_types(s, r)
|
|
||||||
for s in scopes))
|
|
||||||
return scopes[0], []
|
|
||||||
return obj, rest
|
|
||||||
|
|
||||||
def _real_follow_file_system(self):
|
def _real_follow_file_system(self):
|
||||||
if self.file_path:
|
if self.file_path:
|
||||||
@@ -645,8 +642,13 @@ class _Importer(object):
|
|||||||
if os.path.isdir(flaskext):
|
if os.path.isdir(flaskext):
|
||||||
names += self._get_module_names([flaskext])
|
names += self._get_module_names([flaskext])
|
||||||
|
|
||||||
from jedi.evaluate import finder
|
from jedi.evaluate import finder, representation as er
|
||||||
for scope in self.follow(evaluator):
|
for scope in self.follow(evaluator):
|
||||||
|
# Non-modules are not completable.
|
||||||
|
if not isinstance(scope, er.ModuleWrapper) and not (isinstance(scope,
|
||||||
|
compiled.CompiledObject) and scope.type() == 'module'):
|
||||||
|
continue
|
||||||
|
|
||||||
# namespace packages
|
# namespace packages
|
||||||
if isinstance(scope, pr.Module) and scope.path.endswith('__init__.py'):
|
if isinstance(scope, pr.Module) and scope.path.endswith('__init__.py'):
|
||||||
pkg_path = os.path.dirname(scope.path)
|
pkg_path = os.path.dirname(scope.path)
|
||||||
|
|||||||
Reference in New Issue
Block a user