diff --git a/jedi/api/__init__.py b/jedi/api/__init__.py index 4ab5e7b0..a9a0f835 100644 --- a/jedi/api/__init__.py +++ b/jedi/api/__init__.py @@ -144,14 +144,15 @@ class Script(object): # TODO this paragraph is necessary, but not sure it works. context = self._user_context.get_context() next(context) # skip the path - if False and next(context) == 'from': + if next(context) == 'from': # completion is just "import" if before stands from .. completions += ((k, bs) for k in keywords.keyword_names('import')) module = self._parser.module() name = user_stmt.name_for_position(self._pos) - imp = imports.ImportWrapper(self._evaluator, name) - completions += [(n, module) for n in imp.completions()] + if name is not None: + imp = imports.ImportWrapper(self._evaluator, name) + completions += [(n, module) for n in imp.completion_names()] if importer or isinstance(user_stmt, pr.Import): return completions diff --git a/jedi/api/helpers.py b/jedi/api/helpers.py index 47be20b0..c69e0af3 100644 --- a/jedi/api/helpers.py +++ b/jedi/api/helpers.py @@ -57,7 +57,7 @@ def importer_from_error_statement(evaluator, module, error_statement, pos): elif typ == 'import_from': for node in nodes: if isinstance(node, pt.Node) and node.type == 'dotted_name': - names += check_dotted(node.children[::2]) + names += check_dotted(node.children) elif node in ('.', '...'): level += len(node.value) elif isinstance(node, pt.Name) and node.end_pos < pos: diff --git a/jedi/evaluate/imports.py b/jedi/evaluate/imports.py index 96645f0c..b8500f9c 100644 --- a/jedi/evaluate/imports.py +++ b/jedi/evaluate/imports.py @@ -47,7 +47,7 @@ class ImportWrapper(pr.Base): self.import_path = self._import.path_for_name(name) self.is_like_search = False # TODO REMOVE - def completions(self): + def completion_names(self): # The import path needs to be reduced by one, because we're completing. import_path = self.import_path[:-1] module = self._import.get_parent_until() @@ -628,28 +628,29 @@ class _Importer(object): """ names = [] if self.import_path: - for scope in self.follow(evaluator): - # flask - if self.str_import_path == ('flask', 'ext'): - # List Flask extensions like ``flask_foo`` - for mod in self._get_module_names(): - modname = str(mod) - if modname.startswith('flask_'): - extname = modname[len('flask_'):] - names.append(self._generate_name(extname)) - # Now the old style: ``flaskext.foo`` - for dir in self.sys_path_with_modifications(): - flaskext = os.path.join(dir, 'flaskext') - if os.path.isdir(flaskext): - names += self._get_module_names([flaskext]) + # flask + if self.str_import_path == ('flask', 'ext'): + # List Flask extensions like ``flask_foo`` + for mod in self._get_module_names(): + modname = str(mod) + if modname.startswith('flask_'): + extname = modname[len('flask_'):] + names.append(self._generate_name(extname)) + # Now the old style: ``flaskext.foo`` + for dir in self.sys_path_with_modifications(): + flaskext = os.path.join(dir, 'flaskext') + if os.path.isdir(flaskext): + names += self._get_module_names([flaskext]) + from jedi.evaluate import finder, representation as er + for scope in self.follow(evaluator): # namespace packages if isinstance(scope, pr.Module) and scope.path.endswith('__init__.py'): 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 only_modules: + if only_modules or not isinstance(scope, er.ModuleWrapper): # In the case of an import like `from x.` we don't need to # add all the variables. if ('os',) == self.str_import_path and not self.level: @@ -659,15 +660,6 @@ class _Importer(object): continue - # TODO delete - if False and not self.import_stmt.from_names or False and self.is_partial_import: - # from_names must be defined to access module - # values plus a partial import means that there - # is something after the import, which - # automatically implies that there must not be - # any non-module scope. - continue - from jedi.evaluate import finder for s, scope_names in finder.get_names_of_scope(self._evaluator, scope, include_builtin=False): for n in scope_names: diff --git a/test/completion/on_import.py b/test/completion/on_import.py index 76b45fb9..0007cba5 100644 --- a/test/completion/on_import.py +++ b/test/completion/on_import.py @@ -61,10 +61,10 @@ import datetime. #? [] import datetime.date -#? 17 ['import'] -from import_tree import pkg -#? 18 ['import', 'pkg'] -from import_tree. import pkg +#? 21 ['import'] +from import_tree.pkg import pkg +#? 22 ['import', 'mod1'] +from import_tree.pkg. import mod1 #? 17 ['mod1', 'mod2', 'random', 'pkg', 'rename1', 'rename2', 'recurse_class1', 'recurse_class2'] from import_tree. import pkg