Skip to content

Commit c428381

Browse files
kunhtkunpengjiz
kunhtkun
andauthored
Refine diagnostic severity for flake8 (#490)
Co-authored-by: Pengji Zhang <me@pengjiz.com>
1 parent 73b945b commit c428381

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

pylsp/plugins/flake8_lint.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
from pathlib import PurePath
1010
from subprocess import PIPE, Popen
1111

12+
from flake8.plugins.pyflakes import FLAKE8_PYFLAKES_CODES
13+
1214
from pylsp import hookimpl, lsp
15+
from pylsp.plugins.pyflakes_lint import PYFLAKES_ERROR_MESSAGES
1316

1417
log = logging.getLogger(__name__)
18+
1519
FIX_IGNORES_RE = re.compile(r"([^a-zA-Z0-9_,]*;.*(\W+||$))")
1620
UNNECESSITY_CODES = {
1721
"F401", # `module` imported but unused
@@ -20,6 +24,14 @@
2024
"F523", # .format(...) unused positional arguments
2125
"F841", # local variable `name` is assigned to but never used
2226
}
27+
# NOTE: If the user sets the flake8 executable with workspace configuration, the
28+
# error codes in this set may be inaccurate.
29+
ERROR_CODES = (
30+
# Errors from the pyflakes plugin of flake8
31+
{FLAKE8_PYFLAKES_CODES.get(m.__name__, "E999") for m in PYFLAKES_ERROR_MESSAGES}
32+
# Syntax error from flake8 itself
33+
| {"E999"}
34+
)
2335

2436

2537
@hookimpl
@@ -208,7 +220,7 @@ def parse_stdout(source, stdout):
208220
# show also the code in message
209221
msg = code + " " + msg
210222
severity = lsp.DiagnosticSeverity.Warning
211-
if code == "E999" or code[0] == "F":
223+
if code in ERROR_CODES:
212224
severity = lsp.DiagnosticSeverity.Error
213225
diagnostic = {
214226
"source": "flake8",

test/plugins/test_flake8_lint.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_flake8_unsaved(workspace):
4040
assert unused_var["code"] == "F841"
4141
assert unused_var["range"]["start"] == {"line": 5, "character": 1}
4242
assert unused_var["range"]["end"] == {"line": 5, "character": 11}
43-
assert unused_var["severity"] == lsp.DiagnosticSeverity.Error
43+
assert unused_var["severity"] == lsp.DiagnosticSeverity.Warning
4444
assert unused_var["tags"] == [lsp.DiagnosticTag.Unnecessary]
4545

4646

@@ -55,7 +55,7 @@ def test_flake8_lint(workspace):
5555
assert unused_var["code"] == "F841"
5656
assert unused_var["range"]["start"] == {"line": 5, "character": 1}
5757
assert unused_var["range"]["end"] == {"line": 5, "character": 11}
58-
assert unused_var["severity"] == lsp.DiagnosticSeverity.Error
58+
assert unused_var["severity"] == lsp.DiagnosticSeverity.Warning
5959
finally:
6060
os.remove(name)
6161

@@ -101,7 +101,7 @@ def test_flake8_respecting_configuration(workspace):
101101
"end": {"line": 5, "character": 11},
102102
},
103103
"message": "F841 local variable 'a' is assigned to but never used",
104-
"severity": 1,
104+
"severity": 2,
105105
"tags": [1],
106106
},
107107
]
@@ -116,7 +116,7 @@ def test_flake8_respecting_configuration(workspace):
116116
"end": {"line": 0, "character": 9},
117117
},
118118
"message": "F401 'os' imported but unused",
119-
"severity": 1,
119+
"severity": 2,
120120
"tags": [1],
121121
}
122122
]

0 commit comments

Comments
 (0)