From 1ef88f9fc32328d7f30709fdb43a771194b2fb3e Mon Sep 17 00:00:00 2001 From: David Tucker Date: Thu, 8 Feb 2024 01:05:41 -0800 Subject: [PATCH] Prevent AttributeError in pytest_terminal_summary --- src/pytest_mypy.py | 29 +++++++++++++++-------------- tests/test_pytest_mypy.py | 2 ++ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/pytest_mypy.py b/src/pytest_mypy.py index faa02a7..82dbd2b 100644 --- a/src/pytest_mypy.py +++ b/src/pytest_mypy.py @@ -327,17 +327,18 @@ class MypyWarning(pytest.PytestWarning): def pytest_terminal_summary(terminalreporter, config): """Report stderr and unrecognized lines from stdout.""" - try: - with open(config._mypy_results_path, mode="r") as results_f: - results = MypyResults.load(results_f) - except FileNotFoundError: - # No MypyItems executed. - return - if results.unmatched_stdout or results.stderr: - terminalreporter.section("mypy") - if results.unmatched_stdout: - color = {"red": True} if results.status else {"green": True} - terminalreporter.write_line(results.unmatched_stdout, **color) - if results.stderr: - terminalreporter.write_line(results.stderr, yellow=True) - os.remove(config._mypy_results_path) + if _is_master(config): + try: + with open(config._mypy_results_path, mode="r") as results_f: + results = MypyResults.load(results_f) + except FileNotFoundError: + # No MypyItems executed. + return + if results.unmatched_stdout or results.stderr: + terminalreporter.section("mypy") + if results.unmatched_stdout: + color = {"red": True} if results.status else {"green": True} + terminalreporter.write_line(results.unmatched_stdout, **color) + if results.stderr: + terminalreporter.write_line(results.stderr, yellow=True) + os.remove(config._mypy_results_path) diff --git a/tests/test_pytest_mypy.py b/tests/test_pytest_mypy.py index 924cdd2..29bdf8f 100644 --- a/tests/test_pytest_mypy.py +++ b/tests/test_pytest_mypy.py @@ -89,6 +89,7 @@ def pyfunc(x: int) -> str: ) result = testdir.runpytest_subprocess(*xdist_args) result.assert_outcomes() + assert "_mypy_results_path" not in result.stderr.str() result = testdir.runpytest_subprocess("--mypy", *xdist_args) mypy_file_checks = 1 mypy_status_check = 1 @@ -96,6 +97,7 @@ def pyfunc(x: int) -> str: result.assert_outcomes(failed=mypy_checks) result.stdout.fnmatch_lines(["2: error: Incompatible return value*"]) assert result.ret != 0 + assert "_mypy_results_path" not in result.stderr.str() def test_mypy_annotation_unchecked(testdir, xdist_args):