Skip to content

Commit f9ec407

Browse files
committed
Clamp Flake8 diagnostic columns to nonnegative positions
1 parent a362006 commit f9ec407

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎pylsp/plugins/flake8_lint.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ def parse_stdout(source, stdout):
223223

224224
_, line, character, code, msg = parsed_line
225225
line = int(line) - 1
226-
character = int(character) - 1
226+
# Some plugins report column zero when no precise column is available.
227+
character = max(0, int(character) - 1)
227228
# show also the code in message
228229
msg = code + " " + msg
229230
severity = lsp.DiagnosticSeverity.Warning

‎test/plugins/test_flake8_lint.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from textwrap import dedent
77
from unittest.mock import patch
88

9+
import pytest
10+
911
from pylsp import lsp, uris
1012
from pylsp.plugins import flake8_lint
1113
from pylsp.workspace import Document
@@ -60,6 +62,33 @@ def test_flake8_lint(workspace) -> None:
6062
os.remove(name)
6163

6264

65+
@pytest.mark.parametrize(
66+
"line, column, expected_character",
67+
[
68+
(" # comment", 0, 0),
69+
("", 0, 0),
70+
(" # comment", 1, 0),
71+
(" # comment", 5, 4),
72+
],
73+
)
74+
def test_flake8_diagnostic_columns(workspace, line, column, expected_character) -> None:
75+
source = f"def myfun():\n{line}\n pass\n"
76+
doc = Document("", workspace, source)
77+
output = (
78+
f"stdin:2:{column}: ANN201 Missing return type annotation for public function\n"
79+
)
80+
81+
with patch("pylsp.plugins.flake8_lint.run_flake8", return_value=output):
82+
diagnostics = flake8_lint.pylsp_lint(workspace, doc)
83+
84+
assert len(diagnostics) == 1
85+
assert diagnostics[0]["range"] == {
86+
"start": {"line": 1, "character": expected_character},
87+
"end": {"line": 1, "character": len(line) + 1},
88+
}
89+
assert diagnostics[0]["code"] == "ANN201"
90+
91+
6392
def test_flake8_respecting_configuration(workspace) -> None:
6493
docs = [
6594
("src/__init__.py", ""),

0 commit comments

Comments
 (0)