Skip to content

Commit

Permalink
Fix a few issues around duplicated import paths, fixes #2033
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhalter committed Nov 24, 2024
1 parent 41e9e95 commit ecb922c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 3 additions & 3 deletions jedi/api/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 7 additions & 2 deletions test/test_inference/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ecb922c

Please sign in to comment.