Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more kinds of mypy messages #343

Merged
merged 1 commit into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)