Skip to content

Commit

Permalink
Don't offer a code action for a diagnostic with no diagnostic "code" (#…
Browse files Browse the repository at this point in the history
…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 <git@richardk2n.de>
  • Loading branch information
mtbentley and Richardk2n authored Jan 25, 2025
1 parent 6136587 commit 64e2f89
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions pylsp_mypy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,21 @@ 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},
"end": {"line": end_lineno, "character": end_offset},
},
"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."""
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion test/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 64e2f89

Please sign in to comment.