Skip to content

Commit be5843a

Browse files
committed
Parse mypy result with regex
1 parent 8c81ffb commit be5843a

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

pyls_mypy/plugin.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
11
# Copyright 2017 Mikael Knutsson
2+
import re
23
from mypy import api as mypy_api
34
from pyls import hookimpl
45

6+
line_pattern = r"(.*):(\d+):(\d+): (\w+): (.*)"
57

6-
@hookimpl
7-
def pyls_lint(document):
8-
args = ('--ignore-missing-imports',
9-
'--incremental',
10-
'--show-column-numbers',
11-
'--command', document.source)
128

13-
report, errors, _ = mypy_api.run(args)
14-
diagnostics = []
15-
for line in report.splitlines():
16-
split = line.split(':', 4)
17-
if len(split) == 5:
18-
_, lineno, offset, severity, msg = split
19-
else:
20-
_, lineno, severity, msg = split
21-
offset = 0
9+
def parse_line(line):
10+
result = re.match(line_pattern, line)
11+
if result:
12+
_, lineno, offset, severity, msg = result.groups()
2213
lineno = int(lineno)
2314
offset = int(offset)
2415
errno = 2
25-
if severity.strip() == 'error':
16+
if severity == 'error':
2617
errno = 1
27-
diagnostics.append({
18+
return {
2819
'source': 'mypy',
2920
'range': {
3021
'start': {'line': lineno - 1, 'character': offset},
3122
# There may be a better solution, but mypy does not provide end
3223
'end': {'line': lineno - 1, 'character': offset + 1}
3324
},
34-
'message': msg.strip(),
25+
'message': msg,
3526
'severity': errno
36-
})
27+
}
28+
29+
30+
@hookimpl
31+
def pyls_lint(document):
32+
args = ('--ignore-missing-imports',
33+
'--incremental',
34+
'--show-column-numbers',
35+
'--command', document.source)
36+
37+
report, errors, _ = mypy_api.run(args)
38+
39+
diagnostics = []
40+
for line in report.splitlines():
41+
diag = parse_line(line)
42+
if diag:
43+
diagnostics.append(diag)
3744

3845
return diagnostics

test/test_plugin.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
DOC_TYPE_ERR = """{}.append(3)
66
"""
77

8+
TEST_LINE = 'main.py:279:8: error: "Request" has no attribute "id"'
9+
10+
811
def test_plugin():
912
doc = Document(DOC_URI, DOC_TYPE_ERR)
1013
diags = plugin.pyls_lint(doc)
@@ -14,3 +17,10 @@ def test_plugin():
1417
assert diag['message'] == 'Dict[<nothing>, <nothing>] has no attribute "append"'
1518
assert diag['range']['start'] == {'line': 0, 'character': 0}
1619
assert diag['range']['end'] == {'line': 0, 'character': 1}
20+
21+
22+
def test_parse_line():
23+
diag = plugin.parse_line(TEST_LINE)
24+
assert diag['message'] == '"Request" has no attribute "id"'
25+
assert diag['range']['start'] == {'line': 278, 'character': 8}
26+
assert diag['range']['end'] == {'line': 278, 'character': 9}

0 commit comments

Comments
 (0)