Skip to content

Commit

Permalink
Support more kinds of mypy messages
Browse files Browse the repository at this point in the history
Extracts a `format_message` to help unit testing

fix #339
  • Loading branch information
rik committed Jul 22, 2019
1 parent 6d97770 commit f1eeb92
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 27 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ matrix:
allow_failures:
- python: "nightly"
install:
- "pip install --editable .[build_tools]"
- "pip install --editable .[with_everything]"
script:
- "nosetests -s --with-coverage --cover-inclusive --cover-package=prospector tests/"
- "mv .coverage .coverage.x"
- "pip install -e .[with_everything]"
- "coverage run --parallel-mode prospector/run.py || true"
- "coverage combine"
- "coverage report --show-missing"
Expand Down
49 changes: 24 additions & 25 deletions prospector/tools/mypy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# -*- coding: utf-8 -*-
from itertools import islice
from mypy import api

from prospector.message import Location, Message
from prospector.tools import ToolBase


__all__ = (
'MypyTool',
)
Expand All @@ -21,6 +18,28 @@
]


def format_message(message):
try:
(path, line, char, err_type, err_msg) = message.split(':', 4)
character = int(char)
except ValueError:
(path, line, err_type, err_msg) = message.split(':', 3)
character = None
location = Location(
path=path,
module=None,
function=None,
line=int(line),
character=character,
)
return Message(
source='mypy',
code=err_type.lstrip(" "),
location=location,
message=err_msg.lstrip(" ")
)


class MypyTool(ToolBase):
def __init__(self, *args, **kwargs):
super(MypyTool, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -72,25 +91,5 @@ def run(self, found_files):
paths.extend(self.options)
result = self.checker.run(paths)
report, _ = result[0], result[1:] # noqa
messages = []

for message in report.splitlines():
iter_message = iter(message.split(':'))
(path, line, char, err_type, err_msg) = islice(iter_message, 5)
location = Location(
path=path,
module=None,
function=None,
line=int(line),
character=int(char),
absolute_path=True
)
message = Message(
source='mypy',
code=err_type.lstrip(" "),
location=location,
message=err_msg.lstrip(" ")
)
messages.append(message)

return messages

return [format_message(message) for message in report.splitlines()]
Empty file added tests/tools/mypy/__init__.py
Empty file.
42 changes: 42 additions & 0 deletions tests/tools/mypy/test_mypy_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
from unittest import SkipTest, TestCase

from prospector.message import Location, Message

try:
from prospector.tools.mypy import format_message
except ImportError:
raise SkipTest


class TestMypyTool(TestCase):
def test_format_message_with_character(self):
location = Location(
path="file.py", module=None, function=None, line=17, character=2
)
expected = Message(
source="mypy", code="error", location=location, message="Important error"
)
self.assertEqual(
format_message("file.py:17:2: error: Important error"), expected
)

def test_format_message_without_character_and_columns_in_message(self):
location = Location(
path="file.py", module=None, function=None, line=17, character=None
)
expected = Message(
source="mypy", code="note", location=location, message="Important error"
)
self.assertEqual(
format_message('file.py:17: note: unused "type: ignore" comment'), expected
)

def test_format_message_without_character(self):
location = Location(
path="file.py", module=None, function=None, line=17, character=None
)
expected = Message(
source="mypy", code="error", location=location, message="Important error"
)
self.assertEqual(format_message("file.py:17: error: Important error"), expected)

0 comments on commit f1eeb92

Please sign in to comment.