8000 Stop failing if mypy only produces notes by dmtucker · Pull Request #146 · realpython/pytest-mypy · GitHub
[go: up one dir, main page]

Skip to content

Stop failing if mypy only produces notes #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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