diff --git a/jedi/api.py b/jedi/api.py index 6ed1f0dc..96ecd0ff 100644 --- a/jedi/api.py +++ b/jedi/api.py @@ -536,8 +536,13 @@ class Script(object): cur_name_part = name_part kill_count += 1 + + context = self._module.get_context() + just_from = next(context) == 'from' + i = imports.ImportPath(user_stmt, is_like_search, - kill_count=kill_count, direct_resolve=True) + kill_count=kill_count, direct_resolve=True, + is_just_from=just_from) return i, cur_name_part def _get_completion_parts(self): diff --git a/jedi/imports.py b/jedi/imports.py index afb972ad..e83a9c51 100644 --- a/jedi/imports.py +++ b/jedi/imports.py @@ -56,10 +56,12 @@ class ImportPath(pr.Base): GlobalNamespace = _GlobalNamespace() def __init__(self, import_stmt, is_like_search=False, kill_count=0, - direct_resolve=False): + direct_resolve=False, is_just_from=False): self.import_stmt = import_stmt self.is_like_search = is_like_search self.direct_resolve = direct_resolve + self.is_just_from = is_just_from + self.is_partial_import = bool(max(0, kill_count)) path = import_stmt.get_parent_until().path self.file_path = os.path.dirname(path) if path is not None else None @@ -131,6 +133,16 @@ class ImportPath(pr.Base): pkg_path = os.path.dirname(scope.path) paths = self._namespace_packages(pkg_path, self.import_path) names += self._get_module_names([pkg_path] + paths) + if self.is_just_from: + # In the case of an import like `from x.` we don't need to + # add all the variables. + if ['os'] == self.import_path and not self._is_relative_import(): + # os.path is a hardcoded exception, because it's a + # ``sys.modules`` modification. + p = (0, 0) + names.append(pr.Name(self.GlobalNamespace, [('path', p)], + p, p, self.import_stmt)) + continue for s, scope_names in evaluate.get_names_of_scope(scope, include_builtin=False): for n in scope_names: diff --git a/test/completion/imports.py b/test/completion/imports.py index 1e78a9d5..fc054b01 100644 --- a/test/completion/imports.py +++ b/test/completion/imports.py @@ -67,6 +67,12 @@ def scope_nested2(): #? [] import_tree.rename1 +def from_names(): + #? ['mod1'] + from import_tree.pkg. + #? ['path'] + from os. + def builtin_test(): #? ['math'] import math