Skip to content

Commit 64e2f89

Browse files
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 <[email protected]>
1 parent 6136587 commit 64e2f89

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

pylsp_mypy/plugin.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,21 @@ def parse_line(line: str, document: Optional[Document] = None) -> Optional[Dict[
112112
log.warning(f"invalid error severity '{severity}'")
113113
errno = 1 if severity == "error" else 3
114114

115-
return {
115+
diag = {
116116
"source": "mypy",
117117
"range": {
118118
"start": {"line": lineno, "character": offset},
119119
"end": {"line": end_lineno, "character": end_offset},
120120
},
121121
"message": result["message"],
122122
"severity": errno,
123-
"code": result["code"],
124123
}
125124

125+
if result["code"]:
126+
diag["code"] = result["code"]
127+
128+
return diag
129+
126130

127131
def apply_overrides(args: List[str], overrides: List[Any]) -> List[str]:
128132
"""Replace or combine default command-line options with overrides."""
@@ -604,6 +608,8 @@ def pylsp_code_actions(
604608
for diagnostic in context.get("diagnostics", []):
605609
if diagnostic["source"] != "mypy":
606610
continue
611+
if "code" not in diagnostic:
612+
continue
607613
code = diagnostic["code"]
608614
lineNumberEnd = diagnostic["range"]["end"]["line"]
609615
line = document.lines[lineNumberEnd]

test/test_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_parse_note_line(workspace):
116116
assert diag["range"]["start"] == {"line": 123, "character": 0}
117117
assert diag["range"]["end"] == {"line": 128, "character": 77}
118118
assert diag["severity"] == 3
119-
assert diag["code"] is None
119+
assert "code" not in diag
120120

121121

122122
def test_multiple_workspaces(tmpdir, last_diagnostics_monkeypatch):

0 commit comments

Comments
 (0)