Skip to content

Commit c01cc60

Browse files
authored
Merge pull request #82 from dmtucker/pytest5.4
Use Node.from_parent to instantiate MypyItems
2 parents d4549a4 + 747bcec commit c01cc60

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

src/pytest_mypy.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,25 @@ def pytest_collect_file(path, parent):
7979
parent.config.option.mypy,
8080
parent.config.option.mypy_ignore_missing_imports,
8181
]):
82-
item = MypyFileItem(path, parent)
83-
if nodeid_name:
84-
item = MypyFileItem(
85-
path,
86-
parent,
87-
nodeid='::'.join([item.nodeid, nodeid_name]),
88-
)
89-
return item
82+
return MypyFile.from_parent(parent=parent, fspath=path)
9083
return None
9184

9285

86+
class MypyFile(pytest.File):
87+
88+
"""A File that Mypy will run on."""
89+
90+
@classmethod
91+
def from_parent(cls, *args, **kwargs):
92+
"""Override from_parent for compatibility."""
93+
# pytest.File.from_parent did not exist before pytest 5.4.
94+
return getattr(super(), 'from_parent', cls)(*args, **kwargs)
95+
96+
def collect(self):
97+
"""Create a MypyFileItem for the File."""
98+
yield MypyFileItem.from_parent(parent=self, name=nodeid_name)
99+
100+
93101
@pytest.hookimpl(hookwrapper=True, trylast=True)
94102
def pytest_collection_modifyitems(session, config, items):
95103
"""
@@ -105,7 +113,9 @@ def pytest_collection_modifyitems(session, config, items):
105113
"""
106114
yield
107115
if any(isinstance(item, MypyFileItem) for item in items):
108-
items.append(MypyStatusItem(nodeid_name, session, config, session))
116+
items.append(
117+
MypyStatusItem.from_parent(parent=session, name=nodeid_name),
118+
)
109119

110120

111121
class MypyItem(pytest.Item):
@@ -118,6 +128,12 @@ def __init__(self, *args, **kwargs):
118128
super().__init__(*args, **kwargs)
119129
self.add_marker(self.MARKER)
120130

131+
@classmethod
132+
def from_parent(cls, *args, **kwargs):
133+
"""Override from_parent for compatibility."""
134+
# pytest.Item.from_parent did not exist before pytest 5.4.
135+
return getattr(super(), 'from_parent', cls)(*args, **kwargs)
136+
121137
def repr_failure(self, excinfo):
122138
"""
123139
Unwrap mypy errors so we get a clean error message without the
@@ -128,9 +144,9 @@ def repr_failure(self, excinfo):
128144
return super().repr_failure(excinfo)
129145

130146

131-
class MypyFileItem(MypyItem, pytest.File):
147+
class MypyFileItem(MypyItem):
132148

133-
"""A File that Mypy Runs On."""
149+
"""A check for Mypy errors in a File."""
134150

135151
def runtest(self):
136152
"""Raise an exception if mypy found errors for this item."""

0 commit comments

Comments
 (0)