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

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 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]?
    

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 pip install -U -r requirements.txt.
  • Start developing
  • To run all tests with tox, Python 3.6, 3.7 and 3.8 must be available. You might want to look into using pyenv.

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.0.6.tar.gz (24.9 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.0.6-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

Details for the file pytest-mypy-testing-0.0.6.tar.gz.

File metadata

  • Download URL: pytest-mypy-testing-0.0.6.tar.gz
  • Upload date:
  • Size: 24.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.23.0

File hashes

Hashes for pytest-mypy-testing-0.0.6.tar.gz
Algorithm Hash digest
SHA256 dd460904e3d37868e38f87aa23d24a70c910e5546707d5c2f90f20e5c55c5436
MD5 3a9f6bf23572c84083a924ab3086c389
BLAKE2b-256 38c624643a0b7c14fa37cd2ff36008a8a655f3da53c149d657ac1d2416784630

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytest_mypy_testing-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7f760fdbe6b0474ac3986851d3e6a22f4fb351a7bc305a9dc35522327f08994d
MD5 dbfd82fd2da16f8b1c85a4ebb94809d7
BLAKE2b-256 bbcdb9c5265b2f866514b0808f166824272ad29a2b57723bba69bffc47e8ab5a

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