Skip to main content

Run-time type checker for Python

Project description

Build Status Code Coverage Documentation Tidelift

This library provides run-time type checking for functions defined with PEP 484 argument (and return) type annotations, and any arbitrary objects. It can be used together with static type checkers as an additional layer of type safety, to catch type violations that could only be detected at run time.

Three principal ways to do type checking are provided, each with its pros and cons:

  1. The check_type function:

    • like isinstance(), but supports arbitrary type annotations (within limits)

    • can be used as a cast() replacement, but with actual checking of the value

  2. The check_argument_types() and check_return_type() functions:

    • debugger friendly (except when running with the pydev debugger with the C extension installed)

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

  3. Code instrumentation:

    • entire modules, or individual functions (via @typechecked) are recompiled, with type checking code injected into them

    • automatically checks function arguments, return values and assignments to annotated local variables

    • for generator functions (regular and async), checks yield and send values

    • requires the original source code of the instrumented module(s) to be accessible

Two options are provided for code instrumentation:

  1. the @typechecked function:

    • can be applied to functions individually

  2. the import hook (typeguard.install_import_hook()):

    • automatically instruments targeted modules on import

    • no manual code changes required in the target modules

    • requires the import hook to be installed before the targeted modules are imported

    • may clash with other import hooks

See the documentation for further information.

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

Uploaded Source

Built Distribution

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

typeguard-4.5.2-py3-none-any.whl (36.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typeguard-4.5.2.tar.gz
  • Upload date:
  • Size: 80.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for typeguard-4.5.2.tar.gz
Algorithm Hash digest
SHA256 5a16dcac23502039299c97c8941651bc33d7ea8cc4b2f7d6bbb1b528f6eea423
MD5 4bedcaf8bcee6c2ec90c2b11ad644182
BLAKE2b-256 671cdfba5c4633cafc4c701f237d2ba63b416805047fd6d96aab4cfc40969f98

See more details on using hashes here.

Provenance

The following attestation bundles were made for typeguard-4.5.2.tar.gz:

Publisher: publish.yml on agronholm/typeguard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: typeguard-4.5.2-py3-none-any.whl
  • Upload date:
  • Size: 36.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for typeguard-4.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fcf9de18bd945cdb4c7b996e12b4c51ce83f92f191314a6d7cf1739586ec98cf
MD5 647797cfd54142cef198dcf167231ebc
BLAKE2b-256 5b2974eeb4d3f3ae61ca096b018ad486b3b3c74b17bec09ab4edab721cbefec3

See more details on using hashes here.

Provenance

The following attestation bundles were made for typeguard-4.5.2-py3-none-any.whl:

Publisher: publish.yml on agronholm/typeguard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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