|
| 1 | +# Mypy Pytest |
| 2 | + |
| 3 | +A Mypy plugin for type checking Pytest code. |
| 4 | +(Not a Pytest plugin for running Mypy.) |
| 5 | + |
| 6 | +## Features |
| 7 | + |
| 8 | +- Check your parametrizations are correct |
| 9 | + - [x] You didn't mispel the argnames |
| 10 | + - [x] Your test cases have the right type |
| 11 | + - [x] You didn't forget any arguments |
| 12 | +- Works with fixtures |
| 13 | + - [x] Your fixtures have the correct types |
| 14 | + - [x] All the fixture arguments are included |
| 15 | + - [x] You don't have conflicting fixtures |
| 16 | +- Checks your mocks |
| 17 | + - [x] Your mock has the correct type (or close enough) |
| 18 | +- Checks your marks |
| 19 | + - [x] You're using a pre-defined mark |
| 20 | + - [x] Your mark is registered |
| 21 | +- Checks for `Iterator` bugs |
| 22 | + - [x] You're robustly testing methods that want `Iterable` by giving them an `Iterable` and not a `Sequence` |
| 23 | +- Works with whatever Pytest configuration |
| 24 | + - [x] Only checks your custom test files and test names |
| 25 | + - [x] Analyzes third-party Pytest plugins |
| 26 | +- [x] Some [limitations](#limitations) because Mypy didn't expect plugins this cool :sunglasses: |
| 27 | +- [x] All the rest of Mypy |
| 28 | + |
| 29 | +## Install and Usage |
| 30 | + |
| 31 | +Install with pip |
| 32 | + |
| 33 | +```bash |
| 34 | +pip install git+https://github.com/George-Ogden/dbg.git |
| 35 | +``` |
| 36 | + |
| 37 | +Then register the plugin. |
| 38 | + |
| 39 | +`pyproject.toml`: |
| 40 | + |
| 41 | +```toml |
| 42 | +plugins = ["mypy_pytest_plugin.plugin"] |
| 43 | +``` |
| 44 | + |
| 45 | +`mypy.ini`: |
| 46 | + |
| 47 | +```toml |
| 48 | +plugins = mypy_pytest_plugin.plugin |
| 49 | +``` |
| 50 | + |
| 51 | +See the [Mypy docs](https://mypy.readthedocs.io/en/stable/extending_mypy.html#configuring-mypy-to-use-plugins) for more info. |
| 52 | + |
| 53 | +## Limitations |
| 54 | + |
| 55 | +The Mypy plugin system is fairly limited, so this can only check marked functions. |
| 56 | +If you're using parametrized testing, that's fine as you `pytest.mark.parametrize`. |
| 57 | +If not, [add a `typed` mark](https://docs.pytest.org/en/stable/how-to/mark.html#registering-marks) then mark any remaining tests you want to check. |
| 58 | + |
| 59 | +```python |
| 60 | +import random |
| 61 | +import pytest |
| 62 | + |
| 63 | +@pytest.fixture |
| 64 | +def random_0_to_10() -> float: |
| 65 | + return random.random() * 10 |
| 66 | + |
| 67 | +@pytest.mark.typed |
| 68 | +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" |
| 69 | + assert 0 <= random_0_to_10 <= 10 |
| 70 | +``` |
| 71 | + |
| 72 | +## Development |
| 73 | + |
| 74 | +Use the GitHub issue tracker for bugs/feature requests. |
0 commit comments