8000 Merge pull request #146 from dmtucker/issue145 · realpython/pytest-mypy@62c03db · GitHub
[go: up one dir, main page]

Skip to content

Commit 62c03db

Browse files
authored
Merge pull request #146 from dmtucker/issue145
Stop failing if mypy only produces notes
2 parents cf649b9 + a8637bb commit 62c03db

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

.github/workflows/validation.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ name: Validation
22
on: [push, pull_request]
33
jobs:
44
build:
5-
runs-on: ubuntu-latest
5+
runs-on: ubuntu-20.04
66
strategy:
77
matrix:
88
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"]
99
steps:
10-
- uses: actions/checkout@v2
11-
- uses: actions/setup-python@v2
10+
- uses: actions/checkout@v3
11+
- uses: actions/setup-python@v4
1212
with:
1313
python-version: ${{ matrix.python-version }}
1414
- run: python -m pip install --upgrade tox-gh-actions

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## [0.10.3](https://github.com/dbader/pytest-mypy/milestone/21)
4+
* Stop failing if mypy only produces notes.
5+
36
## [0.10.2](https://github.com/dbader/pytest-mypy/milestone/20)
47
* Update and loosen [build-system] requirements.
58

src/pytest_mypy.py

Lines changed: 12 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,13 @@ 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+
# This line cannot be easily covered on mypy < 0.990:
212+
warnings.warn("\n" + "\n".join(errors), MypyWarning) # pragma: no cover
206213

207214
def reportinfo(self):
208215
"""Produce a heading for the test report."""
@@ -314,6 +321,10 @@ class MypyError(Exception):
314321
"""
315322

316323

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

tests/test_pytest_mypy.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,33 @@ 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+
outcomes = {"passed": mypy_checks}
117+
# mypy doesn't emit annotation-unchecked warnings until 0.990:
118+
min_mypy_version = Version("0.990")
119+
if MYPY_VERSION >= min_mypy_version and PYTEST_VERSION >= Version("7.0"):
120+
# assert_outcomes does not support `warnings` until 7.x.
121+
outcomes["warnings"] = 1
122+
result.assert_outcomes(**outcomes)
123+
if MYPY_VERSION >= min_mypy_version:
124+
result.stdout.fnmatch_lines(["*MypyWarning*"])
125+
assert result.ret == 0
126+
127+
101128
def test_mypy_ignore_missings_imports(testdir, xdist_args):
102129
"""
103130
Verify that --mypy-ignore-missing-imports

0 commit comments

Comments
 (0)
0