Package: pytest-mypy-testing
pytest-mypy-testing — Plugin to test mypy output with pytestpytest-mypy-testingprovides apytestplugin to test thatmypyproduces a given output. As mypy can be
told todisplay the type of an
expressionthis allows us to check mypys type interference.Installationpython-mpipinstallpytest-mypy-testingThe Python distribution package contains anentry
pointso 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 CasesA mypy test case is a top-level functions decorated with@pytest.mark.mypy_testingin a file named*.mypy-testingor in a
pytest test module.pytest-mypy-testingfollows the pytest logic in
identifying test modules and respects thepython_filesconfig value.Note thatpytest-mypy-testinguses the Pythonastmodule 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_testingdefmypy_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# F: <msg>- we expect a mypy fatal error message# R: <msg>- we expect a mypy note messageRevealed type is '<msg>'. This is useful to easily checkreveal_typeoutput:@pytest.mark.mypy_testingdefmypy_use_reveal_type():reveal_type(123)# N: Revealed type is 'Literal[123]?'reveal_type(456)# R: Literal[456]?mypy Error Code MatchingThe algorithm matching messages parses mypy error code both in the
output generated by mypy and in the Python comments.If both the mypy output and the Python comment contain an error code
and a full message, then the messages and the error codes must
match. The following test case expects that mypy writes out anassignmenterror code and a specific error message:@pytest.mark.mypy_testingdefmypy_test_invalid_assignment()->None:foo="abc"foo=123# E: Incompatible types in assignment (expression has type "int", variable has type "str")  [assignment]If the Python comment does not contain an error code, then the error
code written out by mypy (if any) is ignored. The following test case
expects a specific error message from mypy, but ignores the error code
produced by mypy:@pytest.mark.mypy_testingdefmypy_test_invalid_assignment()->None:foo="abc"foo=123# E: Incompatible types in assignment (expression has type "int", variable has type "str")If the Python comment specifies only an error code, then the message
written out by mypy is ignored, i.e., the following test case checks
that mypy reports anassignmenterror:@pytest.mark.mypy_testingdefmypy_test_invalid_assignment()->None:foo="abc"foo=123# E: [assignment]Skipping and Expected FailuresMypy test case functions can be decorated with@pytest.mark.skipand@pytest.mark.xfailto mark them as to-be-skipped and as
expected-to-fail, respectively. As with the@pytest.mark.mypy_testingmark, the names must match exactly as the
decorators are extracted from the ast.DevelopmentCreate and activate a Python virtual environment.Install development dependencies by callingpython -m pip install -U -r requirements.txt.Start developing.To run all tests withtox,
Python 3.7, 3.8, 3.9, 3.10, 3.11 and 3.12 must be available. You
might want to look into usingpyenv.Changelogv0.1.2 (2024-02-26)Add support for pytest 8 (no actual change, but declare support)
(#46,#47)Declare support for Python 3.12 (#50)Update GitHub actions (#48)Update development dependencies (#49)In GitHub PRs run tests with Python 3.11 and 3.12 (#50)v0.1.1Compare just mypy error codes if given and no error message is given
in the test case Python comment (#36,#43)v0.1.0Implement support for flexible matching of mypy error codes (towards#36,#41)Add support for pytest 7.2.x (#42)Add support for mypy 1.0.x (#42)Add support for Python 3.11 (#42)Drop support for pytest 6.x (#42)Drop support for mypy versions less than 0.931 (#42)v0.0.12Allow Windows drives in filename (#17,#34)Support async def tests (#30,#31)Add support for mypy 0.971 (#35,#27)Remove support for Python 3.6 (#32)Bump development dependencies (#40)v0.0.11Add support for mypy 0.960 (#25)v0.0.10Add support for pytest 7.0.x and require Python >= 3.7 (#23)Bump dependencies (#24)v0.0.9Disable soft error limit (#21)v0.0.8Normalize messages to enable support for mypy 0.902 and pytest 6.2.4 (#20)v0.0.7FixPYTEST_VERSION_INFO- by@blueyed(#8)Always pass--check-untyped-defsto mypy (#11)Respect pytest configpython_fileswhen identifying pytest test modules (#12)v0.0.6 - add pytest 5.4 supportUpdate the plugin to work with pytest 5.4 (#7)v0.0.5 - CI improvementsMake 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 withLICENSESdirectory
(#6)Bump dependencies (#6)