diff --git a/pylsp/plugins/rope_autoimport.py b/pylsp/plugins/rope_autoimport.py index f6366989..2c0c46ed 100644 --- a/pylsp/plugins/rope_autoimport.py +++ b/pylsp/plugins/rope_autoimport.py @@ -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] diff --git a/test/plugins/test_autoimport.py b/test/plugins/test_autoimport.py index b017b92f..a1ddd779 100644 --- a/test/plugins/test_autoimport.py +++ b/test/plugins/test_autoimport.py @@ -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 + + @pytest.mark.parametrize("completions", [("""class Test(NamedTupl):""", 20)], indirect=True) def test_autoimport_class_complete(completions):