@@ -79,17 +79,25 @@ def pytest_collect_file(path, parent):
79
79
parent .config .option .mypy ,
80
80
parent .config .option .mypy_ignore_missing_imports ,
81
81
]):
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 )
90
83
return None
91
84
92
85
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
+
93
101
@pytest .hookimpl (hookwrapper = True , trylast = True )
94
102
def pytest_collection_modifyitems (session , config , items ):
95
103
"""
@@ -105,7 +113,9 @@ def pytest_collection_modifyitems(session, config, items):
105
113
"""
106
114
yield
107
115
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
+ )
109
119
110
120
111
121
class MypyItem (pytest .Item ):
@@ -118,6 +128,12 @@ def __init__(self, *args, **kwargs):
118
128
super ().__init__ (* args , ** kwargs )
119
129
self .add_marker (self .MARKER )
120
130
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
+
121
137
def repr_failure (self , excinfo ):
122
138
"""
123
139
Unwrap mypy errors so we get a clean error message without the
@@ -128,9 +144,9 @@ def repr_failure(self, excinfo):
128
144
return super ().repr_failure (excinfo )
129
145
130
146
131
- class MypyFileItem (MypyItem , pytest . File ):
147
+ class MypyFileItem (MypyItem ):
132
148
133
- """A File that Mypy Runs On ."""
149
+ """A check for Mypy errors in a File ."""
134
150
135
151
def runtest (self ):
136
152
"""Raise an exception if mypy found errors for this item."""
0 commit comments