pytest-mypy is a pytest plugin that integrates the Mypy static type checker into your pytest test runs. It allows you to run Mypy checks on your Python source files as part of your existing test suite, similar to how pytest-flake8 works for Flake8. The current version is 1.0.1, and it is actively maintained, with releases as needed.
pip install pytest-mypyVerified import paths — ran on the pinned version, not inferred.
After installing `pytest-mypy`, enable it by passing the `--mypy` flag to pytest, specifying the files or directories you want to type check. For more granular control or to enforce stricter Mypy settings, you can add options to `plugin.mypy_argv` within a `pytest_configure` hook in your `conftest.py` file.
Always aim for strict Mypy settings in CI/CD pipelines (e.g., using `--strict`). Review your `mypy.ini` or `pyproject.toml` configuration to ensure no critical errors are being ignored. Consider using `plugin.mypy_argv.append('--strict')` in `conftest.py` if configuring via pytest.To resolve pathing issues, use absolute paths for `MYPYPATH`/`PYTHONPATH` or instruct `pytest-mypy` to run Mypy in the same process using the `--mypy-same-process` flag.
To ensure comprehensive type checking, explicitly add type annotations to your functions. Alternatively, configure Mypy with flags like `--disallow-untyped-defs` (to report errors for untyped function definitions) or use `--strict` mode, which enables a comprehensive set of strict checks.