Skip to content

Refine diagnostic severity for flake8 #490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from Dec 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion pylsp/plugins/flake8_lint.py
Original file line number Diff line number Diff line change
@@ -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",
8 changes: 4 additions & 4 deletions test/plugins/test_flake8_lint.py
Original file line number Diff line number Diff line change
@@ -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],
}
]