Skip to content

Commit

Permalink
Enable branch code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtucker committed Feb 14, 2024
1 parent a266479 commit fcde05e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/pytest_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ def runtest(self):
for error in errors
):
raise MypyError(file_error_formatter(self, results, errors))
# This line cannot be easily covered on mypy < 0.990:
warnings.warn("\n" + "\n".join(errors), MypyWarning) # pragma: no cover
warnings.warn("\n" + "\n".join(errors), MypyWarning)

def reportinfo(self):
"""Produce a heading for the test report."""
Expand Down Expand Up @@ -258,7 +257,9 @@ def from_mypy(
) -> "MypyResults":
"""Generate results from mypy."""

if opts is None:
# This is covered by test_mypy_results_from_mypy_with_opts;
# however, coverage is not recognized on py38-pytest4.6:
if opts is None: # pragma: no cover
opts = mypy_argv[:]
abspath_errors = {
os.path.abspath(str(item.fspath)): [] for item in items
Expand Down
83 changes: 82 additions & 1 deletion tests/test_pytest_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import pexpect
import pytest

import pytest_mypy


MYPY_VERSION = Version(mypy.version.__version__)
PYTEST_VERSION = Version(pytest.__version__)
Expand Down Expand Up @@ -100,7 +102,7 @@ def pyfunc(x: int) -> str:
assert "_mypy_results_path" not in result.stderr.str()


def test_mypy_annotation_unchecked(testdir, xdist_args):
def test_mypy_annotation_unchecked(testdir, xdist_args, tmp_path, monkeypatch):
"""Verify that annotation-unchecked warnings do not manifest as an error."""
testdir.makepyfile(
"""
Expand All @@ -109,6 +111,29 @@ def pyfunc(x):
return x * y
""",
)
min_mypy_version = Version("0.990")
if MYPY_VERSION < min_mypy_version:
# mypy doesn't emit annotation-unchecked warnings until 0.990:
fake_mypy_path = tmp_path / "mypy"
fake_mypy_path.mkdir()
(fake_mypy_path / "__init__.py").touch()
(fake_mypy_path / "api.py").write_text(
textwrap.dedent(
"""
def run(*args, **kwargs):
return (
"test_mypy_annotation_unchecked.py:2:"
" note: By default the bodies of untyped functions"
" are not checked, consider using --check-untyped-defs"
" [annotation-unchecked]\\nSuccess: no issues found in"
" 1 source file\\n",
"",
0,
)
"""
)
)
monkeypatch.setenv("PYTHONPATH", str(tmp_path))
result = testdir.runpytest_subprocess(*xdist_args)
result.assert_outcomes()
result = testdir.runpytest_subprocess("--mypy", *xdist_args)
Expand Down Expand Up @@ -552,3 +577,59 @@ def test_mypy_item_collect(request):
mypy_status_check = 1
result.assert_outcomes(passed=test_count + mypy_file_checks + mypy_status_check)
assert result.ret == 0


@pytest.mark.xfail(
MYPY_VERSION < Version("0.750"),
raises=AssertionError,
reason="https://github.com/python/mypy/issues/7800",
)
def test_mypy_results_from_mypy_with_opts():
"""MypyResults.from_mypy respects passed options."""
mypy_results = pytest_mypy.MypyResults.from_mypy([], opts=["--version"])
assert mypy_results.status == 0
assert mypy_results.abspath_errors == {}
assert str(MYPY_VERSION) in mypy_results.stdout


def test_mypy_no_output(testdir, xdist_args):
"""No terminal summary is shown if there is no output from mypy."""
type_ignore = (
"# type: ignore"
if (
PYTEST_VERSION
< Version("6.0") # Pytest didn't add type annotations until 6.0.
or MYPY_VERSION < Version("0.710")
)
else ""
)
testdir.makepyfile(
conftest=f"""
import tempfile
import pytest {type_ignore}
@pytest.hookimpl(hookwrapper=True)
def pytest_terminal_summary(config):
pytest_mypy = config.pluginmanager.getplugin("mypy")
with open(config._mypy_results_path, mode="r") as results_f:
results = pytest_mypy.MypyResults.load(results_f)
with open(config._mypy_results_path, mode="w") as results_f:
pytest_mypy.MypyResults(
opts=results.opts,
stdout=results.stdout,
stderr="",
status=results.status,
abspath_errors=results.abspath_errors,
unmatched_stdout="",
).dump(results_f)
yield
""",
)
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(passed=mypy_checks)
assert result.ret == 0
assert "= mypy =" not in str(result.stdout)
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ deps =

packaging ~= 21.3
pexpect ~= 4.8.0
pytest-cov ~= 2.10
pytest-cov ~= 4.1.0
pytest-randomly ~= 3.4
pytest-xdist ~= 1.34

commands = pytest -p no:mypy {posargs:--cov pytest_mypy --cov-fail-under 100 --cov-report term-missing -n auto}
commands = pytest -p no:mypy {posargs:--cov pytest_mypy --cov-branch --cov-fail-under 100 --cov-report term-missing -n auto}

[pytest]
testpaths = tests
Expand Down

0 comments on commit fcde05e

Please sign in to comment.