Test your types by running typecheckers on them.
Project description
pytest-typing: Test Your Types
Ensure your types do what you think they do.
pytest-typing lets you test the type signatures of your code to ensure they really are what you expect. You write Markdown files with Python code blocks and expected type assertions; pytest-typing runs type checkers on these blocks and compares the actual and expected outputs.
First, install the plugin, making it discoverable by pytest:
$ pip install pytest-typing
$ # or
$ uv add pytest-typing --group dev
Then, configure pytest-typing with the type checker (or checkers) you want to use in pyproject.toml:
[tool.pytest]
typing_checkers = ["mypy"]
The currently supported type checkers are Mypy, ty and Pyright.
Finally, start writing Markdown files in your test directory (tests/ by default).
The files need to be named test_typing_*.md in order to be collected.
Inside the Markdown file, use Markdown headings (#, ##, etc.) to separate tests.
Inside each heading, write fenced Python code blocks (python or py) with code that will be type-checked.
This is what an example file might look like, defining two tests:
# This is a test file
This is free-form text that can explain the test but is ignored by the plugin.
## This is a happy-path test
This code snippet should type-check without problems.
```python
a: int = 1
```
## This is a sad-path test
This test demonstrates how errors can be checked using error codes.
Note that error codes are specific to individual type checkers.
```python
a: str = 1 # error: [invalid-assignment]
```
In addition to matching on error codes, we can additionally match the exact error messages too.
a: str = 1 # error: [invalid-assignment] Object of type `Literal[1]` is not assignable to `str`
Although this may be fragile since checkers can improve their error messages from time to time.
Comments may be placed above lines that are expected to produce errors.
# error: [invalid-assignment]
a: str = 1
reveal_type statements can be matched against.
reveal_type(1) # revealed: Literal[1]
Or, again, on the line above.
# revealed: Literal[1]
reveal_type(1)
A section may contain multiple code blocks; this is useful for inserting longer pieces of prose in between blocks. All blocks within a section get concatenated.
# This test contains multiple blocks
```python
a: int = 1
```
Here's some prose explaining what's going on, and then a second block:
```python
b: str = 1 # error: [invalid-assignment]
```
Testing on multiple checkers
When the typing_checkers field is configured with multiple checkers, tests will be run on all of them.
Difference checkers usually have different error codes, so different error comments should be used.
a: str = 1 # ty-error: [invalid-assignment] # mypy-error: [assignment] # pyright-error: [reportAssignmentType]
A code block can be configured to run only on a specific checker:
```python only=mypy
a: int = 1
```
Or configured to skip only a specific checker:
```python skip=mypy
a: int = 1
```
pytest-typing is inspired by Astral's mdtest framework.
Project Information
License
pytest-typing is written by Tin Tvrtković and distributed under the terms of the MIT license.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_typing-26.2.0.tar.gz.
File metadata
- Download URL: pytest_typing-26.2.0.tar.gz
- Upload date:
- Size: 63.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ef070ee6f970bd891fdc42566525bbc9fbc3b7adae51a7385c5abbd25761501
|
|
| MD5 |
b7a955191f2cb5fb4ffc1c27a60bdca3
|
|
| BLAKE2b-256 |
193af9fb40058a783b1e1c14966d5d6e924fdace33c19afe69b978f4abb9e0fb
|
Provenance
The following attestation bundles were made for pytest_typing-26.2.0.tar.gz:
Publisher:
pypi-package.yml on Tinche/pytest-typing
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_typing-26.2.0.tar.gz -
Subject digest:
0ef070ee6f970bd891fdc42566525bbc9fbc3b7adae51a7385c5abbd25761501 - Sigstore transparency entry: 1145216240
- Sigstore integration time:
-
Permalink:
Tinche/pytest-typing@9c57b6fe8ab9bf62caead79d69d1909e86536c0a -
Branch / Tag:
refs/tags/v26.2.0 - Owner: https://github.com/Tinche
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-package.yml@9c57b6fe8ab9bf62caead79d69d1909e86536c0a -
Trigger Event:
release
-
Statement type:
File details
Details for the file pytest_typing-26.2.0-py3-none-any.whl.
File metadata
- Download URL: pytest_typing-26.2.0-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ae0dcd05c38c25aea582647b5b7c390c162206e64e6c3a25bd41613db4f5478
|
|
| MD5 |
950561c7dc8e5867356c3c269be5ed7a
|
|
| BLAKE2b-256 |
dc00a54edef55f25f6db3f2e0b1bd682a8489a492b03e8b200f5931bf306b86c
|
Provenance
The following attestation bundles were made for pytest_typing-26.2.0-py3-none-any.whl:
Publisher:
pypi-package.yml on Tinche/pytest-typing
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_typing-26.2.0-py3-none-any.whl -
Subject digest:
4ae0dcd05c38c25aea582647b5b7c390c162206e64e6c3a25bd41613db4f5478 - Sigstore transparency entry: 1145216259
- Sigstore integration time:
-
Permalink:
Tinche/pytest-typing@9c57b6fe8ab9bf62caead79d69d1909e86536c0a -
Branch / Tag:
refs/tags/v26.2.0 - Owner: https://github.com/Tinche
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-package.yml@9c57b6fe8ab9bf62caead79d69d1909e86536c0a -
Trigger Event:
release
-
Statement type: