Skip to content

Commit 031514f

Browse files
David Fritzschedavidfritzsche
David Fritzsche
authored andcommitted
Run tests with mypy 0.971
1 parent 8c3f364 commit 031514f

File tree

9 files changed

+137
-69
lines changed

9 files changed

+137
-69
lines changed

Diff for: .github/workflows/pythonpackage.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ jobs:
9595
python-version: ${{ matrix.python-version }}
9696
- name: Install dependencies
9797
run: |
98-
python -m pip install -c constraints.txt flake8 flake8-isort black mypy
98+
python -m pip install -c constraints.txt black flake8 flake8-isort mypy pytest
9999
pip list
100100
- name: Run black
101101
run: |

Diff for: constraints.txt

+9-5
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ distlib==0.3.4
2020
docutils==0.18.1
2121
filelock==3.7.0
2222
flake8==4.0.1
23-
flake8-black==0.3.3
24-
flake8-isort==4.1.1
23+
flake8-bugbear==22.7.1
24+
flake8-comprehensions==3.10.0
25+
flake8-html==0.4.2
26+
flake8-logging-format==0.6.0
27+
flake8-mutable==1.2.0
28+
flake8-pyi==22.7.0
2529
fsfe-reuse==1.0.0
2630
idna==3.3
2731
iniconfig==1.1.1
@@ -31,7 +35,7 @@ jinja2==3.1.2
3135
license-expression==30.0.0
3236
markupsafe==2.1.1
3337
mccabe==0.6.1
34-
mypy==0.931
38+
mypy==0.971
3539
mypy-extensions==0.4.3
3640
packaging==21.3
3741
pathspec==0.9.0
@@ -42,8 +46,9 @@ pluggy==1.0.0
4246
py==1.11.0
4347
pycodestyle==2.8.0
4448
pyflakes==2.4.0
49+
pygments==2.12.0
4550
pyparsing==3.0.9
46-
pytest==7.0.1
51+
pytest==7.1.2
4752
pytest-cov==3.0.0
4853
pytest-html==3.1.1
4954
pytest-metadata==2.0.1
@@ -52,7 +57,6 @@ pytoml==0.1.21
5257
requests==2.27.1
5358
reuse==1.0.0
5459
six==1.16.0
55-
testfixtures==6.18.5
5660
toml==0.10.2
5761
tomli==2.0.1
5862
tox==3.25.0

Diff for: mypy.ini

-9
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,3 @@ warn_return_any = True
4747
# Strict Optional checks.
4848
# If False, mypy treats None as compatible with every type. (default True)
4949
strict_optional = True
50-
51-
[mypy-py.*]
52-
ignore_missing_imports = True
53-
54-
[mypy-pytest]
55-
ignore_missing_imports = True
56-
57-
[mypy-_pytest.*]
58-
ignore_missing_imports = True

Diff for: requirements.in

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@ black<23
44
bump2version
55
coverage[toml]
66
dflit
7-
flake8-black
8-
flake8-isort
7+
flake8-bugbear
8+
flake8-comprehensions
9+
flake8-html
10+
flake8-logging-format
11+
flake8-mutable
12+
flake8-pyi
913
fsfe-reuse
1014
invoke
11-
mypy==0.931
15+
isort
16+
mypy==0.971
1217
pip-tools
1318
pip>=19.3
1419
pytest-cov
1520
pytest-html
16-
pytest~=7.0.1
21+
pytest~=7.1.2
1722
setuptools>=43
1823
tox-pyenv
1924
tox>=3.14.3

Diff for: requirements.txt

+30-16
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
# pip-compile --allow-unsafe --no-emit-index-url
66
#
77
attrs==21.4.0
8-
# via pytest
8+
# via
9+
# flake8-bugbear
10+
# pytest
911
binaryornot==0.4.4
1012
# via reuse
1113
black==22.3.0
12-
# via
13-
# -r requirements.in
14-
# flake8-black
14+
# via -r requirements.in
1515
boolean-py==4.0
1616
# via
1717
# license-expression
@@ -48,11 +48,22 @@ filelock==3.7.0
4848
# virtualenv
4949
flake8==4.0.1
5050
# via
51-
# flake8-black
52-
# flake8-isort
53-
flake8-black==0.3.3
51+
# flake8-bugbear
52+
# flake8-comprehensions
53+
# flake8-html
54+
# flake8-mutable
55+
# flake8-pyi
56+
flake8-bugbear==22.7.1
57+
# via -r requirements.in
58+
flake8-comprehensions==3.10.0
59+
# via -r requirements.in
60+
flake8-html==0.4.2
5461
# via -r requirements.in
55-
flake8-isort==4.1.1
62+
flake8-logging-format==0.6.0
63+
# via -r requirements.in
64+
flake8-mutable==1.2.0
65+
# via -r requirements.in
66+
flake8-pyi==22.7.0
5667
# via -r requirements.in
5768
fsfe-reuse==1.0.0
5869
# via -r requirements.in
@@ -63,16 +74,18 @@ iniconfig==1.1.1
6374
invoke==1.7.1
6475
# via -r requirements.in
6576
isort==5.10.1
66-
# via flake8-isort
77+
# via -r requirements.in
6778
jinja2==3.1.2
68-
# via reuse
79+
# via
80+
# flake8-html
81+
# reuse
6982
license-expression==30.0.0
7083
# via reuse
7184
markupsafe==2.1.1
7285
# via jinja2
7386
mccabe==0.6.1
7487
# via flake8
75-
mypy==0.931
88+
mypy==0.971
7689
# via -r requirements.in
7790
mypy-extensions==0.4.3
7891
# via
@@ -103,10 +116,14 @@ py==1.11.0
103116
pycodestyle==2.8.0
104117
# via flake8
105118
pyflakes==2.4.0
106-
# via flake8
119+
# via
120+
# flake8
121+
# flake8-pyi
122+
pygments==2.12.0
123+
# via flake8-html
107124
pyparsing==3.0.9
108125
# via packaging
109-
pytest==7.0.1
126+
pytest==7.1.2
110127
# via
111128
# -r requirements.in
112129
# pytest-cov
@@ -134,15 +151,12 @@ six==1.16.0
134151
# via
135152
# tox
136153
# virtualenv
137-
testfixtures==6.18.5
138-
# via flake8-isort
139154
toml==0.10.2
140155
# via tox
141156
tomli==2.0.1
142157
# via
143158
# black
144159
# coverage
145-
# flake8-black
146160
# mypy
147161
# pep517
148162
# pytest

Diff for: src/pytest_mypy_testing/message.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import dataclasses
66
import enum
77
import os
8+
import pathlib
89
import re
9-
from typing import Optional, Tuple
10+
from typing import Optional, Tuple, Union
1011

1112

1213
__all__ = [
@@ -166,7 +167,9 @@ def __str__(self) -> str:
166167
return f"{self._prefix} {self.severity.name.lower()}: {self.message}"
167168

168169
@classmethod
169-
def from_comment(cls, filename: str, lineno: int, comment: str) -> "Message":
170+
def from_comment(
171+
cls, filename: Union[pathlib.Path, str], lineno: int, comment: str
172+
) -> "Message":
170173
"""Create message object from Python *comment*.
171174
172175
>>> Message.from_comment("foo.py", 1, "R: foo")
@@ -183,7 +186,7 @@ def from_comment(cls, filename: str, lineno: int, comment: str) -> "Message":
183186
else:
184187
revealed_type = None
185188
return Message(
186-
filename,
189+
str(filename),
187190
lineno=lineno,
188191
colno=colno,
189192
severity=Severity.from_string(m.group("severity")),

Diff for: src/pytest_mypy_testing/parser.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
import io
88
import itertools
99
import os
10+
import pathlib
1011
import sys
1112
import tokenize
12-
from typing import Iterable, Iterator, List, Optional, Set, Tuple
13+
from typing import Iterable, Iterator, List, Optional, Set, Tuple, Union
1314

1415
from .message import Message
1516

@@ -71,7 +72,7 @@ class MypyTestFile:
7172

7273

7374
def iter_comments(
74-
filename: str, token_lists: List[List[tokenize.TokenInfo]]
75+
filename: Union[pathlib.Path, str], token_lists: List[List[tokenize.TokenInfo]]
7576
) -> Iterator[tokenize.TokenInfo]:
7677
for toks in token_lists:
7778
for tok in toks:
@@ -80,7 +81,7 @@ def iter_comments(
8081

8182

8283
def iter_mypy_comments(
83-
filename: str, tokens: List[List[tokenize.TokenInfo]]
84+
filename: Union[pathlib.Path, str], tokens: List[List[tokenize.TokenInfo]]
8485
) -> Iterator[Message]:
8586
for tok in iter_comments(filename, tokens):
8687
try:
@@ -103,17 +104,17 @@ def generate_per_line_token_lists(source: str) -> Iterator[List[tokenize.TokenIn
103104
i += 1
104105

105106

106-
def parse_file(filename: str, config) -> MypyTestFile:
107+
def parse_file(filename: Union[os.PathLike, str, pathlib.Path], config) -> MypyTestFile:
107108
"""Parse *filename* and return information about mypy test cases."""
108-
filename = os.path.abspath(filename)
109+
filename = pathlib.Path(filename).resolve()
109110
with open(filename, "r", encoding="utf-8") as f:
110111
source_text = f.read()
111112

112113
source_lines = source_text.splitlines()
113114
token_lists = list(generate_per_line_token_lists(source_text))
114115
messages = list(iter_mypy_comments(filename, token_lists))
115116

116-
tree = ast.parse(source_text, filename=filename)
117+
tree = ast.parse(source_text, filename=str(filename))
117118
if sys.version_info < (3, 8):
118119
_add_end_lineno_if_missing(tree, len(source_lines))
119120

@@ -131,7 +132,10 @@ def parse_file(filename: str, config) -> MypyTestFile:
131132
)
132133

133134
return MypyTestFile(
134-
filename=filename, source_lines=source_lines, items=items, messages=messages
135+
filename=str(filename),
136+
source_lines=source_lines,
137+
items=items,
138+
messages=messages,
135139
)
136140

137141

@@ -140,18 +144,18 @@ def _add_end_lineno_if_missing(tree, line_count: int):
140144
prev_node: Optional[ast.AST] = None
141145
for node in ast.iter_child_nodes(tree):
142146
if prev_node is not None:
143-
setattr(prev_node, "end_lineno", node.lineno)
147+
setattr(prev_node, "end_lineno", node.lineno) # noqa: B010
144148
prev_node = node
145149
if prev_node:
146-
setattr(prev_node, "end_lineno", line_count)
150+
setattr(prev_node, "end_lineno", line_count) # noqa: B010
147151

148152

149153
def _find_marks(func_node: ast.FunctionDef) -> Set[str]:
150-
return set(
154+
return {
151155
name.split(".", 2)[2]
152156
for name, _ in _iter_func_decorators(func_node)
153157
if name.startswith("pytest.mark.")
154-
)
158+
}
155159

156160

157161
def _iter_func_decorators(func_node: ast.FunctionDef) -> Iterator[Tuple[str, ast.AST]]:

Diff for: src/pytest_mypy_testing/plugin.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
# SPDX-License-Identifier: Apache-2.0 OR MIT
33

44
import os
5+
import pathlib
56
import tempfile
6-
from typing import Iterable, Iterator, List, NamedTuple, Optional, Tuple
7+
from typing import Iterable, Iterator, List, NamedTuple, Optional, Tuple, Union
78

89
import mypy.api
910
import pytest
1011
from _pytest._code.code import ReprEntry, ReprFileLocation
1112
from _pytest.config import Config
12-
from py._path.local import LocalPath
1313

1414
from .message import Message, Severity
1515
from .output_processing import OutputMismatch, diff_message_sequences
@@ -36,6 +36,8 @@ def __init__(self, item, errors: Iterable[OutputMismatch]):
3636

3737

3838
class PytestMypyTestItem(pytest.Item):
39+
parent: "PytestMypyFile"
40+
3941
def __init__(
4042
self,
4143
name: str,
@@ -72,7 +74,7 @@ def runtest(self) -> None:
7274
if errors:
7375
raise MypyAssertionError(item=self, errors=errors)
7476

75-
def reportinfo(self) -> Tuple[str, Optional[int], str]:
77+
def reportinfo(self) -> Tuple[Union["os.PathLike[str]", str], Optional[int], str]:
7678
return self.parent.fspath, self.mypy_item.lineno, self.name
7779

7880
def repr_failure(self, excinfo, style=None):
@@ -153,7 +155,8 @@ def run_mypy(self, item: MypyTestItem) -> Tuple[int, List[Message]]:
153155
),
154156
)
155157

156-
def _run_mypy(self, filename: str) -> MypyResult:
158+
def _run_mypy(self, filename: Union[pathlib.Path, os.PathLike, str]) -> MypyResult:
159+
filename = pathlib.Path(filename)
157160
with tempfile.TemporaryDirectory(prefix="pytest-mypy-testing-") as tmp_dir_name:
158161

159162
mypy_cache_dir = os.path.join(tmp_dir_name, "mypy_cache")
@@ -178,9 +181,6 @@ def _run_mypy(self, filename: str) -> MypyResult:
178181

179182
lines = (out + err).splitlines()
180183

181-
# for line in lines:
182-
# print("%%%%", line)
183-
184184
file_messages = [
185185
msg
186186
for msg in map(Message.from_output, lines)
@@ -213,7 +213,7 @@ def _run_mypy(self, filename: str) -> MypyResult:
213213

214214
if PYTEST_VERSION_INFO < (7,):
215215

216-
def pytest_collect_file(path: LocalPath, parent):
216+
def pytest_collect_file(path, parent):
217217
if path.ext == ".mypy-testing" or _is_pytest_test_file(path, parent):
218218
file = PytestMypyFile.from_parent(parent=parent, fspath=path)
219219
if file.mypy_file.items:
@@ -222,15 +222,15 @@ def pytest_collect_file(path: LocalPath, parent):
222222

223223
else:
224224

225-
def pytest_collect_file(file_path, path: LocalPath, parent): # type: ignore
225+
def pytest_collect_file(file_path, path, parent): # type: ignore
226226
if path.ext == ".mypy-testing" or _is_pytest_test_file(path, parent):
227227
file = PytestMypyFile.from_parent(parent=parent, path=file_path)
228228
if file.mypy_file.items:
229229
return file
230230
return None
231231

232232

233-
def _is_pytest_test_file(path: LocalPath, parent):
233+
def _is_pytest_test_file(path, parent):
234234
"""Return `True` if *path* is considered to be a pytest test file."""
235235
# Based on _pytest/python.py::pytest_collect_file
236236
fn_patterns = parent.config.getini("python_files") + ["__init__.py"]
@@ -259,4 +259,4 @@ def _add_reveal_type_to_builtins():
259259
import builtins
260260

261261
if not hasattr(builtins, "reveal_type"):
262-
setattr(builtins, "reveal_type", lambda x: x)
262+
setattr(builtins, "reveal_type", lambda x: x) # noqa: B010

0 commit comments

Comments
 (0)