Skip to content

Commit aa6d4cf

Browse files
authored
Fix an issue where PEP 561 .pyi stub files are ignored (#84)
* Fixed an issue where .pyi files were ignored * Skip a .py file if an identically named .pyi file already exists Skip a .py file if an identically named .pyi file already exists as pytest will complain about duplicate modules otherwise. * Removed a redundant if/else statement * Added a test for .pyi files * Implemented #84 (comment)
1 parent 4e71f55 commit aa6d4cf

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/pytest_mypy.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,15 @@ def pytest_configure_node(self, node): # xdist hook
7575

7676
def pytest_collect_file(path, parent):
7777
"""Create a MypyFileItem for every file mypy should run on."""
78-
if path.ext == '.py' and any([
78+
if path.ext in {'.py', '.pyi'} and any([
7979
parent.config.option.mypy,
8080
parent.config.option.mypy_ignore_missing_imports,
8181
]):
82-
return MypyFile.from_parent(parent=parent, fspath=path)
82+
# Do not create MypyFile instance for a .py file if a
83+
# .pyi file with the same name already exists;
84+
# pytest will complain about duplicate modules otherwise
85+
if path.ext == '.pyi' or not path.new(ext='.pyi').isfile():
86+
return MypyFile.from_parent(parent=parent, fspath=path)
8387
return None
8488

8589

tests/test_pytest_mypy.py

+30
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,36 @@ def pyfunc(x: int) -> int:
3636
assert result.ret == 0
3737

3838

39+
def test_mypy_pyi(testdir, xdist_args):
40+
"""
41+
Verify that a .py file will be skipped if
42+
a .pyi file exists with the same filename.
43+
"""
44+
# The incorrect signature below should be ignored
45+
# as the .pyi file takes priority
46+
testdir.makefile(
47+
'.py', pyfile='''
48+
def pyfunc(x: int) -> str:
49+
return x * 2
50+
'''
51+
)
52+
53+
testdir.makefile(
54+
'.pyi', pyfile='''
55+
def pyfunc(x: int) -> int: ...
56+
'''
57+
)
58+
59+
result = testdir.runpytest_subprocess(*xdist_args)
60+
result.assert_outcomes()
61+
result = testdir.runpytest_subprocess('--mypy', *xdist_args)
62+
mypy_file_checks = 1
63+
mypy_status_check = 1
64+
mypy_checks = mypy_file_checks + mypy_status_check
65+
result.assert_outcomes(passed=mypy_checks)
66+
assert result.ret == 0
67+
68+
3969
def test_mypy_error(testdir, xdist_args):
4070
"""Verify that running on a module with type errors fails."""
4171
testdir.makepyfile('''

0 commit comments

Comments
 (0)