Pytest plugin to check mypy output.
Project description
pytest-mypy-testing — Plugin to test mypy output with pytest
pytest-mypy-testing
provides a
pytest plugin to test that
mypy produces a given output. As mypy can be
told to display the type of an
expression
this allows us to check mypys type interference.
Installation
pip install pytest-mypy-testing
The Python distribution package contains an entry
point
so that the plugin is automatically discovered by pytest. To disable
the plugin when it is installed , you can use the pytest command line
option -p no:mypy-testing
.
Writing Mypy Output Test Cases
A mypy test case is a top-level functions decorated with
@pytest.mark.mypy_testing
in a file name test_*.py
or
test_*.mypy-testing
. Note that we use the Python
ast module to parse
candidate files and do not import any file, i.e., the decorator must
be exactly named @pytest.mark.mypy_testing
.
In a test_*.py
file you may combine both regular pytest test
functions and mypy test functions. A single function can even be both.
Example: A simple mypy test case could look like this:
@pytest.mark.mypy_testing
def mypy_test_invalid_assginment():
foo = "abc"
foo = 123 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
The plugin runs mypy for every file containing at least one mypy test case. The mypy output is then compared to special Python comments in the file:
# N: <msg>
- we expect a mypy note message# W: <msg>
- we expect a mypy warning message# E: <msg>
- we expect a mypy error message# R: <msg>
- we expect a mypy note messageRevealed type is '<msg>'
. This is useful to easily checkreveal_type
output:@pytest.mark.mypy_testing def mypy_use_reveal_type(): reveal_type(123) # N: Revealed type is 'Literal[123]?' reveal_type(456) # R: Literal[456]?
Skipping and Expected Failures
Mypy test case functions can be decorated with @pytest.mark.skip
and
@pytest.mark.xfail
to mark them as to-be-skipped and as
expected-to-fail, respectively. As with the
@pytest.mark.mypy_testing
mark, the names must match exactly as the
decorators are extracted from the ast.
Development
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pytest-mypy-testing-0.0.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d20e0ee4efcd4d60414b2b022da643460ee8cb938b3c623c61947b1708c7b168 |
|
MD5 | 0491654cad452318fa39f1e02d325bc6 |
|
BLAKE2b-256 | ea250243a88656a3cf66af33cf33ba3a2f54fa534ee33832965ef05bee89cb5f |
Hashes for pytest_mypy_testing-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c706c658f7451f83cc4809ed62a2c50a7253b1047f9890e0080dc048fab69ad |
|
MD5 | 78fcb7ba66d3d987d7e5f64f970fac5c |
|
BLAKE2b-256 | 6f4faccd920909dbbe0f40301abcd923e3587c347b4d39ee87ec7df49d6a0287 |