pytest-mypy-plugins

4.0.1 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

Create a Python file (`my_module.py`) with code to be type-checked, and a YAML file (`test_types.yml`) defining test cases. Each `case` block in the YAML file contains `main` (Python code) and uses `# N:` for expected `reveal_type` output or `# E:` for expected mypy error messages. The plugin automatically discovers and runs these tests when `pytest` is executed.

# 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

view raw JSON →