From 2274633937229ca0b16d80c46e41b6d2f57f107c Mon Sep 17 00:00:00 2001 From: Madison Bentley Date: Thu, 2 Jan 2025 23:08:56 -0800 Subject: [PATCH 1/2] Don't offer a code action for a diagnostic with no diagnostic "code" Per the lsp spec, a diagnostic code, if one exists, should be a number or a string. At least one error message (the help message for missing imports) doesn't include a code, so the parse_line function sets it to "None". When this happens, don't offer a code action for that diagnostic --- pylsp_mypy/plugin.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pylsp_mypy/plugin.py b/pylsp_mypy/plugin.py index 82e501f..6d143ec 100644 --- a/pylsp_mypy/plugin.py +++ b/pylsp_mypy/plugin.py @@ -604,6 +604,8 @@ def pylsp_code_actions( for diagnostic in context.get("diagnostics", []): if diagnostic["source"] != "mypy": continue + if diagnostic.get("code") is None: + continue code = diagnostic["code"] lineNumberEnd = diagnostic["range"]["end"]["line"] line = document.lines[lineNumberEnd] From b097f81082c096816028c81243070aa0e9fdc40b Mon Sep 17 00:00:00 2001 From: Richard Kellnberger Date: Sat, 25 Jan 2025 13:36:28 +0100 Subject: [PATCH 2/2] only include valid codes in diag --- pylsp_mypy/plugin.py | 10 +++++++--- test/test_plugin.py | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pylsp_mypy/plugin.py b/pylsp_mypy/plugin.py index 6d143ec..15d11e7 100644 --- a/pylsp_mypy/plugin.py +++ b/pylsp_mypy/plugin.py @@ -112,7 +112,7 @@ def parse_line(line: str, document: Optional[Document] = None) -> Optional[Dict[ log.warning(f"invalid error severity '{severity}'") errno = 1 if severity == "error" else 3 - return { + diag = { "source": "mypy", "range": { "start": {"line": lineno, "character": offset}, @@ -120,9 +120,13 @@ def parse_line(line: str, document: Optional[Document] = None) -> Optional[Dict[ }, "message": result["message"], "severity": errno, - "code": result["code"], } + if result["code"]: + diag["code"] = result["code"] + + return diag + def apply_overrides(args: List[str], overrides: List[Any]) -> List[str]: """Replace or combine default command-line options with overrides.""" @@ -604,7 +608,7 @@ def pylsp_code_actions( for diagnostic in context.get("diagnostics", []): if diagnostic["source"] != "mypy": continue - if diagnostic.get("code") is None: + if "code" not in diagnostic: continue code = diagnostic["code"] lineNumberEnd = diagnostic["range"]["end"]["line"] diff --git a/test/test_plugin.py b/test/test_plugin.py index fa0d388..56dd64a 100644 --- a/test/test_plugin.py +++ b/test/test_plugin.py @@ -98,7 +98,7 @@ def test_parse_note_line(workspace): assert diag["range"]["start"] == {"line": 123, "character": 0} assert diag["range"]["end"] == {"line": 128, "character": 77} assert diag["severity"] == 3 - assert diag["code"] is None + assert "code" not in diag def test_multiple_workspaces(tmpdir, last_diagnostics_monkeypatch):