diff --git a/jedi/api/completion.py b/jedi/api/completion.py index 3078d83f4..674833404 100644 --- a/jedi/api/completion.py +++ b/jedi/api/completion.py @@ -683,14 +683,14 @@ def search_in_module(inference_state, module_context, names, wanted_names, def extract_imported_names(node): imported_names = [] - if node.type in ['import_as_names', 'dotted_as_names', 'import_as_name']: + if node.type in ['import_as_names', 'dotted_as_names', 'dotted_as_name', 'import_as_name']: for index, child in enumerate(node.children): if child.type == 'name': - if (index > 0 and node.children[index - 1].type == "keyword" + if (index > 1 and node.children[index - 1].type == "keyword" and node.children[index - 1].value == "as"): continue imported_names.append(child.value) - elif child.type == 'import_as_name': + elif child.type in ('import_as_name', 'dotted_as_name'): imported_names.extend(extract_imported_names(child)) return imported_names diff --git a/test/test_inference/test_imports.py b/test/test_inference/test_imports.py index 03de11827..dfec5cd45 100644 --- a/test/test_inference/test_imports.py +++ b/test/test_inference/test_imports.py @@ -318,9 +318,14 @@ def import_names(*args, **kwargs): s = 'from os import path, p' assert 'path' not in import_names(s) assert 'path' in import_names(s, column=len(s) - 3) + assert 'path' in import_names("from import path") + assert 'path' in import_names("from import chdir, path") - s = 'import path as pp, p' - assert 'path' not in import_names(s) + s = 'import math as mm, m' + assert 'math' not in import_names(s) + + s = 'import math as os, o' + assert 'os' in import_names(s) s = 'from os import path as pp, p' assert 'path' not in import_names(s)