From be5843ae7a96836114acd96f6b70a5936f52dc1a Mon Sep 17 00:00:00 2001 From: Tom van Ommeren Date: Tue, 8 Aug 2017 17:47:24 +0200 Subject: [PATCH] Parse mypy result with regex --- pyls_mypy/plugin.py | 45 ++++++++++++++++++++++++++------------------- test/test_plugin.py | 10 ++++++++++ 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/pyls_mypy/plugin.py b/pyls_mypy/plugin.py index 6346b79..dd07fc4 100644 --- a/pyls_mypy/plugin.py +++ b/pyls_mypy/plugin.py @@ -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 diff --git a/test/test_plugin.py b/test/test_plugin.py index 1c7ff6a..5426cd4 100644 --- a/test/test_plugin.py +++ b/test/test_plugin.py @@ -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) @@ -14,3 +17,10 @@ def test_plugin(): assert diag['message'] == 'Dict[, ] 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}