Pytest Mypy Plugin

1.0.1 · active · verified Mon Apr 13

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.

Warnings

Install

Imports

Quickstart

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')

view raw JSON →