8000 Stop failing if mypy only produces notes · realpython/pytest-mypy@af41ec0 · GitHub
[go: up one dir, main page]

Skip to content

Commit af41ec0

Browse files
committed
Stop failing if mypy only produces notes
1 parent 35fab38 commit af41ec0

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/pytest_mypy.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pathlib import Path
66
from tempfile import NamedTemporaryFile
77
from typing import Dict, List, Optional, TextIO
8+
import warnings
89

910
import attr
1011
from filelock import FileLock # type: ignore
@@ -202,7 +203,12 @@ def runtest(self):
202203
abspath = os.path.abspath(str(self.fspath))
203204
errors = results.abspath_errors.get(abspath)
204205
if errors:
205-
raise MypyError(file_error_formatter(self, results, errors))
206+
if not all(
207+
error.partition(":")[2].partition(":")[0].strip() == "note"
208+
for error in errors
209+
):
210+
raise MypyError(file_error_formatter(self, results, errors))
211+
warnings.warn("\n" + "\n".join(errors), MypyWarning)
206212

207213
def reportinfo(self):
208214
"""Produce a heading for the test report."""
@@ -314,6 +320,10 @@ class MypyError(Exception):
314320
"""
315321

316322

323+
class MypyWarning(pytest.PytestWarning):
324+
"""A non-failure message regarding the mypy run."""
325+
326+
317327
def pytest_terminal_summary(terminalreporter, config):
318328
"""Report stderr and unrecognized lines from stdout."""
319329
try:

tests/test_pytest_mypy.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,26 @@ def pyfunc(x: int) -> str:
9898
assert result.ret != 0
9999

100100

101+
def test_mypy_annotation_unchecked(testdir, xdist_args):
102+
"""Verify that annotation-unchecked warnings do not manifest as an error."""
103+
testdir.makepyfile(
104+
"""
105+
def pyfunc(x):
106+
y: int = 2
107+
return x * y
108+
""",
109+
)
110+
result = testdir.runpytest_subprocess(*xdist_args)
111+
result.assert_outcomes()
112+
result = testdir.runpytest_subprocess("--mypy", *xdist_args)
113+
mypy_file_checks = 1
114+
mypy_status_check = 1
115+
mypy_checks = mypy_file_checks + mypy_status_check
116+
result.assert_outcomes(passed=mypy_checks, warnings=1)
117+
result.stdout.fnmatch_lines(["*MypyWarning*"])
118+
assert result.ret == 0
119+
120+
101121
def test_mypy_ignore_missings_imports(testdir, xdist_args):
102122
"""
103123
Verify that --mypy-ignore-missing-imports

0 commit comments

Comments
 (0)
0