Skip to content

Commit

Permalink
Merge pull request #162 from dmtucker/issue161
Browse files Browse the repository at this point in the history
Prevent AttributeError in pytest_terminal_summary
  • Loading branch information
dmtucker committed Feb 9, 2024
2 parents 82c41fb + 1ef88f9 commit 2684be7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/pytest_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions tests/test_pytest_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ 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
mypy_checks = mypy_file_checks + mypy_status_check
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):
Expand Down

0 comments on commit 2684be7

Please sign in to comment.