forked from realpython/pytest-mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pytest_mypy.py
563 lines (495 loc) · 17.2 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import signal
import sys
import textwrap
import mypy.version
from packaging.version import Version
import pytest
import pytest_mypy
MYPY_VERSION = Version(mypy.version.__version__)
PYTHON_VERSION = Version(
".".join(
str(token)
for token in [
sys.version_info.major,
sys.version_info.minor,
sys.version_info.micro,
]
)
)
@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_{0}".format(
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()
assert result.ret == pytest.ExitCode.NO_TESTS_COLLECTED
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 == pytest.ExitCode.OK
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.makepyfile(
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 == pytest.ExitCode.OK
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()
assert "_mypy_results_path" not in result.stderr.str()
assert result.ret == pytest.ExitCode.NO_TESTS_COLLECTED
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 "_mypy_results_path" not in result.stderr.str()
assert result.ret == pytest.ExitCode.TESTS_FAILED
def test_mypy_annotation_unchecked(testdir, xdist_args, tmp_path, monkeypatch):
"""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}
result.assert_outcomes(**outcomes)
result.stdout.fnmatch_lines(["*MypyWarning*"])
assert result.ret == pytest.ExitCode.OK
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 == pytest.ExitCode.TESTS_FAILED
result = testdir.runpytest_subprocess("--mypy-ignore-missing-imports", *xdist_args)
result.assert_outcomes(passed=mypy_checks)
assert result.ret == pytest.ExitCode.OK
def test_mypy_config_file(testdir, xdist_args):
"""Verify that --mypy-config-file works."""
testdir.makepyfile(
"""
def pyfunc(x):
return x * 2
""",
)
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 == pytest.ExitCode.OK
mypy_config_file = testdir.makeini(
"""
[mypy]
disallow_untyped_defs = True
""",
)
result = testdir.runpytest_subprocess(
"--mypy-config-file",
mypy_config_file,
*xdist_args,
)
result.assert_outcomes(failed=mypy_checks)
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 == pytest.ExitCode.TESTS_FAILED
result = testdir.runpytest_subprocess("--mypy", "-m", "mypy", *xdist_args)
result.assert_outcomes(passed=mypy_checks)
assert result.ret == pytest.ExitCode.OK
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()
assert result.ret == pytest.ExitCode.NO_TESTS_COLLECTED
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 == pytest.ExitCode.TESTS_FAILED
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 == pytest.ExitCode.OK
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 == pytest.ExitCode.OK
@pytest.mark.xfail(
Version("0.971") <= MYPY_VERSION,
raises=AssertionError,
reason="https://github.com/python/mypy/issues/13701",
)
@pytest.mark.parametrize(
"module_name",
[
"__init__",
"good",
],
)
def test_mypy_indirect(testdir, xdist_args, module_name):
"""Verify that uncollected files checked by mypy cause a failure."""
testdir.makepyfile(
bad="""
def pyfunc(x: int) -> str:
return x * 2
""",
)
pyfile = testdir.makepyfile(
**{
module_name: """
import bad
""",
},
)
result = testdir.runpytest_subprocess("--mypy", *xdist_args, str(pyfile))
mypy_file_checks = 1
mypy_status_check = 1
result.assert_outcomes(passed=mypy_file_checks, failed=mypy_status_check)
assert result.ret == pytest.ExitCode.TESTS_FAILED
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 == pytest.ExitCode.TESTS_FAILED
def test_pyproject_toml(testdir, xdist_args):
"""Ensure that the plugin allows configuration with pyproject.toml."""
testdir.makefile(
".toml",
pyproject="""
[tool.mypy]
disallow_untyped_defs = true
""",
)
testdir.makepyfile(
conftest="""
def pyfunc(x):
return x * 2
""",
)
result = testdir.runpytest_subprocess("--mypy", *xdist_args)
result.stdout.fnmatch_lines(["1: error: Function is missing a type annotation*"])
assert result.ret == pytest.ExitCode.TESTS_FAILED
def test_setup_cfg(testdir, xdist_args):
"""Ensure that the plugin allows configuration with setup.cfg."""
testdir.makefile(
".cfg",
setup="""
[mypy]
disallow_untyped_defs = True
""",
)
testdir.makepyfile(
conftest="""
def pyfunc(x):
return x * 2
""",
)
result = testdir.runpytest_subprocess("--mypy", *xdist_args)
result.stdout.fnmatch_lines(["1: error: Function is missing a type annotation*"])
assert result.ret == pytest.ExitCode.TESTS_FAILED
@pytest.mark.parametrize("module_name", ["__init__", "test_demo"])
def test_looponfail(testdir, module_name):
"""Ensure that the plugin works with --looponfail."""
pass_source = textwrap.dedent(
"""\
def pyfunc(x: int) -> int:
return x * 2
""",
)
fail_source = textwrap.dedent(
"""\
def pyfunc(x: int) -> str:
return x * 2
""",
)
pyfile = testdir.makepyfile(**{module_name: fail_source})
looponfailroot = testdir.mkdir("looponfailroot")
looponfailroot_pyfile = looponfailroot.join(pyfile.basename)
pyfile.move(looponfailroot_pyfile)
pyfile = looponfailroot_pyfile
testdir.makeini(
textwrap.dedent(
"""\
[pytest]
looponfailroots = {looponfailroots}
""".format(
looponfailroots=looponfailroot,
),
),
)
child = testdir.spawn_pytest(
"--mypy --looponfail " + str(pyfile),
expect_timeout=60.0,
)
def _expect_session():
child.expect("==== test session starts ====")
def _expect_failure():
_expect_session()
child.expect("==== FAILURES ====")
child.expect(pyfile.basename + " ____")
child.expect("2: error: Incompatible return value")
child.expect("==== mypy ====")
child.expect("Found 1 error in 1 file (checked 1 source file)")
child.expect("2 failed")
child.expect("#### LOOPONFAILING ####")
_expect_waiting()
def _expect_waiting():
child.expect("#### waiting for changes ####")
child.expect("Watching")
def _fix():
pyfile.write(pass_source)
_expect_changed()
_expect_success()
def _expect_changed():
child.expect("MODIFIED " + str(pyfile))
def _expect_success():
for _ in range(2):
_expect_session()
child.expect("==== mypy ====")
child.expect("Success: no issues found in 1 source file")
child.expect("2 passed")
_expect_waiting()
def _break():
pyfile.write(fail_source)
_expect_changed()
_expect_failure()
_expect_failure()
_fix()
_break()
_fix()
child.kill(signal.SIGTERM)
def test_mypy_results_from_mypy_with_opts():
"""MypyResults.from_mypy respects passed options."""
mypy_results = pytest_mypy.MypyResults.from_mypy([], opts=["--version"])
assert mypy_results.status == 0
assert mypy_results.abspath_errors == {}
assert str(MYPY_VERSION) in mypy_results.stdout
def test_mypy_no_output(testdir, xdist_args):
"""No terminal summary is shown if there is no output from mypy."""
testdir.makepyfile(
# Mypy prints a success message to stderr by default:
# "Success: no issues found in 1 source file"
# Clear stderr and unmatched_stdout to simulate mypy having no output:
conftest="""
import pytest
@pytest.hookimpl(trylast=True)
def pytest_configure(config):
pytest_mypy = config.pluginmanager.getplugin("mypy")
mypy_config_stash = config.stash[pytest_mypy.stash_key["config"]]
with open(mypy_config_stash.mypy_results_path, mode="w") as results_f:
pytest_mypy.MypyResults(
opts=[],
stdout="",
stderr="",
status=0,
abspath_errors={},
unmatched_stdout="",
).dump(results_f)
""",
)
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 == pytest.ExitCode.OK
assert f"= {pytest_mypy.terminal_summary_title} =" not in str(result.stdout)
def test_py_typed(testdir):
"""Mypy recognizes that pytest_mypy is typed."""
name = "typed"
testdir.makepyfile(**{name: "import pytest_mypy"})
result = testdir.run("mypy", f"{name}.py")
assert result.ret == 0
def test_mypy_no_status_check(testdir, xdist_args):
"""Verify that --mypy-no-status-check disables MypyStatusItem collection."""
testdir.makepyfile(thon="one: int = 1")
result = testdir.runpytest_subprocess("--mypy", *xdist_args)
mypy_file_checks = 1
mypy_status_check = 1
result.assert_outcomes(passed=mypy_file_checks + mypy_status_check)
assert result.ret == pytest.ExitCode.OK
result = testdir.runpytest_subprocess("--mypy-no-status-check", *xdist_args)
result.assert_outcomes(passed=mypy_file_checks)
assert result.ret == pytest.ExitCode.OK