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 three principal ways to use type checking, each with its pros and cons:

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

    • debugger friendly

    • cannot check the type of the return value

    • does not work reliably with dynamically defined type hints (e.g. in nested functions)

  2. decorating the function with @typechecked:

    • can check the type of the return value

    • adds an extra frame to the call stack for every call to a decorated function

  3. using with TypeChecker('packagename')::

    • emits warnings instead of raising TypeError

    • eliminates boilerplate

    • multiple TypeCheckers can be stacked/nested

    • noninvasive (only records type violations; does not raise exceptions)

    • does not work reliably with dynamically defined type hints (e.g. in nested functions)

    • may cause problems with badly behaving debuggers or profilers

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

Using TypeChecker:

from warnings import filterwarnings

from typeguard import TypeChecker, TypeWarning

# Display all TypeWarnings, not just the first one
filterwarnings('always', category=TypeWarning)

# Run your entire application inside this context block
with TypeChecker(['mypackage', 'otherpackage']):
    mypackage.run_app()

# Alternatively, manually start (and stop) the checker:
checker = TypeChecker('mypackage')
checker.start()
mypackage.start_app()

The following types from the typing package have specialized support:

Type

Notes

Callable

Argument count is checked but types are not (yet)

Dict

Keys and values are typechecked

List

Contents are typechecked

NamedTuple

Field values are typechecked

Set

Contents are typechecked

Tuple

Contents are typechecked

Type

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-2.1.3.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

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

typeguard-2.1.3-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for typeguard-2.1.3.tar.gz
Algorithm Hash digest
SHA256 f33a66f60b18765910ba479bb80ce13ac7ea712eb014e1e2d9c910c50a8c7750
MD5 124b5a75a6512ec0b0bbf3df1286e4f9
BLAKE2b-256 d93286a3550d71de6b9d2f422d76abc832a21e3cf2b94f5d61d3a2d00187114c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typeguard-2.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 970ad7aa0b7460a478fe0006a2e90924726d3d8dcb4cf9dd7be4681004aaac9d
MD5 de4e39830055224b1d3446263a03669f
BLAKE2b-256 9235e6d75515e7bae6444539e051bfa1f0e91192501078c4e18cf21bb11e3b13

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