Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create MypyCollectionPlugin #184

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions src/pytest_mypy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,27 +163,37 @@ def pytest_configure(config: pytest.Config) -> None:
if mypy_config_file:
mypy_argv.append(f"--config-file={mypy_config_file}")


def pytest_collect_file(
file_path: Path,
parent: pytest.Collector,
) -> Optional[MypyFile]:
"""Create a MypyFileItem for every file mypy should run on."""
if file_path.suffix in {".py", ".pyi"} and any(
if any(
[
parent.config.option.mypy,
parent.config.option.mypy_config_file,
parent.config.option.mypy_ignore_missing_imports,
parent.config.option.mypy_no_status_check,
parent.config.option.mypy_xfail,
config.option.mypy,
config.option.mypy_config_file,
config.option.mypy_ignore_missing_imports,
config.option.mypy_no_status_check,
config.option.mypy_xfail,
],
):
# Do not create MypyFile instance for a .py file if a
# .pyi file with the same name already exists;
# pytest will complain about duplicate modules otherwise
if file_path.suffix == ".pyi" or not file_path.with_suffix(".pyi").is_file():
return MypyFile.from_parent(parent=parent, path=file_path)
return None
config.pluginmanager.register(MypyCollectionPlugin())


class MypyCollectionPlugin:
"""A Pytest plugin that collects MypyFiles."""

def pytest_collect_file(
self,
file_path: Path,
parent: pytest.Collector,
) -> Optional[MypyFile]:
"""Create a MypyFileItem for every file mypy should run on."""
if file_path.suffix in {".py", ".pyi"}:
# Do not create MypyFile instance for a .py file if a
# .pyi file with the same name already exists;
# pytest will complain about duplicate modules otherwise
if (
file_path.suffix == ".pyi"
or not file_path.with_suffix(".pyi").is_file()
):
return MypyFile.from_parent(parent=parent, path=file_path)
return None


class MypyFile(pytest.File):
Expand Down
Loading