From 78ea840d6c899c06f03b248a0c6f72bf01355b6b Mon Sep 17 00:00:00 2001 From: tiou Date: Tue, 29 Nov 2022 17:47:10 +0800 Subject: [PATCH 1/3] Fix #307 autoimport raise AttributeError in some case. --- pylsp/plugins/rope_autoimport.py | 2 ++ test/plugins/test_autoimport.py | 3 +++ 2 files changed, 5 insertions(+) diff --git a/pylsp/plugins/rope_autoimport.py b/pylsp/plugins/rope_autoimport.py index f6366989..97b81015 100644 --- a/pylsp/plugins/rope_autoimport.py +++ b/pylsp/plugins/rope_autoimport.py @@ -35,6 +35,8 @@ def _should_insert(expr: tree.BaseNode, word_node: tree.Leaf) -> bool: 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..b17e4e78 100644 --- a/test/plugins/test_autoimport.py +++ b/test/plugins/test_autoimport.py @@ -100,6 +100,9 @@ def test_autoimport_function(completions): 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) From 25aacd27d3b576ef7374fc2d125f9a5932898146 Mon Sep 17 00:00:00 2001 From: tiou Date: Wed, 30 Nov 2022 09:08:43 +0800 Subject: [PATCH 2/3] Disable too many return statements lint for _should_insert --- pylsp/plugins/rope_autoimport.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylsp/plugins/rope_autoimport.py b/pylsp/plugins/rope_autoimport.py index 97b81015..2c0c46ed 100644 --- a/pylsp/plugins/rope_autoimport.py +++ b/pylsp/plugins/rope_autoimport.py @@ -28,7 +28,7 @@ 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. From d4906e84d88bf6f9c5feb0af7ac498d1c26731b2 Mon Sep 17 00:00:00 2001 From: Tiou Lims Date: Wed, 30 Nov 2022 11:34:17 +0800 Subject: [PATCH 3/3] Update test/plugins/test_autoimport.py Co-authored-by: Carlos Cordoba --- test/plugins/test_autoimport.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/plugins/test_autoimport.py b/test/plugins/test_autoimport.py index b17e4e78..a1ddd779 100644 --- a/test/plugins/test_autoimport.py +++ b/test/plugins/test_autoimport.py @@ -100,10 +100,12 @@ def test_autoimport_function(completions): 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):