pytest-mypy-plugins
pytest-mypy-plugins is a pytest plugin designed for writing tests for mypy plugins, stubs, and type definitions. It allows developers to verify the correctness of their type annotations and mypy plugin behavior directly within their pytest test suite. The library is actively maintained, with releases as needed, and the current version is 4.0.1.
Common errors
-
No errors reported for obviously wrong code
cause Mypy, by default, might not type-check functions without any type annotations, or it might infer `Any` types due to missing stubs or the `--ignore-missing-imports` flag, silently passing errors.fixEnsure all relevant functions have type annotations. For wider checks, configure mypy to use `--check-untyped-defs`. If `Any` is the issue, investigate missing library stubs or remove `--ignore-missing-imports` to expose hidden import errors. -
Mypy errors are not causing pytest to fail as expected, even with `--mypy` flag.
cause This can happen if `mypy`'s configuration (e.g., in `mypy.ini` or `pyproject.toml`) contains settings like `ignore_errors = True` or `warn_unreachable = False`, which suppress error reporting. Or, if `--ignore-missing-imports` is broadly used.fixReview your `mypy` configuration (e.g., `mypy.ini` or `pyproject.toml`) to ensure error reporting is not suppressed. Remove `ignore_errors = True` or similar options that prevent mypy from failing. Consider removing or narrowing the scope of `--ignore-missing-imports`. -
Cannot import module X when using relative paths in MYPYPATH/PYTHONPATH within pytest-mypy-plugins tests.
cause When `mypy` runs as a subprocess (the default behavior for `pytest-mypy-plugins`), its working directory might differ from where `pytest` was invoked. This causes relative paths in environment variables to resolve incorrectly.fixTo resolve this, either configure `pytest-mypy-plugins` to run `mypy` in the same process using the `--mypy-same-process` pytest CLI option, or ensure that any paths specified in `MYPYPATH` or `PYTHONPATH` are absolute paths (e.g., using `$(pwd)/my_plugin` in a shell script).
Warnings
- breaking Errors are now ignored in site-packages by default starting from version 4.0.0.
- breaking Python 3.8 support was dropped in versions 3.2.0 and 3.3.0.
- breaking Python 3.7 support was dropped starting from version 3.0.0.
- gotcha Relative paths in `PYTHONPATH` or `MYPYPATH` environment variables may not work as expected when `mypy` runs in a subprocess (the default mode for the plugin).
Install
-
pip install pytest-mypy-plugins
Imports
- pytest-mypy-plugins
No explicit import usually needed, automatically discovered by pytest.
Quickstart
# Save as my_module.py
def add(a: int, b: int) -> int:
return a + b
# Save as test_types.yml in a directory like 'typesafety/'
- case: correct_addition
main: |
from my_module import add
result = add(1, 2)
reveal_type(result) # N: Revealed type is 'builtins.int'
- case: incorrect_addition_type
main: |
from my_module import add
add(1, '2') # E: Argument 2 to "add" has incompatible type "str"; expected "int" [arg-type]
# To run tests, simply execute pytest in your terminal from the project root:
# pytest