From 64e2f89e26966f5c900a5d1a53e5ad141bc00979 Mon Sep 17 00:00:00 2001 From: Madison Bentley Date: Sat, 25 Jan 2025 04:41:25 -0800 Subject: [PATCH] Don't offer a code action for a diagnostic with no diagnostic "code" (#100) * 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 * only include valid codes in diag --------- Co-authored-by: Richard Kellnberger --- pylsp_mypy/plugin.py | 10 ++++++++-- test/test_plugin.py | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pylsp_mypy/plugin.py b/pylsp_mypy/plugin.py index 4ab301b..8f954af 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,6 +608,8 @@ def pylsp_code_actions( for diagnostic in context.get("diagnostics", []): if diagnostic["source"] != "mypy": continue + if "code" not in diagnostic: + continue code = diagnostic["code"] lineNumberEnd = diagnostic["range"]["end"]["line"] line = document.lines[lineNumberEnd] diff --git a/test/test_plugin.py b/test/test_plugin.py index e816715..99d0950 100644 --- a/test/test_plugin.py +++ b/test/test_plugin.py @@ -116,7 +116,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):