Skip to content

Commit

Permalink
Parse mypy result with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
tomv564 committed Aug 8, 2017
1 parent 8c81ffb commit be5843a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
45 changes: 26 additions & 19 deletions pyls_mypy/plugin.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
# Copyright 2017 Mikael Knutsson
import re
from mypy import api as mypy_api
from pyls import hookimpl

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

@hookimpl
def pyls_lint(document):
args = ('--ignore-missing-imports',
'--incremental',
'--show-column-numbers',
'--command', document.source)

report, errors, _ = mypy_api.run(args)
diagnostics = []
for line in report.splitlines():
split = line.split(':', 4)
if len(split) == 5:
_, lineno, offset, severity, msg = split
else:
_, lineno, severity, msg = split
offset = 0
def parse_line(line):
result = re.match(line_pattern, line)
if result:
_, lineno, offset, severity, msg = result.groups()
lineno = int(lineno)
offset = int(offset)
errno = 2
if severity.strip() == 'error':
if severity == 'error':
errno = 1
diagnostics.append({
return {
'source': 'mypy',
'range': {
'start': {'line': lineno - 1, 'character': offset},
# There may be a better solution, but mypy does not provide end
'end': {'line': lineno - 1, 'character': offset + 1}
},
'message': msg.strip(),
'message': msg,
'severity': errno
})
}


@hookimpl
def pyls_lint(document):
args = ('--ignore-missing-imports',
'--incremental',
'--show-column-numbers',
'--command', document.source)

report, errors, _ = mypy_api.run(args)

diagnostics = []
for line in report.splitlines():
diag = parse_line(line)
if diag:
diagnostics.append(diag)

return diagnostics
10 changes: 10 additions & 0 deletions test/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
DOC_TYPE_ERR = """{}.append(3)
"""

TEST_LINE = 'main.py:279:8: error: "Request" has no attribute "id"'


def test_plugin():
doc = Document(DOC_URI, DOC_TYPE_ERR)
diags = plugin.pyls_lint(doc)
Expand All @@ -14,3 +17,10 @@ def test_plugin():
assert diag['message'] == 'Dict[<nothing>, <nothing>] has no attribute "append"'
assert diag['range']['start'] == {'line': 0, 'character': 0}
assert diag['range']['end'] == {'line': 0, 'character': 1}


def test_parse_line():
diag = plugin.parse_line(TEST_LINE)
assert diag['message'] == '"Request" has no attribute "id"'
assert diag['range']['start'] == {'line': 278, 'character': 8}
assert diag['range']['end'] == {'line': 278, 'character': 9}

0 comments on commit be5843a

Please sign in to comment.