Skip to main content

mypy-typing-asserts

Adds the ability to assert types when typechecking to mypy.

assert_type[MyType[tuple[int, ...]]](myobject.attribute)

Support

mypy-typing-asserts is supported in mypy >= 0.700.

For similar functionality in pyright, use reveal_type(expression, expected_type=...). (See this discussion). (Supported in version 1.1.211+)

For similar functionality in pytype, use assert_type. (See the docs)

Installation

mypy-typing-asserts should be installed in to the same environment as your typechecker.

It can be installed by running pip install mypy-typing-asserts.

Alternatively if you're using poetry, poetry add -D mypy-typing-asserts.

Usage

To use, just call assert_type providing a type-parameter and an argument. This will assert that the type of the argument is exactly the same type as the type-parameter (type-hierarchies are not traversed) when typechecking (assuming you have enabled this functionality).

from mypy_typing_asserts import assert_type

...

assert_type[MyType](my_expression)

# `assert_type` also returns the argument (but does no runtime checking)
var = assert_type[int](my_function())

Note that you may need to hide your import and usage behind if typing.TYPE_CHECKING if the environment you're running the code in isn't the same that you typecheck in.

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from mypy_typing_asserts import assert_type

...

if TYPE_CHECKING:
    assert_type[MyType](my_expression)

Pitfalls

This plugin only gets executed for code that is being typechecked. Dependening on your configuration, yopur typechecker might be skipping function bodies (e.g. mypy will skip unannotated function bodies by default unless --check-untyped-defs is enabled).

If you're putting the assert_type calls inside a pytest test, make sure to annotate the -> None return type to avoid this!

Enabling the mypy plugin

In your mypy config, add mypy_typing_asserts.mypy_plugin to your plugins declaration.

See mypy's documentation

Alternatives

All of the alternatives today ensure types are deduced correctly by running mypy in a subprocess, which isn't always feasible or ideal. These include:

Metadata

Release files for mypy-typing-asserts 0.1.1

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

Source distribution (sdist)

Source distribution for mypy-typing-asserts 0.1.1
File Size Uploaded
mypy_typing_asserts-0.1.1.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mypy-typing-asserts 0.1.1
File Interpreter ABI Platform
mypy_typing_asserts-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.3 kB

Release files / mypy_typing_asserts-0.1.1.tar.gz

Download URL mypy_typing_asserts-0.1.1.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a93087a4242b660ae567463f93cab2673304bd0820172268f488248bf3b0fa40
BLAKE2b-256 checksum
How to use checksums
a92209ad7d73a546eee818b671f990d3d63ff18cc17b3c343dca9d4963eb228f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.8.12 Linux/5.10.0-1051-oem

Release files / mypy_typing_asserts-0.1.1-py3-none-any.whl

Download URL mypy_typing_asserts-0.1.1-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
917da6d572f187f72d6dfb4b1108c50a3d8edd8e5ccd1a0f0c576fe0a4819cda
BLAKE2b-256 checksum
How to use checksums
e00ef6f705299002e014d73036ab2f6cac4063203af28e9adcdcf60c03a7219f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.8.12 Linux/5.10.0-1051-oem

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.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