Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter duplicate imports when completing #2027

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions jedi/api/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ def _must_be_kwarg(signatures, positional_count, used_kwargs):
return must_be_kwarg


def filter_names(inference_state, completion_names, stack, like_name, fuzzy, cached_name):
def filter_names(inference_state, completion_names, stack, like_name, fuzzy,
imported_names, cached_name):
comp_dct = set()
if settings.case_insensitive_completion:
like_name = like_name.lower()
for name in completion_names:
string = name.string_name
if string in imported_names and string != like_name:
continue
if settings.case_insensitive_completion:
string = string.lower()
if helpers.match(string, like_name, fuzzy=fuzzy):
Expand Down Expand Up @@ -174,9 +177,13 @@ def complete(self):

cached_name, completion_names = self._complete_python(leaf)

imported_names = []
if leaf.parent is not None and leaf.parent.type in ['import_as_names', 'dotted_as_names']:
imported_names.extend(extract_imported_names(leaf.parent))

completions = list(filter_names(self._inference_state, completion_names,
self.stack, self._like_name,
self._fuzzy, cached_name=cached_name))
self._fuzzy, imported_names, cached_name=cached_name))

return (
# Removing duplicates mostly to remove False/True/None duplicates.
Expand Down Expand Up @@ -448,6 +455,7 @@ def _complete_in_string(self, start_leaf, string):
- Having some doctest code that starts with `>>>`
- Having backticks that doesn't have whitespace inside it
"""

def iter_relevant_lines(lines):
include_next_line = False
for l in code_lines:
Expand Down Expand Up @@ -670,3 +678,19 @@ def search_in_module(inference_state, module_context, names, wanted_names,
def_ = classes.Name(inference_state, n2)
if not wanted_type or wanted_type == def_.type:
yield def_


def extract_imported_names(node):
imported_names = []

if node.type in ['import_as_names', 'dotted_as_names', 'import_as_name']:
for index, child in enumerate(node.children):
if child.type == 'name':
if (index > 0 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':
imported_names.extend(extract_imported_names(child))

return imported_names
22 changes: 22 additions & 0 deletions test/test_inference/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,28 @@ def import_names(*args, **kwargs):
assert 'path' in import_names(s, column=len(s) - 3)


def test_duplicated_import(Script):
def import_names(*args, **kwargs):
return [d.name for d in Script(*args).complete(**kwargs)]

s = 'import os, o'
assert 'os' not in import_names(s)
assert 'os' in import_names(s, column=len(s) - 3)

s = 'from os import path, p'
assert 'path' not in import_names(s)
assert 'path' in import_names(s, column=len(s) - 3)

s = 'import path as pp, p'
assert 'path' not in import_names(s)

s = 'from os import path as pp, p'
assert 'path' not in import_names(s)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test here for

s = 'from os import chdir as path, p'
assert 'path' in import_names(s)

I feel like this is still wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will be wrong. I mentioned it above:

import alias will also be filtered Generally, there is unlikely to be a situation where an alias is named exactly the same as a specific original name.

If you think it unacceptable,I can parse this syntax and remove it from the filter

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I didn't read that.

If you think it unacceptable,I can parse this syntax and remove it from the filter

I think it is acceptable, but it is the kind of thing I always fix before merging. So if you don't do it, I have to :)

I think it's also a very easy fix (probably one simple if).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also know that this piece of software is used by millions of people, so issues like this one will appear sooner or later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done~


s = 'from os import chdir as path, p'
assert 'path' in import_names(s)


def test_path_issues(Script):
"""
See pull request #684 for details.
Expand Down