Registry / testing / pytest-mypy

pytest-mypy

JSON →
library1.0.1pypypi✓ verified 24d ago

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-mypy
INSTALL
IMPORT
SIG · PYTEST-MYPY
P
pytest-mypy
testingpythonv1.0.1
Install
5.0s avg
Import
Disk
94MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 96.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 5.0s · import 0.000s · 94MB
94MB installed
● package 94MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pytest-mypy
This is a pytest plugin and is automatically discovered upon installation; no explicit import statement is typically needed in your test files.
The plugin is activated via the '--mypy' CLI flag or configured through conftest.py.

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.

# my_project/my_module.py def add_numbers(a: int, b: int) -> int: return a + b def incorrect_call(): # Mypy should flag this type mismatch return add_numbers('hello', 5) # my_project/test_example.py (empty or with other tests) # This file just needs to exist for pytest to discover the project. # To run mypy with pytest (from my_project directory): # pip install pytest pytest-mypy mypy # pytest --mypy my_module.py # Example conftest.py to add extra mypy arguments (e.g., --strict): # # my_project/conftest.py # def pytest_configure(config): # plugin = config.pluginmanager.getplugin('mypy') # if plugin: # # Example: make mypy run in strict mode # plugin.mypy_argv.append('--strict') # # Example: check untyped definitions # plugin.mypy_argv.append('--disallow-untyped-defs')
Debug
Known issues
gotchaMypy configuration flags like `--ignore-missing-imports` or not using strict checking (`--strict`, `--disallow-untyped-defs`) can cause Mypy to silently miss type errors. Users might mistakenly believe their code is fully type-checked when issues are being suppressed.
fix
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.
affects: All versions
gotchaWhen Mypy is run as a subprocess (the default behavior of `pytest-mypy`), relative paths specified in environment variables like `MYPYPATH` or `PYTHONPATH` may not resolve correctly because pytest tests can run in temporary working directories.
fix
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.
affects: All versions
gotchaBy default, Mypy does not fully check functions without type annotations, and `Any` types can propagate through your code, masking potential type errors. This can lead to false negatives in your type checking results.
fix
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.
affects: All versions
Upgrade
Version history
1.0.1latest on PyPI · released Apr 2, 2025
Audit
Dependencies
pytestrequiredRequired for pytest plugin functionality.
mypyrequiredThe static type checker that this plugin integrates.
Agent activity
18 hits · last 30 days
node
16
Resources
pytest-mypy — pip install pytest-mypy · libregistry