Skip to content

Commit

Permalink
Merge pull request #146 from dmtucker/issue145
Browse files Browse the repository at this point in the history
Stop failing if mypy only produces notes
  • Loading branch information
dmtucker authored Dec 18, 2022
2 parents cf649b9 + a8637bb commit 62c03db
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ name: Validation
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- run: python -m pip install --upgrade tox-gh-actions
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## [0.10.3](https://github.com/dbader/pytest-mypy/milestone/21)
* Stop failing if mypy only produces notes.

## [0.10.2](https://github.com/dbader/pytest-mypy/milestone/20)
* Update and loosen [build-system] requirements.

Expand Down
13 changes: 12 additions & 1 deletion src/pytest_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Dict, List, Optional, TextIO
import warnings

import attr
from filelock import FileLock # type: ignore
Expand Down Expand Up @@ -202,7 +203,13 @@ def runtest(self):
abspath = os.path.abspath(str(self.fspath))
errors = results.abspath_errors.get(abspath)
if errors:
raise MypyError(file_error_formatter(self, results, errors))
if not all(
error.partition(":")[2].partition(":")[0].strip() == "note"
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

def reportinfo(self):
"""Produce a heading for the test report."""
Expand Down Expand Up @@ -314,6 +321,10 @@ class MypyError(Exception):
"""


class MypyWarning(pytest.PytestWarning):
"""A non-failure message regarding the mypy run."""


def pytest_terminal_summary(terminalreporter, config):
"""Report stderr and unrecognized lines from stdout."""
try:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_pytest_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,33 @@ def pyfunc(x: int) -> str:
assert result.ret != 0


def test_mypy_annotation_unchecked(testdir, xdist_args):
"""Verify that annotation-unchecked warnings do not manifest as an error."""
testdir.makepyfile(
"""
def pyfunc(x):
y: int = 2
return x * y
""",
)
result = testdir.runpytest_subprocess(*xdist_args)
result.assert_outcomes()
result = testdir.runpytest_subprocess("--mypy", *xdist_args)
mypy_file_checks = 1
mypy_status_check = 1
mypy_checks = mypy_file_checks + mypy_status_check
outcomes = {"passed": mypy_checks}
# mypy doesn't emit annotation-unchecked warnings until 0.990:
min_mypy_version = Version("0.990")
if MYPY_VERSION >= min_mypy_version and PYTEST_VERSION >= Version("7.0"):
# assert_outcomes does not support `warnings` until 7.x.
outcomes["warnings"] = 1
result.assert_outcomes(**outcomes)
if MYPY_VERSION >= min_mypy_version:
result.stdout.fnmatch_lines(["*MypyWarning*"])
assert result.ret == 0


def test_mypy_ignore_missings_imports(testdir, xdist_args):
"""
Verify that --mypy-ignore-missing-imports
Expand Down

0 comments on commit 62c03db

Please sign in to comment.