Skip to content

Commit 4cd6b5d

Browse files
authored
Merge pull request #184 from dmtucker/collection-plugin
Create MypyCollectionPlugin
2 parents 8ad390a + 85c9024 commit 4cd6b5d

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

src/pytest_mypy/__init__.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,27 +163,37 @@ def pytest_configure(config: pytest.Config) -> None:
163163
if mypy_config_file:
164164
mypy_argv.append(f"--config-file={mypy_config_file}")
165165

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(
173167
[
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,
179173
],
180174
):
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
187197

188198

189199
class MypyFile(pytest.File):

0 commit comments

Comments
 (0)