8000 Add support for pytest 7 · realpython/pytest-mypy@d4970a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit d4970a4

Browse files
committed
Add support for pytest 7
1 parent 518aae9 commit d4970a4

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

src/pytest_mypy.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import os
5+
from pathlib import Path
56
from tempfile import NamedTemporaryFile
67
from typing import Dict, List, Optional, TextIO
78

@@ -11,6 +12,7 @@
1112
import pytest # type: ignore
1213

1314

15+
PYTEST_MAJOR_VERSION = int(pytest.__version__.partition(".")[0])
1416
mypy_argv = []
1517
nodeid_name = "mypy"
1618

@@ -105,9 +107,9 @@ def pytest_configure_node(self, node): # xdist hook
105107
mypy_argv.append("--config-file={}".format(mypy_config_file))
106108

107109

108-
def pytest_collect_file(path, parent):
110+
def pytest_collect_file(file_path, parent):
109111
"""Create a MypyFileItem for every file mypy should run on."""
110-
if path.ext in {".py", ".pyi"} and any(
112+
if file_path.suffix in {".py", ".pyi"} and any(
111113
[
112114
parent.config.option.mypy,
113115
parent.config.option.mypy_config_file,
@@ -117,11 +119,23 @@ def pytest_collect_file(path, parent):
117119
# Do not create MypyFile instance for a .py file if a
118120
# .pyi file with the same name already exists;
119121
# pytest will complain about duplicate modules otherwise
120-
if path.ext == ".pyi" or not path.new(ext=".pyi").isfile():
121-
return MypyFile.from_parent(parent=parent, fspath=path)
122+
if file_path.suffix == ".pyi" or not file_path.with_suffix(".pyi").is_file():
123+
return MypyFile.from_parent(parent=parent, path=file_path)
122124
return None
123125

124126

127+
if PYTEST_MAJOR_VERSION < 7: # pragma: no cover
128+
_pytest_collect_file = pytest_collect_file
129+
130+
def pytest_collect_file(path, parent): # type: ignore
131+
try:
132+
# https://docs.pytest.org/en/7.0.x/deprecations.html#py-path-local-arguments-for-hooks-replaced-with-pathlib-path
133+
return _pytest_collect_file(Path(str(path)), parent)
134+
except TypeError:
135+
# https://docs.pytest.org/en/7.0.x/deprecations.html#fspath-argument-for-node-constructors-replaced-with-pathlib-path
136+
return MypyFile.from_parent(parent=parent, fspath=path)
137+
138+
125139
class MypyFile(pytest.File):
126140

127141
"""A File that Mypy will run on."""

tox.ini

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
minversion = 3.20
44
isolated_build = true
55
envlist =
6-
py35-pytest{4.6, 5.0, 5.x, 6.0, 6.x}-mypy{0.50, 0.5x, 0.60, 0.6x, 0.70, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
7-
py36-pytest{4.6, 5.0, 5.x, 6.0, 6.x}-mypy{0.50, 0.5x, 0.60, 0.6x, 0.70, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
8-
py37-pytest{4.6, 5.0, 5.x, 6.0, 6.x}-mypy{0.50, 0.5x, 0.60, 0.6x, 0.70, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
9-
py38-pytest{4.6, 5.0, 5.x, 6.0, 6.x}-mypy{0.71, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
10-
py39-pytest{4.6, 5.0, 5.x, 6.0, 6.x}-mypy{0.78, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
11-
py310-pytest{6.2, 6.x}-mypy{0.78, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
6+
py35-pytest{4.6, 5.0, 5.x, 6.0, 6.x, 7.0, 7.x}-mypy{0.50, 0.5x, 0.60, 0.6x, 0.70, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
7+
py36-pytest{4.6, 5.0, 5.x, 6.0, 6.x, 7.0, 7.x}-mypy{0.50, 0.5x, 0.60, 0.6x, 0.70, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
8+
py37-pytest{4.6, 5.0, 5.x, 6.0, 6.x, 7.0, 7.x}-mypy{0.50, 0.5x, 0.60, 0.6x, 0.70, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
9+
py38-pytest{4.6, 5.0, 5.x, 6.0, 6.x, 7.0, 7.x}-mypy{0.71, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
10+
py39-pytest{4.6, 5.0, 5.x, 6.0, 6.x, 7.0, 7.x}-mypy{0.78, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
11+
py310-pytest{6.2, 6.x, 7.0, 7.x}-mypy{0.78, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
1212
publish
1313
static
1414

1515
[gh-actions]
1616
python =
17-
3.5: py35-pytest{4.6, 5.0, 5.x, 6.0, 6.x}-mypy{0.50, 0.5x, 0.60, 0.6x, 0.70, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
18-
3.6: py36-pytest{4.6, 5.0, 5.x, 6.0, 6.x}-mypy{0.50, 0.5x, 0.60, 0.6x, 0.70, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
19-
3.7: py37-pytest{4.6, 5.0, 5.x, 6.0, 6.x}-mypy{0.50, 0.5x, 0.60, 0.6x, 0.70, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
20-
3.8: py38-pytest{4.6, 5.0, 5.x, 6.0, 6.x}-mypy{0.71, 0.7x, 0.80, 0.8x, 0.90, 0.9x}, publish, static
21-
3.9: py39-pytest{4.6, 5.0, 5.x, 6.0, 6.x}-mypy{0.78, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
22-
3.10: py310-pytest{6.2, 6.x}-mypy{0.78, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
17+
3.5: py35-pytest{4.6, 5.0, 5.x, 6.0, 6.x, 7.0, 7.x}-mypy{0.50, 0.5x, 0.60, 0.6x, 0.70, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
18+
3.6: py36-pytest{4.6, 5.0, 5.x, 6.0, 6.x, 7.0, 7.x}-mypy{0.50, 0.5x, 0.60, 0.6x, 0.70, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
19+
3.7: py37-pytest{4.6, 5.0, 5.x, 6.0, 6.x, 7.0, 7.x}-mypy{0.50, 0.5x, 0.60, 0.6x, 0.70, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
20+
3.8: py38-pytest{4.6, 5.0, 5.x, 6.0, 6.x, 7.0, 7.x}-mypy{0.71, 0.7x, 0.80, 0.8x, 0.90, 0.9x}, publish, static
21+
3.9: py39-pytest{4.6, 5.0, 5.x, 6.0, 6.x, 7.0, 7.x}-mypy{0.78, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
22+
3.10: py310-pytest{6.2, 6.x, 7.0, 7.x}-mypy{0.78, 0.7x, 0.80, 0.8x, 0.90, 0.9x}
2323

2424
[testenv]
2525
deps =
@@ -29,6 +29,8 @@ deps =
2929
pytest6.0: pytest ~= 6.0.0
3030
pytest6.2: pytest ~= 6.2.0
3131
pytest6.x: pytest ~= 6.0
32+
pytest7.0: pytest ~= 7.0.0
33+
pytest7.x: pytest ~= 7.0
3234
mypy0.50: mypy >= 0.500, < 0.510
3335
mypy0.51: mypy >= 0.510, < 0.520
3436
mypy0.52: mypy >= 0.520, < 0.530

0 commit comments

Comments
 (0)
0