Skip to main content

Adds the ability to _assert_ types when typechecking to mypy

Project description

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 pyright support, use reveal_type(expression, expected_type=...). (See this discussion). (Supported in version 1.1.211+)

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:

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

mypy_typing_asserts-0.1.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

mypy_typing_asserts-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file mypy_typing_asserts-0.1.0.tar.gz.

File metadata

  • Download URL: mypy_typing_asserts-0.1.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.8.12 Linux/5.10.0-1051-oem

File hashes

Hashes for mypy_typing_asserts-0.1.0.tar.gz
Algorithm Hash digest
SHA256 597c82c595bbdc1e14accf4dbd30e9af3e3cf51aa34dcb70391d5773e4720b40
MD5 6cb0a8fe69342c13ffa9f0806075f76d
BLAKE2b-256 63433f6fef8642ccc4df30ccd09915d6688fc95c7101dee30e3b8b8bef76926b

See more details on using hashes here.

File details

Details for the file mypy_typing_asserts-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mypy_typing_asserts-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fb6eaa45bfaeed2c53de18be23d27e57121a59dadc2d51a39c26df434e80d35c
MD5 84150123356d969312bd778b71a11599
BLAKE2b-256 b1fd1e12fc42a0a0b85db5aa920f73577bc4c1ecd0864c6b7d5a8a02aaf8fc87

See more details on using hashes here.

Supported by

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