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 PEP 484 argument (and return) type annotations.

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

  1. 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)
  2. the @typechecked decorator:
    • automatically type checks yields and sends of returned generators (regular and async)
    • adds an extra frame to the call stack for every call to a decorated function
  3. the stack profiler hook (with TypeChecker('packagename'):) (deprecated):
    • emits warnings instead of raising TypeError
    • requires very few modifications to the code
    • multiple TypeCheckers can be stacked/nested
    • does not work reliably with dynamically defined type hints (e.g. in nested functions)
    • may cause problems with badly behaving debuggers or profilers
    • cannot distinguish between an exception being raised and a None being returned
  4. the import hook (typeguard.importhook.install_import_hook()):
    • automatically annotates classes and functions with @typechecked on import
    • no code changes required in target modules
    • requires imports of modules you need to check to be deferred until after the import hook has been installed
    • may clash with other import hooks

See the documentation for further instructions.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for typeguard, version 2.12.1
Filename, size File type Python version Upload date Hashes
Filename, size typeguard-2.12.1-py3-none-any.whl (17.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size typeguard-2.12.1.tar.gz (39.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page