Skip to main content

Run-time type checker for Python

Project description

Build Status Code Coverage

This library provides run-time type checking for functions defined with argument type annotations.

The typing module introduced in Python 3.5 (and available on PyPI for older versions of Python 3) is supported. See below for details.

There are two principal ways to use type checking, each with its pros and cons:

  1. calling check_argument_types() from within the function body:

    debugger friendly but cannot check the type of the return value

  2. decorating the function with @typechecked:

    can check the type of the return value but adds an extra frame to the call stack for every call to a decorated function

If a function is called with incompatible argument types or a @typechecked decorated function returns a value incompatible with the declared type, a descriptive TypeError exception is raised.

Type checks can be fairly expensive so it is recommended to run Python in “optimized” mode (python -O or setting the PYTHONOPTIMIZE environment variable) when running code containing type checks in production. The optimized mode will disable the type checks, by virtue of removing all assert statements and setting the __debug__ constant to False.

Using check_argument_types():

from typeguard import check_argument_types

def some_function(a: int, b: float, c: str, *args: str):
    assert check_argument_types()
    ...

Using @typechecked:

from typeguard import typechecked

@typechecked
def some_function(a: int, b: float, c: str, *args: str) -> bool:
    ...

To enable type checks even in optimized mode:

@typechecked(always=True)
def foo(a: str, b: int, c: Union[str, int]) -> bool:
   ...

The following types from the typing package have specialized support:

Type

Notes

Dict

Keys and values are typechecked

List

Contents are typechecked

Set

Contents are typechecked

Tuple

Contents are typechecked

Callable

Argument count is checked but types are not (yet)

TypeVar

Constraints, bound types and co/contravariance are supported but custom generic types are not (due to type erasure)

Union

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

typeguard-1.2.1.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typeguard-1.2.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file typeguard-1.2.1.tar.gz.

File metadata

  • Download URL: typeguard-1.2.1.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for typeguard-1.2.1.tar.gz
Algorithm Hash digest
SHA256 3a5596e9e6f98ac35ae9bfaf2de2b0c2bf8fcc2694600377efaa04b8420f231f
MD5 d78d8215e7831a3c408de5135a67c0d2
BLAKE2b-256 cec9c00ce04074a447c66ad220fd033ede55e2239027b3a0fb874781333c0673

See more details on using hashes here.

File details

Details for the file typeguard-1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for typeguard-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f13df706f60448e6292eb31fc189722073f55bd3a7e1760fe83e096ba121e7cf
MD5 6f7eaa8cea4d1b5416f95043bd4cf676
BLAKE2b-256 c49395ade0d28d90634f8c4fde65fec5d07d7df0286bef3fbc6f43941db6dc91

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