From d2bef680188aabc750ce4b51858a35294e256e83 Mon Sep 17 00:00:00 2001 From: wutingjia Date: Tue, 15 Oct 2024 20:15:58 +0800 Subject: [PATCH 1/3] filter imported names during completion --- jedi/api/completion.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/jedi/api/completion.py b/jedi/api/completion.py index 342f7506..5cab00b5 100644 --- a/jedi/api/completion.py +++ b/jedi/api/completion.py @@ -65,12 +65,14 @@ 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: + continue if settings.case_insensitive_completion: string = string.lower() if helpers.match(string, like_name, fuzzy=fuzzy): @@ -169,9 +171,15 @@ class Completion: cached_name, completion_names = self._complete_python(leaf) + imported_names = [] + if leaf.parent is not None and leaf.parent.type == 'import_as_names': + for child in leaf.parent.children: + if child.type == 'name': + imported_names.append(child.value) + 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. From c962f4d72083ea60522b4c595d813a578282e627 Mon Sep 17 00:00:00 2001 From: wutingjia Date: Wed, 16 Oct 2024 11:19:37 +0800 Subject: [PATCH 2/3] filter imported names during completion --- jedi/api/completion.py | 21 ++++++++++++++++----- test/test_inference/test_imports.py | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/jedi/api/completion.py b/jedi/api/completion.py index 5cab00b5..4e99248d 100644 --- a/jedi/api/completion.py +++ b/jedi/api/completion.py @@ -71,7 +71,7 @@ def filter_names(inference_state, completion_names, stack, like_name, fuzzy, imp like_name = like_name.lower() for name in completion_names: string = name.string_name - if string in imported_names: + if string in imported_names and string != like_name: continue if settings.case_insensitive_completion: string = string.lower() @@ -172,10 +172,8 @@ class Completion: cached_name, completion_names = self._complete_python(leaf) imported_names = [] - if leaf.parent is not None and leaf.parent.type == 'import_as_names': - for child in leaf.parent.children: - if child.type == 'name': - imported_names.append(child.value) + 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, @@ -672,3 +670,16 @@ 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 child in node.children: + if child.type == 'name': + imported_names.append(child.value) + elif child.type == 'import_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 53f5478b..ca8e513e 100644 --- a/test/test_inference/test_imports.py +++ b/test/test_inference/test_imports.py @@ -307,6 +307,24 @@ def test_os_issues(Script): 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) + def test_path_issues(Script): """ See pull request #684 for details. From e5749f83c9d9c6a7a1de3fefe0716b7581a5b0aa Mon Sep 17 00:00:00 2001 From: wutingjia Date: Wed, 16 Oct 2024 11:38:04 +0800 Subject: [PATCH 3/3] typo --- jedi/api/completion.py | 3 ++- test/test_inference/test_imports.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/jedi/api/completion.py b/jedi/api/completion.py index 4e99248d..c3e6cc95 100644 --- a/jedi/api/completion.py +++ b/jedi/api/completion.py @@ -65,7 +65,8 @@ def _must_be_kwarg(signatures, positional_count, used_kwargs): return must_be_kwarg -def filter_names(inference_state, completion_names, stack, like_name, fuzzy, imported_names, 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() diff --git a/test/test_inference/test_imports.py b/test/test_inference/test_imports.py index ca8e513e..4f47ca0e 100644 --- a/test/test_inference/test_imports.py +++ b/test/test_inference/test_imports.py @@ -325,6 +325,7 @@ def test_duplicated_import(Script): s = 'from os import path as pp, p' assert 'path' not in import_names(s) + def test_path_issues(Script): """ See pull request #684 for details.