diff --git a/.travis.yml b/.travis.yml index 045697e0..d9e47b42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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" diff --git a/prospector/tools/mypy/__init__.py b/prospector/tools/mypy/__init__.py index 332dbf29..b179d5c7 100644 --- a/prospector/tools/mypy/__init__.py +++ b/prospector/tools/mypy/__init__.py @@ -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', ) @@ -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) @@ -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()] diff --git a/tests/tools/mypy/__init__.py b/tests/tools/mypy/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/tools/mypy/test_mypy_tool.py b/tests/tools/mypy/test_mypy_tool.py new file mode 100644 index 00000000..cb012578 --- /dev/null +++ b/tests/tools/mypy/test_mypy_tool.py @@ -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)