Skip to content

Commit 2120ff1

Browse files
committed
Split MypyFileItem out of MypyItem
1 parent 0791070 commit 2120ff1

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

src/pytest_mypy.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ def pytest_configure_node(self, node): # xdist hook
7373

7474

7575
def pytest_collect_file(path, parent):
76-
"""Create a MypyItem for every file mypy should run on."""
76+
"""Create a MypyFileItem for every file mypy should run on."""
7777
if path.ext == '.py' and any([
7878
parent.config.option.mypy,
7979
parent.config.option.mypy_ignore_missing_imports,
8080
]):
81-
item = MypyItem(path, parent)
81+
item = MypyFileItem(path, parent)
8282
if nodeid_name:
83-
item = MypyItem(
83+
item = MypyFileItem(
8484
path,
8585
parent,
8686
nodeid='::'.join([item.nodeid, nodeid_name]),
@@ -89,16 +89,30 @@ def pytest_collect_file(path, parent):
8989
return None
9090

9191

92-
class MypyItem(pytest.Item, pytest.File):
92+
class MypyItem(pytest.Item):
9393

94-
"""A File that Mypy Runs On."""
94+
"""A Mypy-related test Item."""
9595

9696
MARKER = 'mypy'
9797

9898
def __init__(self, *args, **kwargs):
9999
super().__init__(*args, **kwargs)
100100
self.add_marker(self.MARKER)
101101

102+
def repr_failure(self, excinfo):
103+
"""
104+
Unwrap mypy errors so we get a clean error message without the
105+
full exception repr.
106+
"""
107+
if excinfo.errisinstance(MypyError):
108+
return excinfo.value.args[0]
109+
return super().repr_failure(excinfo)
110+
111+
112+
class MypyFileItem(MypyItem, pytest.File):
113+
114+
"""A File that Mypy Runs On."""
115+
102116
def runtest(self):
103117
"""Raise an exception if mypy found errors for this item."""
104118
results = _cached_json_results(
@@ -112,7 +126,7 @@ def runtest(self):
112126
abspaths=[
113127
os.path.abspath(str(item.fspath))
114128
for item in self.session.items
115-
if isinstance(item, MypyItem)
129+
if isinstance(item, MypyFileItem)
116130
],
117131
)
118132
)
@@ -129,15 +143,6 @@ def reportinfo(self):
129143
self.config.invocation_dir.bestrelpath(self.fspath),
130144
)
131145

132-
def repr_failure(self, excinfo):
133-
"""
134-
Unwrap mypy errors so we get a clean error message without the
135-
full exception repr.
136-
"""
137-
if excinfo.errisinstance(MypyError):
138-
return excinfo.value.args[0]
139-
return super().repr_failure(excinfo)
140-
141146

142147
def _cached_json_results(results_path, results_factory=None):
143148
"""

tests/test_pytest_mypy.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ def test_non_mypy_error(testdir, xdist_args):
9898
def pytest_configure(config):
9999
plugin = config.pluginmanager.getplugin('mypy')
100100
101-
class PatchedMypyItem(plugin.MypyItem):
101+
class PatchedMypyFileItem(plugin.MypyFileItem):
102102
def runtest(self):
103103
raise Exception('{message}')
104104
105-
plugin.MypyItem = PatchedMypyItem
105+
plugin.MypyFileItem = PatchedMypyFileItem
106106
'''.format(message=message))
107107
result = testdir.runpytest_subprocess(*xdist_args)
108108
result.assert_outcomes()
@@ -178,7 +178,7 @@ def pytest_collection_modifyitems(session, config, items):
178178
for mypy_item_i in reversed([
179179
i
180180
for i, item in enumerate(items)
181-
if isinstance(item, plugin.MypyItem)
181+
if isinstance(item, plugin.MypyFileItem)
182182
]):
183183
items.pop(mypy_item_i)
184184
''')

0 commit comments

Comments
 (0)