forked from realpython/pytest-mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytest_mypy.py
280 lines (222 loc) · 8.89 KB
/
pytest_mypy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""Mypy static type checker plugin for Pytest"""
import functools
import json
import os
from tempfile import NamedTemporaryFile
from filelock import FileLock # type: ignore
import mypy.api
import pytest # type: ignore
mypy_argv = []
nodeid_name = 'mypy'
def default_file_error_formatter(item, results, errors):
"""Create a string to be displayed when mypy finds errors in a file."""
return '\n'.join(errors)
file_error_formatter = default_file_error_formatter
def pytest_addoption(parser):
"""Add options for enabling and running mypy."""
group = parser.getgroup('mypy')
group.addoption(
'--mypy', action='store_true',
help='run mypy on .py files')
group.addoption(
'--mypy-ignore-missing-imports', action='store_true',
help="suppresses error messages about imports that cannot be resolved")
def _is_master(config):
"""
True if the code running the given pytest.config object is running in
an xdist master node or not running xdist at all.
"""
return not hasattr(config, 'slaveinput')
def pytest_configure(config):
"""
Initialize the path used to cache mypy results,
register a custom marker for MypyItems,
and configure the plugin based on the CLI.
"""
if _is_master(config):
# Get the path to a temporary file and delete it.
# The first MypyItem to run will see the file does not exist,
# and it will run and parse mypy results to create it.
# Subsequent MypyItems will see the file exists,
# and they will read the parsed results.
with NamedTemporaryFile(delete=True) as tmp_f:
config._mypy_results_path = tmp_f.name
# If xdist is enabled, then the results path should be exposed to
# the slaves so that they know where to read parsed results from.
if config.pluginmanager.getplugin('xdist'):
class _MypyXdistPlugin:
def pytest_configure_node(self, node): # xdist hook
"""Pass config._mypy_results_path to workers."""
node.slaveinput['_mypy_results_path'] = \
node.config._mypy_results_path
config.pluginmanager.register(_MypyXdistPlugin())
# pytest_terminal_summary cannot accept config before pytest 4.2.
global _pytest_terminal_summary_config
_pytest_terminal_summary_config = config
config.addinivalue_line(
'markers',
'{marker}: mark tests to be checked by mypy.'.format(
marker=MypyItem.MARKER,
),
)
if config.getoption('--mypy-ignore-missing-imports'):
mypy_argv.append('--ignore-missing-imports')
def pytest_collect_file(path, parent):
"""Create a MypyFileItem for every file mypy should run on."""
if path.ext in {'.py', '.pyi'} and any([
parent.config.option.mypy,
parent.config.option.mypy_ignore_missing_imports,
]):
# Do not create MypyFile instance for a .py file if a
# .pyi file with the same name already exists;
# pytest will complain about duplicate modules otherwise
if path.ext == '.pyi' or not path.new(ext='.pyi').isfile():
return MypyFile.from_parent(parent=parent, fspath=path)
return None
class MypyFile(pytest.File):
"""A File that Mypy will run on."""
@classmethod
def from_parent(cls, *args, **kwargs):
"""Override from_parent for compatibility."""
# pytest.File.from_parent did not exist before pytest 5.4.
return getattr(super(), 'from_parent', cls)(*args, **kwargs)
def collect(self):
"""Create a MypyFileItem for the File."""
yield MypyFileItem.from_parent(parent=self, name=nodeid_name)
@pytest.hookimpl(hookwrapper=True, trylast=True)
def pytest_collection_modifyitems(session, config, items):
"""
Add a MypyStatusItem if any MypyFileItems were collected.
Since mypy might check files that were not collected,
pytest could pass even though mypy failed!
To prevent that, add an explicit check for the mypy exit status.
This should execute as late as possible to avoid missing any
MypyFileItems injected by other pytest_collection_modifyitems
implementations.
"""
yield
if any(isinstance(item, MypyFileItem) for item in items):
items.append(
MypyStatusItem.from_parent(parent=session, name=nodeid_name),
)
class MypyItem(pytest.Item):
"""A Mypy-related test Item."""
MARKER = 'mypy'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_marker(self.MARKER)
@classmethod
def from_parent(cls, *args, **kwargs):
"""Override from_parent for compatibility."""
# pytest.Item.from_parent did not exist before pytest 5.4.
return getattr(super(), 'from_parent', cls)(*args, **kwargs)
def repr_failure(self, excinfo):
"""
Unwrap mypy errors so we get a clean error message without the
full exception repr.
"""
if excinfo.errisinstance(MypyError):
return excinfo.value.args[0]
return super().repr_failure(excinfo)
class MypyFileItem(MypyItem):
"""A check for Mypy errors in a File."""
def runtest(self):
"""Raise an exception if mypy found errors for this item."""
results = _mypy_results(self.session)
abspath = os.path.abspath(str(self.fspath))
errors = results['abspath_errors'].get(abspath)
if errors:
raise MypyError(file_error_formatter(self, results, errors))
def reportinfo(self):
"""Produce a heading for the test report."""
return (
self.fspath,
None,
self.config.invocation_dir.bestrelpath(self.fspath),
)
class MypyStatusItem(MypyItem):
"""A check for a non-zero mypy exit status."""
def runtest(self):
"""Raise a MypyError if mypy exited with a non-zero status."""
results = _mypy_results(self.session)
if results['status']:
raise MypyError(
'mypy exited with status {status}.'.format(
status=results['status'],
),
)
def _mypy_results(session):
"""Get the cached mypy results for the session, or generate them."""
return _cached_json_results(
results_path=(
session.config._mypy_results_path
if _is_master(session.config) else
session.config.slaveinput['_mypy_results_path']
),
results_factory=functools.partial(
_mypy_results_factory,
abspaths=[
os.path.abspath(str(item.fspath))
for item in session.items
if isinstance(item, MypyFileItem)
],
)
)
def _cached_json_results(results_path, results_factory=None):
"""
Read results from results_path if it exists;
otherwise, produce them with results_factory,
and write them to results_path.
"""
with FileLock(results_path + '.lock'):
try:
with open(results_path, mode='r') as results_f:
results = json.load(results_f)
except FileNotFoundError:
if not results_factory:
raise
results = results_factory()
with open(results_path, mode='w') as results_f:
json.dump(results, results_f)
return results
def _mypy_results_factory(abspaths):
"""Run mypy on abspaths and return the results as a JSON-able dict."""
stdout, stderr, status = mypy.api.run(mypy_argv + abspaths)
abspath_errors, unmatched_lines = {}, []
for line in stdout.split('\n'):
if not line:
continue
path, _, error = line.partition(':')
abspath = os.path.abspath(path)
if abspath in abspaths:
abspath_errors[abspath] = abspath_errors.get(abspath, []) + [error]
else:
unmatched_lines.append(line)
return {
'stdout': stdout,
'stderr': stderr,
'status': status,
'abspath_errors': abspath_errors,
'unmatched_stdout': '\n'.join(unmatched_lines),
}
class MypyError(Exception):
"""
An error caught by mypy, e.g a type checker violation
or a syntax error.
"""
def pytest_terminal_summary(terminalreporter):
"""Report stderr and unrecognized lines from stdout."""
config = _pytest_terminal_summary_config
try:
results = _cached_json_results(config._mypy_results_path)
except FileNotFoundError:
# No MypyItems executed.
return
if results['unmatched_stdout'] or results['stderr']:
terminalreporter.section('mypy')
if results['unmatched_stdout']:
color = {'red': True} if results['status'] else {'green': True}
terminalreporter.write_line(results['unmatched_stdout'], **color)
if results['stderr']:
terminalreporter.write_line(results['stderr'], yellow=True)
os.remove(config._mypy_results_path)