Skip to content

George-Ogden/mypy-pytest

Repository files navigation

Mypy Pytest

A Mypy plugin for type checking Pytest code. (Not a Pytest plugin for running Mypy.)

Features

  • Checks your parametrizations are correct
    • You didn't mispel the argnames
    • Your test cases have the correct types
    • You didn't forget any arguments
  • Works with fixtures
    • Your fixtures have the correct types
    • All the fixture arguments are included
    • You don't have conflicting fixtures
  • Checks your mocks
    • You're mocking something that exists
    • Your mock has the correct type (or close enough)
  • Checks your marks
    • You're using a pre-defined mark
    • Your mark is registered
  • Checks for Iterator bugs
    • You're robustly testing methods that want Iterable by giving them an Iterable and not a Sequence
  • Works with whatever Pytest configuration
    • Only checks your custom test files and test names
    • Analyzes third-party Pytest plugins
  • Some limitations because Mypy didn't expect plugins this cool 😎
  • All the rest of Mypy

Install and Usage

Install with pip

pip install git+https://github.com/George-Ogden/mypy-pytest.git

Then register the plugin.

pyproject.toml:

plugins = ["mypy_pytest_plugin.plugin"]

mypy.ini:

plugins = mypy_pytest_plugin.plugin

See the Mypy docs for more info.

Limitations

The Mypy plugin system is fairly limited.

This plugin only checks marked functions. If you're using parametrized testing, that's fine as you pytest.mark.parametrize. If not, add a typed mark then mark any remaining tests you want to check.

import random
import pytest

@pytest.fixture
def random_0_to_10() -> float:
    return random.random() * 10

@pytest.mark.typed
def test_random_string_length(random_0_to_10: int) -> None: # 'test_random_string_length' requests 'random_0_to_10' with type "float", but expects type "int"
    assert 0 <= random_0_to_10 <= 10

The order of the error messages is unclear, but this isn't an issue if you're using a plugin to your editor.

If you make changes to your Pytest config, Mypy will ignore this and give you a cached result. If you want to see the newest version, clear the Mypy cache:

rm -rf .mypy_cache/

Development

I made this plugin because I couldn't find any alternatives. It is heavily biased towards the features I want, and I find it extremely useful. Use the GitHub issue tracker for bugs/feature requests.

About

A Mypy plugin for type checking Pytest code.

Resources

License

Stars

Watchers

Forks

Languages