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

Enable custom error formatting #93

Merged
merged 1 commit into from
Aug 10, 2020
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
10 changes: 9 additions & 1 deletion src/pytest_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
nodeid_name = 'mypy'


def default_file_error_formatter(item, results, errors):
"""Create a string to be displayed when mypy finds errors in a file."""
return '\n'.join(errors)


file_error_formatter = default_file_error_formatter


def pytest_addoption(parser):
"""Add options for enabling and running mypy."""
group = parser.getgroup('mypy')
Expand Down Expand Up @@ -158,7 +166,7 @@ def runtest(self):
abspath = os.path.abspath(str(self.fspath))
errors = results['abspath_errors'].get(abspath)
if errors:
raise MypyError('\n'.join(errors))
raise MypyError(file_error_formatter(self, results, errors))

def reportinfo(self):
"""Produce a heading for the test report."""
Expand Down
27 changes: 27 additions & 0 deletions tests/test_pytest_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,30 @@ def pytest_collection_modifyitems(session, config, items):
testdir.mkdir(name)
result = testdir.runpytest_subprocess('--mypy', *xdist_args, name)
assert result.ret != 0


def test_api_error_formatter(testdir, xdist_args):
"""Ensure that the plugin can be configured in a conftest.py."""
testdir.makepyfile(bad='''
def pyfunc(x: int) -> str:
return x * 2
''')
testdir.makepyfile(conftest='''
def custom_file_error_formatter(item, results, errors):
return '\\n'.join(
'{path}:{error}'.format(
path=item.fspath,
error=error,
)
for error in errors
)

def pytest_configure(config):
plugin = config.pluginmanager.getplugin('mypy')
plugin.file_error_formatter = custom_file_error_formatter
''')
result = testdir.runpytest_subprocess('--mypy', *xdist_args)
result.stdout.fnmatch_lines([
'*/bad.py:2: error: Incompatible return value*',
])
assert result.ret != 0