Skip to main content

pytest-typing: Test Your Types

Ensure your types do what you think they do.

License: MIT PyPI Supported Python Versions Coverage


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, Pyrefly, 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]

Or, when more checkers are in use, stacked:

# ty-error: [invalid-assignment]
# mypy-error: [assignment]
# pyrefly-error: [bad-assignment]
# pyright-error: [reportAssignmentType]
a: str = 1

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
```

Pyrefly presets

pytest-typing runs Pyrefly with --preset default so temporary test snippets receive full type checking. Without an explicit preset, recent Pyrefly versions fall back to minimal checking for these unconfigured snippets and can miss assignment errors.

Select a different Pyrefly preset in your pytest configuration:

[tool.pytest]
typing_checkers = ["pyrefly"]
typing_pyrefly_preset = "strict"

Or override it for a single run:

pytest --typing-pyrefly-preset=strict

The command-line option takes precedence over configuration and only affects Pyrefly. Preset names are validated by your installed Pyrefly version. Use a version supporting --preset, or set typing_pyrefly_preset = "" to omit the flag and retain Pyrefly's own configuration selection.

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.

Metadata

Release files for pytest-typing 26.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-typing 26.5.0
File Size Uploaded
pytest_typing-26.5.0.tar.gz 66.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-typing 26.5.0
File Interpreter ABI Platform
pytest_typing-26.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 84.5 kB

Release files / pytest_typing-26.5.0.tar.gz

Download URL pytest_typing-26.5.0.tar.gz
Size 66.5 kB
Tags Source
SHA-256 checksum
How to use checksums
102858c2a93b9d077996e259f399eebb3458fd019c474f936847517f0e0b2523
BLAKE2b-256 checksum
How to use checksums
ce4088fdd6dd1c4c2dd632b6b6b278160d411a396c720f4c9e4e7d17d93f7633
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 13, 2026.

Transparency log

Release files / pytest_typing-26.5.0-py3-none-any.whl

Download URL pytest_typing-26.5.0-py3-none-any.whl
Size 18.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
96ad768a7aa7ee71a8ec6c85c3c330101f6be4bc091f400e54c98d9a6b46364f
BLAKE2b-256 checksum
How to use checksums
6b1926b8f3a4f0c67dc95041fb1475e761f47974e88c800d410fa21bec6bf5bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 13, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

26.5.0 This release

2 release files

26.4.1

2 release files

26.3.0

2 release files

26.2.0

2 release files

26.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page