Skip to content

Commit 789c2a6

Browse files
authored
Fix autoimport raising AttributeError in some cases (#309)
1 parent cd9941c commit 789c2a6

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

pylsp/plugins/rope_autoimport.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ def pylsp_settings() -> Dict[str, Dict[str, Dict[str, Any]]]:
2828
return {"plugins": {"rope_autoimport": {"enabled": False, "memory": False}}}
2929

3030

31-
def _should_insert(expr: tree.BaseNode, word_node: tree.Leaf) -> bool:
31+
def _should_insert(expr: tree.BaseNode, word_node: tree.Leaf) -> bool: # pylint: disable=too-many-return-statements
3232
"""
3333
Check if we should insert the word_node on the given expr.
3434
3535
Works for both correct and incorrect code. This is because the
3636
user is often working on the code as they write it.
3737
"""
38+
if not word_node:
39+
return False
3840
if len(expr.children) == 0:
3941
return True
4042
first_child = expr.children[0]

test/plugins/test_autoimport.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ def test_autoimport_class(completions):
101101
assert len(completions) == 0
102102

103103

104+
@pytest.mark.parametrize("completions", [("""\n""", 0)], indirect=True)
105+
def test_autoimport_empty_line(completions):
106+
assert len(completions) == 0
107+
108+
104109
@pytest.mark.parametrize("completions", [("""class Test(NamedTupl):""", 20)],
105110
indirect=True)
106111
def test_autoimport_class_complete(completions):

0 commit comments

Comments
 (0)