Skip to content

Commit 551f25c

Browse files
authored
Merge pull request #93 from dmtucker/output-format
Enable custom error formatting
2 parents 2367074 + 1d30d6d commit 551f25c

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/pytest_mypy.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
nodeid_name = 'mypy'
1515

1616

17+
def default_file_error_formatter(item, results, errors):
18+
"""Create a string to be displayed when mypy finds errors in a file."""
19+
return '\n'.join(errors)
20+
21+
22+
file_error_formatter = default_file_error_formatter
23+
24+
1725
def pytest_addoption(parser):
1826
"""Add options for enabling and running mypy."""
1927
group = parser.getgroup('mypy')
@@ -158,7 +166,7 @@ def runtest(self):
158166
abspath = os.path.abspath(str(self.fspath))
159167
errors = results['abspath_errors'].get(abspath)
160168
if errors:
161-
raise MypyError('\n'.join(errors))
169+
raise MypyError(file_error_formatter(self, results, errors))
162170

163171
def reportinfo(self):
164172
"""Produce a heading for the test report."""

tests/test_pytest_mypy.py

+27
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,30 @@ def pytest_collection_modifyitems(session, config, items):
289289
testdir.mkdir(name)
290290
result = testdir.runpytest_subprocess('--mypy', *xdist_args, name)
291291
assert result.ret != 0
292+
293+
294+
def test_api_error_formatter(testdir, xdist_args):
295+
"""Ensure that the plugin can be configured in a conftest.py."""
296+
testdir.makepyfile(bad='''
297+
def pyfunc(x: int) -> str:
298+
return x * 2
299+
''')
300+
testdir.makepyfile(conftest='''
301+
def custom_file_error_formatter(item, results, errors):
302+
return '\\n'.join(
303+
'{path}:{error}'.format(
304+
path=item.fspath,
305+
error=error,
306+
)
307+
for error in errors
308+
)
309+
310+
def pytest_configure(config):
311+
plugin = config.pluginmanager.getplugin('mypy')
312+
plugin.file_error_formatter = custom_file_error_formatter
313+
''')
314+
result = testdir.runpytest_subprocess('--mypy', *xdist_args)
315+
result.stdout.fnmatch_lines([
316+
'*/bad.py:2: error: Incompatible return value*',
317+
])
318+
assert result.ret != 0

0 commit comments

Comments
 (0)