diff --git a/pylsp/plugins/flake8_lint.py b/pylsp/plugins/flake8_lint.py index 8d8d4c5f..77aa22b9 100644 --- a/pylsp/plugins/flake8_lint.py +++ b/pylsp/plugins/flake8_lint.py @@ -9,9 +9,13 @@ from pathlib import PurePath from subprocess import PIPE, Popen +from flake8.plugins.pyflakes import FLAKE8_PYFLAKES_CODES + from pylsp import hookimpl, lsp +from pylsp.plugins.pyflakes_lint import PYFLAKES_ERROR_MESSAGES log = logging.getLogger(__name__) + FIX_IGNORES_RE = re.compile(r"([^a-zA-Z0-9_,]*;.*(\W+||$))") UNNECESSITY_CODES = { "F401", # `module` imported but unused @@ -20,6 +24,14 @@ "F523", # .format(...) unused positional arguments "F841", # local variable `name` is assigned to but never used } +# NOTE: If the user sets the flake8 executable with workspace configuration, the +# error codes in this set may be inaccurate. +ERROR_CODES = ( + # Errors from the pyflakes plugin of flake8 + {FLAKE8_PYFLAKES_CODES.get(m.__name__, "E999") for m in PYFLAKES_ERROR_MESSAGES} + # Syntax error from flake8 itself + | {"E999"} +) @hookimpl @@ -208,7 +220,7 @@ def parse_stdout(source, stdout): # show also the code in message msg = code + " " + msg severity = lsp.DiagnosticSeverity.Warning - if code == "E999" or code[0] == "F": + if code in ERROR_CODES: severity = lsp.DiagnosticSeverity.Error diagnostic = { "source": "flake8", diff --git a/test/plugins/test_flake8_lint.py b/test/plugins/test_flake8_lint.py index 882bc996..c2d711e7 100644 --- a/test/plugins/test_flake8_lint.py +++ b/test/plugins/test_flake8_lint.py @@ -40,7 +40,7 @@ def test_flake8_unsaved(workspace): assert unused_var["code"] == "F841" assert unused_var["range"]["start"] == {"line": 5, "character": 1} assert unused_var["range"]["end"] == {"line": 5, "character": 11} - assert unused_var["severity"] == lsp.DiagnosticSeverity.Error + assert unused_var["severity"] == lsp.DiagnosticSeverity.Warning assert unused_var["tags"] == [lsp.DiagnosticTag.Unnecessary] @@ -55,7 +55,7 @@ def test_flake8_lint(workspace): assert unused_var["code"] == "F841" assert unused_var["range"]["start"] == {"line": 5, "character": 1} assert unused_var["range"]["end"] == {"line": 5, "character": 11} - assert unused_var["severity"] == lsp.DiagnosticSeverity.Error + assert unused_var["severity"] == lsp.DiagnosticSeverity.Warning finally: os.remove(name) @@ -101,7 +101,7 @@ def test_flake8_respecting_configuration(workspace): "end": {"line": 5, "character": 11}, }, "message": "F841 local variable 'a' is assigned to but never used", - "severity": 1, + "severity": 2, "tags": [1], }, ] @@ -116,7 +116,7 @@ def test_flake8_respecting_configuration(workspace): "end": {"line": 0, "character": 9}, }, "message": "F401 'os' imported but unused", - "severity": 1, + "severity": 2, "tags": [1], } ]