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

Fix autoimport raising AttributeError in some cases #309

Merged
merged 3 commits into from
Nov 30, 2022
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
4 changes: 3 additions & 1 deletion pylsp/plugins/rope_autoimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ def pylsp_settings() -> Dict[str, Dict[str, Dict[str, Any]]]:
return {"plugins": {"rope_autoimport": {"enabled": False, "memory": False}}}


def _should_insert(expr: tree.BaseNode, word_node: tree.Leaf) -> bool:
def _should_insert(expr: tree.BaseNode, word_node: tree.Leaf) -> bool: # pylint: disable=too-many-return-statements
"""
Check if we should insert the word_node on the given expr.

Works for both correct and incorrect code. This is because the
user is often working on the code as they write it.
"""
if not word_node:
return False
if len(expr.children) == 0:
return True
first_child = expr.children[0]
Expand Down
5 changes: 5 additions & 0 deletions test/plugins/test_autoimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ def test_autoimport_class(completions):
assert len(completions) == 0


@pytest.mark.parametrize("completions", [("""\n""", 0)], indirect=True)
def test_autoimport_empty_line(completions):
assert len(completions) == 0
douo marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.parametrize("completions", [("""class Test(NamedTupl):""", 20)],
indirect=True)
def test_autoimport_class_complete(completions):
Expand Down