Skip to content

Commit

Permalink
filter imported names during completion(consider as keyword)
Browse files Browse the repository at this point in the history
  • Loading branch information
wutingjia committed Oct 17, 2024
1 parent 8f3292a commit 642e224
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
18 changes: 11 additions & 7 deletions jedi/api/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _get_signature_param_names(signatures, positional_count, used_kwargs):
if i < positional_count and kind == Parameter.POSITIONAL_OR_KEYWORD:
continue
if kind in (Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY) \
and p.name not in used_kwargs:
and p.name not in used_kwargs:
yield ParamNameWithEquals(p._name)


Expand Down Expand Up @@ -308,7 +308,7 @@ def _complete_python(self, leaf):
# 3. Decorators are very primitive and have an optional `(` with
# optional arglist in them.
if nodes[-1] in ['(', ','] \
and nonterminals[-1] in ('trailer', 'arglist', 'decorator'):
and nonterminals[-1] in ('trailer', 'arglist', 'decorator'):
signatures = self._signatures_callback(*self._position)
if signatures:
call_details = signatures[0]._call_details
Expand Down Expand Up @@ -455,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 @@ -536,7 +537,7 @@ def return_part_of_leaf(leaf):
if not leaf.prefix:
prefix_leaf = leaf.get_previous_leaf()
if prefix_leaf is None or prefix_leaf.type != 'name' \
or not all(c in 'rubf' for c in prefix_leaf.value.lower()):
or not all(c in 'rubf' for c in prefix_leaf.value.lower()):
prefix_leaf = None

return (
Expand Down Expand Up @@ -615,7 +616,7 @@ def __getattr__(self, name):
atom = atom_expr.children[0]
trailer = atom_expr.children[1]
if len(atom_expr.children) != 2 or atom.type != 'name' \
or atom.value != 'getattr':
or atom.value != 'getattr':
continue
arglist = trailer.children[1]
if arglist.type != 'arglist' or len(arglist.children) < 3:
Expand Down Expand Up @@ -645,7 +646,7 @@ def search_in_module(inference_state, module_context, names, wanted_names,
for n in names:
if s == n.string_name:
if n.tree_name is not None and n.api_type in ('module', 'namespace') \
and ignore_imports:
and ignore_imports:
continue
new_names += complete_trailer(
module_context,
Expand All @@ -658,7 +659,7 @@ def search_in_module(inference_state, module_context, names, wanted_names,
for n in names:
string = n.string_name.lower()
if complete and helpers.match(string, last_name, fuzzy=fuzzy) \
or not complete and string == last_name:
or not complete and string == last_name:
if isinstance(n, SubModuleName):
names = [v.name for v in n.infer()]
else:
Expand All @@ -683,8 +684,11 @@ 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:
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))
Expand Down
3 changes: 3 additions & 0 deletions test/test_inference/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ def import_names(*args, **kwargs):
s = 'from os import path as pp, p'
assert 'path' not in import_names(s)

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


def test_path_issues(Script):
"""
Expand Down

0 comments on commit 642e224

Please sign in to comment.