Skip to main content

Pytest plugin to check mypy output

Project description

PyPI GitHub Action Status License License: MIT

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
  • # F: <msg> - we expect a mypy fatal error message
  • # R: <msg> - we expect a mypy note message Revealed type is '<msg>'. This is useful to easily check reveal_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]?
    

mypy Error Code Matching

The 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 an assignment error code and a specific error message:

@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")  [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_testing
def mypy_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 an assignment error:

@pytest.mark.mypy_testing
def mypy_test_invalid_assignment() -> None:
    foo = "abc"
    foo = 123  # E: [assignment]

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

  • Ensure that uv is available.
  • Create the development virtual environment by calling uv sync.
  • Start developing.
  • To run all tests with tox, Python 3.10, 3.11, 3.12, 3.13 and 3.14 must be available.

Changelog

v0.2.0 (2026-01-26)

  • Modernize project using ruff and uv (#65)
  • Require pytest >= 8 (#64)
  • Remove support for Python 3.7, 3.8 and 3.9. Require at least Python 3.10 (#63)

v0.1.4 (2026-01-26)

  • Remove upper limit of supported pytest version (#61, #62)
  • Add support for Python 3.13 and Python 3.14 (#60)
  • Switch to original flit build backend ([#54][p54], #58)

v0.1.3 (2024-03-05)

  • Replace usage of deprecated path argument to pytest hook pytest_collect_file() with usage of the file_path argument introduced in pytest 7 (#51, #52)

v0.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.1 (2023-02-25)

  • Compare just mypy error codes if given and no error message is given in the test case Python comment (#36, #43)

v0.1.0 (2023-02-25)

  • Implement 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.12 (2023-02-25)

  • Allow 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.11 (2022-05-30)

  • Add support for mypy 0.960 (#25)

v0.0.10 (2022-03-02)

  • Add support for pytest 7.0.x and require Python >= 3.7 (#23)
  • Bump dependencies (#24)

v0.0.9 (2021-12-27)

  • Disable soft error limit (#21)

v0.0.8 (2021-06-13)

  • Normalize messages to enable support for mypy 0.902 and pytest 6.2.4 (#20)

v0.0.7 (2020-04-25)

  • 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 (2020-03-29) - add pytest 5.4 support

  • Update the plugin to work with pytest 5.4 (#7)

v0.0.5 (2020-03-29) - 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)

v0.0.4 (2020-01-16)

v0.0.3 (2020-01-07)

v0.0.2 (2020-01-05)

v0.0.1 (2020-01-05)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_mypy_testing-0.2.0.tar.gz (106.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_mypy_testing-0.2.0-py3-none-any.whl (23.0 kB view details)

Uploaded Python 3

File details

Details for the file pytest_mypy_testing-0.2.0.tar.gz.

File metadata

  • Download URL: pytest_mypy_testing-0.2.0.tar.gz
  • Upload date:
  • Size: 106.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.5

File hashes

Hashes for pytest_mypy_testing-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ad56d3d0a806c4a38b5a65cbbf5ae569139565c50b4c1ff3da95a1a17d27a8ab
MD5 27fc63211aa64f2e8232d0dd566f729f
BLAKE2b-256 176fd083965c8c8b5095fbc7e7c16199af59d59cabd7d1a4a4641ef4507463fe

See more details on using hashes here.

File details

Details for the file pytest_mypy_testing-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_mypy_testing-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 710404fd81005827ad77cbc850e1bed6df5611d382a8142b9ede889b8efc5f1d
MD5 d66910a1449a75a6978882752dc42faf
BLAKE2b-256 238771765e9b74872352e2c97f60e18cc390b4531a332aac2df346aa6a57a170

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page