forked from realpython/pytest-mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pytest_mypy.py
315 lines (273 loc) · 9.94 KB
/
test_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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import pytest
@pytest.fixture(
params=[
True, # xdist enabled, active
False, # xdist enabled, inactive
None, # xdist disabled
],
)
def xdist_args(request):
if request.param is None:
return ['-p', 'no:xdist']
return ['-n', 'auto'] if request.param else []
@pytest.mark.parametrize('pyfile_count', [1, 2])
def test_mypy_success(testdir, pyfile_count, xdist_args):
"""Verify that running on a module with no type errors passes."""
testdir.makepyfile(
**{
'pyfile_' + str(pyfile_i): '''
def pyfunc(x: int) -> int:
return x * 2
'''
for pyfile_i in range(pyfile_count)
}
)
result = testdir.runpytest_subprocess(*xdist_args)
result.assert_outcomes()
result = testdir.runpytest_subprocess('--mypy', *xdist_args)
mypy_file_checks = pyfile_count
mypy_status_check = 1
mypy_checks = mypy_file_checks + mypy_status_check
result.assert_outcomes(passed=mypy_checks)
assert result.ret == 0
def test_mypy_pyi(testdir, xdist_args):
"""
Verify that a .py file will be skipped if
a .pyi file exists with the same filename.
"""
# The incorrect signature below should be ignored
# as the .pyi file takes priority
testdir.makefile(
'.py', pyfile='''
def pyfunc(x: int) -> str:
return x * 2
'''
)
testdir.makefile(
'.pyi', pyfile='''
def pyfunc(x: int) -> int: ...
'''
)
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
result.assert_outcomes(passed=mypy_checks)
assert result.ret == 0
def test_mypy_error(testdir, xdist_args):
"""Verify that running on a module with type errors fails."""
testdir.makepyfile('''
def pyfunc(x: int) -> str:
return x * 2
''')
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
result.assert_outcomes(failed=mypy_checks)
result.stdout.fnmatch_lines([
'2: error: Incompatible return value*',
])
assert result.ret != 0
def test_mypy_ignore_missings_imports(testdir, xdist_args):
"""
Verify that --mypy-ignore-missing-imports
causes mypy to ignore missing imports.
"""
module_name = 'is_always_missing'
testdir.makepyfile('''
try:
import {module_name}
except ImportError:
pass
'''.format(module_name=module_name))
result = testdir.runpytest_subprocess('--mypy', *xdist_args)
mypy_file_checks = 1
mypy_status_check = 1
mypy_checks = mypy_file_checks + mypy_status_check
result.assert_outcomes(failed=mypy_checks)
result.stdout.fnmatch_lines([
"2: error: Cannot find *module named '{module_name}'".format(
module_name=module_name,
),
])
assert result.ret != 0
result = testdir.runpytest_subprocess(
'--mypy-ignore-missing-imports',
*xdist_args
)
result.assert_outcomes(passed=mypy_checks)
assert result.ret == 0
def test_mypy_marker(testdir, xdist_args):
"""Verify that -m mypy only runs the mypy tests."""
testdir.makepyfile('''
def test_fails():
assert False
''')
result = testdir.runpytest_subprocess('--mypy', *xdist_args)
test_count = 1
mypy_file_checks = 1
mypy_status_check = 1
mypy_checks = mypy_file_checks + mypy_status_check
result.assert_outcomes(failed=test_count, passed=mypy_checks)
assert result.ret != 0
result = testdir.runpytest_subprocess('--mypy', '-m', 'mypy', *xdist_args)
result.assert_outcomes(passed=mypy_checks)
assert result.ret == 0
def test_non_mypy_error(testdir, xdist_args):
"""Verify that non-MypyError exceptions are passed through the plugin."""
message = 'This is not a MypyError.'
testdir.makepyfile(conftest='''
def pytest_configure(config):
plugin = config.pluginmanager.getplugin('mypy')
class PatchedMypyFileItem(plugin.MypyFileItem):
def runtest(self):
raise Exception('{message}')
plugin.MypyFileItem = PatchedMypyFileItem
'''.format(message=message))
result = testdir.runpytest_subprocess(*xdist_args)
result.assert_outcomes()
result = testdir.runpytest_subprocess('--mypy', *xdist_args)
mypy_file_checks = 1 # conftest.py
mypy_status_check = 1
result.assert_outcomes(
failed=mypy_file_checks, # patched to raise an Exception
passed=mypy_status_check, # conftest.py has no type errors.
)
result.stdout.fnmatch_lines(['*' + message])
assert result.ret != 0
def test_mypy_stderr(testdir, xdist_args):
"""Verify that stderr from mypy is printed."""
stderr = 'This is stderr from mypy.'
testdir.makepyfile(conftest='''
import mypy.api
def _patched_run(*args, **kwargs):
return '', '{stderr}', 1
mypy.api.run = _patched_run
'''.format(stderr=stderr))
result = testdir.runpytest_subprocess('--mypy', *xdist_args)
result.stdout.fnmatch_lines([stderr])
def test_mypy_unmatched_stdout(testdir, xdist_args):
"""Verify that unexpected output on stdout from mypy is printed."""
stdout = 'This is unexpected output on stdout from mypy.'
testdir.makepyfile(conftest='''
import mypy.api
def _patched_run(*args, **kwargs):
return '{stdout}', '', 1
mypy.api.run = _patched_run
'''.format(stdout=stdout))
result = testdir.runpytest_subprocess('--mypy', *xdist_args)
result.stdout.fnmatch_lines([stdout])
def test_api_mypy_argv(testdir, xdist_args):
"""Ensure that the plugin can be configured in a conftest.py."""
testdir.makepyfile(conftest='''
def pytest_configure(config):
plugin = config.pluginmanager.getplugin('mypy')
plugin.mypy_argv.append('--version')
''')
result = testdir.runpytest_subprocess('--mypy', *xdist_args)
assert result.ret == 0
def test_api_nodeid_name(testdir, xdist_args):
"""Ensure that the plugin can be configured in a conftest.py."""
nodeid_name = 'UnmistakableNodeIDName'
testdir.makepyfile(conftest='''
def pytest_configure(config):
plugin = config.pluginmanager.getplugin('mypy')
plugin.nodeid_name = '{}'
'''.format(nodeid_name))
result = testdir.runpytest_subprocess('--mypy', '--verbose', *xdist_args)
result.stdout.fnmatch_lines(['*conftest.py::' + nodeid_name + '*'])
assert result.ret == 0
def test_pytest_collection_modifyitems(testdir, xdist_args):
"""
Verify that collected files which are removed in a
pytest_collection_modifyitems implementation are not
checked by mypy.
This would also fail if a MypyStatusItem were injected
despite there being no MypyFileItems.
"""
testdir.makepyfile(conftest='''
def pytest_collection_modifyitems(session, config, items):
plugin = config.pluginmanager.getplugin('mypy')
for mypy_item_i in reversed([
i
for i, item in enumerate(items)
if isinstance(item, plugin.MypyFileItem)
]):
items.pop(mypy_item_i)
''')
testdir.makepyfile('''
def pyfunc(x: int) -> str:
return x * 2
def test_pass():
pass
''')
result = testdir.runpytest_subprocess('--mypy', *xdist_args)
test_count = 1
result.assert_outcomes(passed=test_count)
assert result.ret == 0
def test_mypy_indirect(testdir, xdist_args):
"""Verify that uncollected files checked by mypy cause a failure."""
testdir.makepyfile(bad='''
def pyfunc(x: int) -> str:
return x * 2
''')
testdir.makepyfile(good='''
import bad
''')
result = testdir.runpytest_subprocess('--mypy', *xdist_args, 'good.py')
assert result.ret != 0
def test_mypy_indirect_inject(testdir, xdist_args):
"""
Verify that uncollected files checked by mypy because of a MypyFileItem
injected in pytest_collection_modifyitems cause a failure.
"""
testdir.makepyfile(bad='''
def pyfunc(x: int) -> str:
return x * 2
''')
testdir.makepyfile(good='''
import bad
''')
testdir.makepyfile(conftest='''
import py
import pytest
@pytest.hookimpl(trylast=True) # Inject as late as possible.
def pytest_collection_modifyitems(session, config, items):
plugin = config.pluginmanager.getplugin('mypy')
items.append(
plugin.MypyFileItem(py.path.local('good.py'), session),
)
''')
name = 'empty'
testdir.mkdir(name)
result = testdir.runpytest_subprocess('--mypy', *xdist_args, name)
assert result.ret != 0
def test_api_error_formatter(testdir, xdist_args):
"""Ensure that the plugin can be configured in a conftest.py."""
testdir.makepyfile(bad='''
def pyfunc(x: int) -> str:
return x * 2
''')
testdir.makepyfile(conftest='''
def custom_file_error_formatter(item, results, errors):
return '\\n'.join(
'{path}:{error}'.format(
path=item.fspath,
error=error,
)
for error in errors
)
def pytest_configure(config):
plugin = config.pluginmanager.getplugin('mypy')
plugin.file_error_formatter = custom_file_error_formatter
''')
result = testdir.runpytest_subprocess('--mypy', *xdist_args)
result.stdout.fnmatch_lines([
'*/bad.py:2: error: Incompatible return value*',
])
assert result.ret != 0