Skip to content

Commit 2684be7

Browse files
authored
Merge pull request #162 from dmtucker/issue161
Prevent AttributeError in pytest_terminal_summary
2 parents 82c41fb + 1ef88f9 commit 2684be7

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/pytest_mypy.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -327,17 +327,18 @@ class MypyWarning(pytest.PytestWarning):
327327

328328
def pytest_terminal_summary(terminalreporter, config):
329329
"""Report stderr and unrecognized lines from stdout."""
330-
try:
331-
with open(config._mypy_results_path, mode="r") as results_f:
332-
results = MypyResults.load(results_f)
333-
except FileNotFoundError:
334-
# No MypyItems executed.
335-
return
336-
if results.unmatched_stdout or results.stderr:
337-
terminalreporter.section("mypy")
338-
if results.unmatched_stdout:
339-
color = {"red": True} if results.status else {"green": True}
340-
terminalreporter.write_line(results.unmatched_stdout, **color)
341-
if results.stderr:
342-
terminalreporter.write_line(results.stderr, yellow=True)
343-
os.remove(config._mypy_results_path)
330+
if _is_master(config):
331+
try:
332+
with open(config._mypy_results_path, mode="r") as results_f:
333+
results = MypyResults.load(results_f)
334+
except FileNotFoundError:
335+
# No MypyItems executed.
336+
return
337+
if results.unmatched_stdout or results.stderr:
338+
terminalreporter.section("mypy")
339+
if results.unmatched_stdout:
340+
color = {"red": True} if results.status else {"green": True}
341+
terminalreporter.write_line(results.unmatched_stdout, **color)
342+
if results.stderr:
343+
terminalreporter.write_line(results.stderr, yellow=True)
344+
os.remove(config._mypy_results_path)

tests/test_pytest_mypy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ def pyfunc(x: int) -> str:
8989
)
9090
result = testdir.runpytest_subprocess(*xdist_args)
9191
result.assert_outcomes()
92+
assert "_mypy_results_path" not in result.stderr.str()
9293
result = testdir.runpytest_subprocess("--mypy", *xdist_args)
9394
mypy_file_checks = 1
9495
mypy_status_check = 1
9596
mypy_checks = mypy_file_checks + mypy_status_check
9697
result.assert_outcomes(failed=mypy_checks)
9798
result.stdout.fnmatch_lines(["2: error: Incompatible return value*"])
9899
assert result.ret != 0
100+
assert "_mypy_results_path" not in result.stderr.str()
99101

100102

101103
def test_mypy_annotation_unchecked(testdir, xdist_args):

0 commit comments

Comments
 (0)