@@ -163,27 +163,37 @@ def pytest_configure(config: pytest.Config) -> None:
163
163
if mypy_config_file :
164
164
mypy_argv .append (f"--config-file={ mypy_config_file } " )
165
165
166
-
167
- def pytest_collect_file (
168
- file_path : Path ,
169
- parent : pytest .Collector ,
170
- ) -> Optional [MypyFile ]:
171
- """Create a MypyFileItem for every file mypy should run on."""
172
- if file_path .suffix in {".py" , ".pyi" } and any (
166
+ if any (
173
167
[
174
- parent . config .option .mypy ,
175
- parent . config .option .mypy_config_file ,
176
- parent . config .option .mypy_ignore_missing_imports ,
177
- parent . config .option .mypy_no_status_check ,
178
- parent . config .option .mypy_xfail ,
168
+ config .option .mypy ,
169
+ config .option .mypy_config_file ,
170
+ config .option .mypy_ignore_missing_imports ,
171
+ config .option .mypy_no_status_check ,
172
+ config .option .mypy_xfail ,
179
173
],
180
174
):
181
- # Do not create MypyFile instance for a .py file if a
182
- # .pyi file with the same name already exists;
183
- # pytest will complain about duplicate modules otherwise
184
- if file_path .suffix == ".pyi" or not file_path .with_suffix (".pyi" ).is_file ():
185
- return MypyFile .from_parent (parent = parent , path = file_path )
186
- return None
175
+ config .pluginmanager .register (MypyCollectionPlugin ())
176
+
177
+
178
+ class MypyCollectionPlugin :
179
+ """A Pytest plugin that collects MypyFiles."""
180
+
181
+ def pytest_collect_file (
182
+ self ,
183
+ file_path : Path ,
184
+ parent : pytest .Collector ,
185
+ ) -> Optional [MypyFile ]:
186
+ """Create a MypyFileItem for every file mypy should run on."""
187
+ if file_path .suffix in {".py" , ".pyi" }:
188
+ # Do not create MypyFile instance for a .py file if a
189
+ # .pyi file with the same name already exists;
190
+ # pytest will complain about duplicate modules otherwise
191
+ if (
192
+ file_path .suffix == ".pyi"
193
+ or not file_path .with_suffix (".pyi" ).is_file ()
194
+ ):
195
+ return MypyFile .from_parent (parent = parent , path = file_path )
196
+ return None
187
197
188
198
189
199
class MypyFile (pytest .File ):
0 commit comments