Skip to content

Commit c7225b5

Browse files
committed
Add --mypy-xfail
1 parent 23131ba commit c7225b5

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

src/pytest_mypy/__init__.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ def pytest_addoption(parser: pytest.Parser) -> None:
8989
action="store_true",
9090
help="ignore mypy's exit status",
9191
)
92+
group.addoption(
93+
"--mypy-xfail",
94+
action="store_true",
95+
help="xfail mypy errors",
96+
)
9297

9398

9499
def _xdist_worker(config: pytest.Config) -> Dict[str, Any]:
@@ -170,6 +175,7 @@ def pytest_collect_file(
170175
parent.config.option.mypy_config_file,
171176
parent.config.option.mypy_ignore_missing_imports,
172177
parent.config.option.mypy_no_status_check,
178+
parent.config.option.mypy_xfail,
173179
],
174180
):
175181
# Do not create MypyFile instance for a .py file if a
@@ -234,6 +240,13 @@ def runtest(self) -> None:
234240
error.partition(":")[2].partition(":")[0].strip() == "note"
235241
for error in errors
236242
):
243+
if self.session.config.option.mypy_xfail:
244+
self.add_marker(
245+
pytest.mark.xfail(
246+
raises=MypyError,
247+
reason="mypy errors are expected by --mypy-xfail.",
248+
)
249+
)
237250
raise MypyError(file_error_formatter(self, results, errors))
238251
warnings.warn("\n" + "\n".join(errors), MypyWarning)
239252

@@ -253,6 +266,15 @@ def runtest(self) -> None:
253266
"""Raise a MypyError if mypy exited with a non-zero status."""
254267
results = MypyResults.from_session(self.session)
255268
if results.status:
269+
if self.session.config.option.mypy_xfail:
270+
self.add_marker(
271+
pytest.mark.xfail(
272+
raises=MypyError,
273+
reason=(
274+
"A non-zero mypy exit status is expected by --mypy-xfail."
275+
),
276+
)
277+
)
256278
raise MypyError(f"mypy exited with status {results.status}.")
257279

258280

@@ -366,9 +388,11 @@ def pytest_terminal_summary(
366388
except FileNotFoundError:
367389
# No MypyItems executed.
368390
return
369-
if results.unmatched_stdout or results.stderr:
391+
if config.option.mypy_xfail or results.unmatched_stdout or results.stderr:
370392
terminalreporter.section(terminal_summary_title)
371-
if results.unmatched_stdout:
393+
if config.option.mypy_xfail:
394+
terminalreporter.write(results.stdout)
395+
elif results.unmatched_stdout:
372396
color = {"red": True} if results.status else {"green": True}
373397
terminalreporter.write_line(results.unmatched_stdout, **color)
374398
if results.stderr:

tests/test_pytest_mypy.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,59 @@ def test_mypy_no_status_check(testdir, xdist_args):
561561
result = testdir.runpytest_subprocess("--mypy-no-status-check", *xdist_args)
562562
result.assert_outcomes(passed=mypy_file_checks)
563563
assert result.ret == pytest.ExitCode.OK
564+
565+
566+
def test_mypy_xfail_passes(testdir, xdist_args):
567+
"""Verify that --mypy-xfail passes passes."""
568+
testdir.makepyfile(thon="one: int = 1")
569+
result = testdir.runpytest_subprocess("--mypy", *xdist_args)
570+
mypy_file_checks = 1
571+
mypy_status_check = 1
572+
result.assert_outcomes(passed=mypy_file_checks + mypy_status_check)
573+
assert result.ret == pytest.ExitCode.OK
574+
result = testdir.runpytest_subprocess("--mypy-xfail", *xdist_args)
575+
result.assert_outcomes(passed=mypy_file_checks + mypy_status_check)
576+
assert result.ret == pytest.ExitCode.OK
577+
578+
579+
def test_mypy_xfail_xfails(testdir, xdist_args):
580+
"""Verify that --mypy-xfail xfails failures."""
581+
testdir.makepyfile(thon="one: str = 1")
582+
result = testdir.runpytest_subprocess("--mypy", *xdist_args)
583+
mypy_file_checks = 1
584+
mypy_status_check = 1
585+
result.assert_outcomes(failed=mypy_file_checks + mypy_status_check)
586+
assert result.ret == pytest.ExitCode.TESTS_FAILED
587+
result = testdir.runpytest_subprocess("--mypy-xfail", *xdist_args)
588+
result.assert_outcomes(xfailed=mypy_file_checks + mypy_status_check)
589+
assert result.ret == pytest.ExitCode.OK
590+
591+
592+
def test_mypy_xfail_reports_stdout(testdir, xdist_args):
593+
"""Verify that --mypy-xfail reports stdout from mypy."""
594+
stdout = "a distinct string on stdout"
595+
testdir.makepyfile(
596+
conftest=f"""
597+
import pytest
598+
599+
@pytest.hookimpl(trylast=True)
600+
def pytest_configure(config):
601+
pytest_mypy = config.pluginmanager.getplugin("mypy")
602+
mypy_config_stash = config.stash[pytest_mypy.stash_key["config"]]
603+
with open(mypy_config_stash.mypy_results_path, mode="w") as results_f:
604+
pytest_mypy.MypyResults(
605+
opts=[],
606+
stdout="{stdout}",
607+
stderr="",
608+
status=0,
609+
abspath_errors={{}},
610+
unmatched_stdout="",
611+
).dump(results_f)
612+
""",
613+
)
614+
result = testdir.runpytest_subprocess("--mypy", *xdist_args)
615+
assert result.ret == pytest.ExitCode.OK
616+
assert stdout not in result.stdout.str()
617+
result = testdir.runpytest_subprocess("--mypy-xfail", *xdist_args)
618+
assert result.ret == pytest.ExitCode.OK
619+
assert stdout in result.stdout.str()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy