Skip to content

Commit 7e232a1

Browse files
committed
Add --mypy-default-report-style
1 parent e236fdb commit 7e232a1

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

src/pytest_mypy/__init__.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ def default_test_name_formatter(*, item: MypyFileItem) -> str:
6868
def default_file_error_formatter(
6969
item: MypyItem,
7070
results: MypyResults,
71-
errors: List[str],
71+
lines: List[str],
7272
) -> str:
7373
"""Create a string to be displayed when mypy finds errors in a file."""
74-
return "\n".join(errors)
74+
if item.config.option.mypy_default_report_style == "mypy":
75+
return "\n".join(lines)
76+
# "pytest" style
77+
return "\n".join(line.partition(":")[2].strip() for line in lines)
7578

7679

7780
file_error_formatter = default_file_error_formatter
@@ -92,6 +95,14 @@ def pytest_addoption(parser: pytest.Parser) -> None:
9295
type=str,
9396
help="adds custom mypy config file",
9497
)
98+
group.addoption(
99+
"--mypy-default-report-style",
100+
choices=[
101+
"mypy",
102+
"pytest",
103+
],
104+
help="change the way mypy output is reported",
105+
)
95106
group.addoption(
96107
"--mypy-no-status-check",
97108
action="store_true",
@@ -175,6 +186,7 @@ def pytest_configure(config: pytest.Config) -> None:
175186
[
176187
config.option.mypy,
177188
config.option.mypy_config_file,
189+
config.option.mypy_default_report_style,
178190
config.option.mypy_ignore_missing_imports,
179191
config.option.mypy_no_status_check,
180192
config.option.mypy_xfail,
@@ -268,13 +280,7 @@ def runtest(self) -> None:
268280
reason="mypy errors are expected by --mypy-xfail.",
269281
)
270282
)
271-
raise MypyError(
272-
file_error_formatter(
273-
self,
274-
results,
275-
errors=[line.partition(":")[2].strip() for line in lines],
276-
)
277-
)
283+
raise MypyError(file_error_formatter(self, results, lines))
278284

279285
def reportinfo(self) -> Tuple[Path, None, str]:
280286
"""Produce a heading for the test report."""

tests/test_pytest_mypy.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def pyfunc(x: int) -> str:
410410
file_error = "UnmistakableFileError"
411411
testdir.makepyfile(
412412
conftest=f"""
413-
def custom_file_error_formatter(item, results, errors):
413+
def custom_file_error_formatter(item, results, lines):
414414
return '{file_error}'
415415
416416
def pytest_configure(config):
@@ -666,3 +666,33 @@ def pytest_configure(config):
666666
def test_error_severity():
667667
"""Verify that non-error lines produce no severity."""
668668
assert pytest_mypy._error_severity("arbitrary line with no error") is None
669+
670+
671+
def test_mypy_default_report_style(testdir, xdist_args):
672+
"""Verify that --mypy-default-report-style functions correctly."""
673+
module_name = "unmistakable_module_name"
674+
testdir.makepyfile(
675+
**{
676+
module_name: """
677+
def pyfunc(x: int) -> str:
678+
return x * 2
679+
"""
680+
},
681+
)
682+
result = testdir.runpytest_subprocess(
683+
"--mypy-default-report-style", "pytest", *xdist_args
684+
)
685+
mypy_file_checks = 1
686+
mypy_status_check = 1
687+
mypy_checks = mypy_file_checks + mypy_status_check
688+
result.assert_outcomes(failed=mypy_checks)
689+
result.stdout.fnmatch_lines(["2: error: Incompatible return value*"])
690+
assert result.ret == pytest.ExitCode.TESTS_FAILED
691+
result = testdir.runpytest_subprocess(
692+
"--mypy-default-report-style", "mypy", *xdist_args
693+
)
694+
result.assert_outcomes(failed=mypy_checks)
695+
result.stdout.fnmatch_lines(
696+
[f"{module_name}.py:2: error: Incompatible return value*"]
697+
)
698+
assert result.ret == pytest.ExitCode.TESTS_FAILED

0 commit comments

Comments
 (0)