Skip to content

Commit 40455f9

Browse files
committed
Add --mypy-default-report-style
1 parent e236fdb commit 40455f9

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-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: 1 addition & 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):

0 commit comments

Comments
 (0)