diff --git a/src/pytest_mypy/__init__.py b/src/pytest_mypy/__init__.py index 87f597c..40d8c3d 100644 --- a/src/pytest_mypy/__init__.py +++ b/src/pytest_mypy/__init__.py @@ -84,6 +84,11 @@ def pytest_addoption(parser: pytest.Parser) -> None: type=str, help="adds custom mypy config file", ) + group.addoption( + "--mypy-no-status-check", + action="store_true", + help="ignore mypy's exit status", + ) def _xdist_worker(config: pytest.Config) -> Dict[str, Any]: @@ -164,6 +169,7 @@ def pytest_collect_file( parent.config.option.mypy, parent.config.option.mypy_config_file, parent.config.option.mypy_ignore_missing_imports, + parent.config.option.mypy_no_status_check, ], ): # Do not create MypyFile instance for a .py file if a @@ -183,7 +189,9 @@ def collect(self) -> Iterator[MypyItem]: # Since mypy might check files that were not collected, # pytest could pass even though mypy failed! # To prevent that, add an explicit check for the mypy exit status. - if not any(isinstance(item, MypyStatusItem) for item in self.session.items): + if not self.session.config.option.mypy_no_status_check and not any( + isinstance(item, MypyStatusItem) for item in self.session.items + ): yield MypyStatusItem.from_parent( parent=self, name=nodeid_name + "-status", diff --git a/tests/test_pytest_mypy.py b/tests/test_pytest_mypy.py index d3d8948..3143521 100644 --- a/tests/test_pytest_mypy.py +++ b/tests/test_pytest_mypy.py @@ -548,3 +548,16 @@ def test_py_typed(testdir): testdir.makepyfile(**{name: "import pytest_mypy"}) result = testdir.run("mypy", f"{name}.py") assert result.ret == 0 + + +def test_mypy_no_status_check(testdir, xdist_args): + """Verify that --mypy-no-status-check disables MypyStatusItem collection.""" + testdir.makepyfile(thon="one: int = 1") + result = testdir.runpytest_subprocess("--mypy", *xdist_args) + mypy_file_checks = 1 + mypy_status_check = 1 + result.assert_outcomes(passed=mypy_file_checks + mypy_status_check) + assert result.ret == pytest.ExitCode.OK + result = testdir.runpytest_subprocess("--mypy-no-status-check", *xdist_args) + result.assert_outcomes(passed=mypy_file_checks) + assert result.ret == pytest.ExitCode.OK