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
python -m 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 named *.mypy-testing
or in a
pytest test module. pytest-mypy-testing
follows the pytest logic in
identifying test modules and respects the
python_files
config value.
Note that pytest-mypy-testing
uses the Python
ast module to parse
candidate files and does not import any file, i.e., the decorator must
be exactly named @pytest.mark.mypy_testing
.
In a pytest test module file you may combine both regular pytest test functions and mypy test functions. A single function can be both.
Example: A simple mypy test case could look like this:
@pytest.mark.mypy_testing
def mypy_test_invalid_assignment() -> None:
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
- Create and activate a Python virtual environment.
- Install development dependencies by calling
python -m pip install -U -r requirements.txt
. - Start developing.
- To run all tests with tox, Python 3.7, 3.8, 3.9 and 3.10 must be available. You might want to look into using pyenv.
Changelog
v0.0.10
- Add support for pytest 7.0.x and require Python >= 3.7 (#23)
- Bump dependencies (#24)
v0.0.9
- Disable soft error limit (#21)
v0.0.8
- Normalize messages to enable support for mypy 0.902 and pytest 6.2.4 (#20)
v0.0.7
- Fix
PYTEST_VERSION_INFO
- by @blueyed (#8) - Always pass
--check-untyped-defs
to mypy (#11) - Respect pytest config
python_files
when identifying pytest test modules (#12)
v0.0.6 - add pytest 5.4 support
- Update the plugin to work with pytest 5.4 (#7)
v0.0.5 - CI improvements
- Make invoke tasks work (partially) on Windows (#6)
- Add an invoke task to run tox environments by selecting globs (e.g.,
inv tox -e py-*
) (#6) - Use coverage directly for code coverage to get more consistent parallel run results (#6)
- Use flit fork dflit to make packaging work with
LICENSES
directory (#6) - Bump dependencies (#6)
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.12.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b5527d8d2c3d466a729b66d846bc5dcb87144548da9ef7ab4ad4522b8326aca |
|
MD5 | 227477d89166c6041d77d77300c6fa3f |
|
BLAKE2b-256 | 52a3111f2d11afc8dac400d4d6f7662ecd933bc5d68eebd3c51737f9ebd86942 |
Hashes for pytest_mypy_testing-0.0.12-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24cf0f86a58b8456fba5c5aa30b5922b3a109f69e3ac913a87b7142ecd71b7ba |
|
MD5 | 17ecb4daf1a4a4a6806b6dd9d62b2c0f |
|
BLAKE2b-256 | 01415f43953ac32a838fc82dd863b68ff5a0cbc1474f0e301439d27bcc090c47 |