Skip to content

Commit cd459a4

Browse files
committed
Stop injecting in pytest_collection_modifyitems
1 parent 4be684b commit cd459a4

File tree

2 files changed

+10
-84
lines changed

2 files changed

+10
-84
lines changed

src/pytest_mypy.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,16 @@ def from_parent(cls, *args, **kwargs):
125125
def collect(self):
126126
"""Create a MypyFileItem for the File."""
127127
yield MypyFileItem.from_parent(parent=self, name=nodeid_name)
128-
129-
130-
@pytest.hookimpl(hookwrapper=True, trylast=True)
131-
def pytest_collection_modifyitems(session, config, items):
132-
"""
133-
Add a MypyStatusItem if any MypyFileItems were collected.
134-
135-
Since mypy might check files that were not collected,
136-
pytest could pass even though mypy failed!
137-
To prevent that, add an explicit check for the mypy exit status.
138-
139-
This should execute as late as possible to avoid missing any
140-
MypyFileItems injected by other pytest_collection_modifyitems
141-
implementations.
142-
"""
143-
yield
144-
if any(isinstance(item, MypyFileItem) for item in items):
145-
items.append(
146-
MypyStatusItem.from_parent(parent=session, name=nodeid_name),
147-
)
128+
# Since mypy might check files that were not collected,
129+
# pytest could pass even though mypy failed!
130+
# To prevent that, add an explicit check for the mypy exit status.
131+
if not any(
132+
isinstance(item, MypyStatusItem) for item in self.session.items
133+
):
134+
yield MypyStatusItem.from_parent(
135+
parent=self,
136+
name=nodeid_name + "-status",
137+
)
148138

149139

150140
class MypyItem(pytest.Item):

tests/test_pytest_mypy.py

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -214,38 +214,6 @@ def pytest_configure(config):
214214
assert result.ret == 0
215215

216216

217-
def test_pytest_collection_modifyitems(testdir, xdist_args):
218-
"""
219-
Verify that collected files which are removed in a
220-
pytest_collection_modifyitems implementation are not
221-
checked by mypy.
222-
223-
This would also fail if a MypyStatusItem were injected
224-
despite there being no MypyFileItems.
225-
"""
226-
testdir.makepyfile(conftest='''
227-
def pytest_collection_modifyitems(session, config, items):
228-
plugin = config.pluginmanager.getplugin('mypy')
229-
for mypy_item_i in reversed([
230-
i
231-
for i, item in enumerate(items)
232-
if isinstance(item, plugin.MypyFileItem)
233-
]):
234-
items.pop(mypy_item_i)
235-
''')
236-
testdir.makepyfile('''
237-
def pyfunc(x: int) -> str:
238-
return x * 2
239-
240-
def test_pass():
241-
pass
242-
''')
243-
result = testdir.runpytest_subprocess('--mypy', *xdist_args)
244-
test_count = 1
245-
result.assert_outcomes(passed=test_count)
246-
assert result.ret == 0
247-
248-
249217
def test_mypy_indirect(testdir, xdist_args):
250218
"""Verify that uncollected files checked by mypy cause a failure."""
251219
testdir.makepyfile(bad='''
@@ -259,38 +227,6 @@ def pyfunc(x: int) -> str:
259227
assert result.ret != 0
260228

261229

262-
def test_mypy_indirect_inject(testdir, xdist_args):
263-
"""
264-
Verify that uncollected files checked by mypy because of a MypyFileItem
265-
injected in pytest_collection_modifyitems cause a failure.
266-
"""
267-
testdir.makepyfile(bad='''
268-
def pyfunc(x: int) -> str:
269-
return x * 2
270-
''')
271-
testdir.makepyfile(good='''
272-
import bad
273-
''')
274-
testdir.makepyfile(conftest='''
275-
import py
276-
import pytest
277-
278-
@pytest.hookimpl(trylast=True) # Inject as late as possible.
279-
def pytest_collection_modifyitems(session, config, items):
280-
plugin = config.pluginmanager.getplugin('mypy')
281-
items.append(
282-
plugin.MypyFileItem.from_parent(
283-
parent=session,
284-
name=str(py.path.local('good.py')),
285-
),
286-
)
287-
''')
288-
name = 'empty'
289-
testdir.mkdir(name)
290-
result = testdir.runpytest_subprocess('--mypy', *xdist_args, name)
291-
assert result.ret != 0
292-
293-
294230
def test_api_error_formatter(testdir, xdist_args):
295231
"""Ensure that the plugin can be configured in a conftest.py."""
296232
testdir.makepyfile(bad='''

0 commit comments

Comments
 (0)