Skip to main content

This package enforces the functions' type hints at runtime, making sure that the types of the actual parameters and the return value match the types specified in the function signature.

Project description

Type Asserter: A tool to type

This package enforces the functions' type hints at runtime, making sure that the types of the actual parameters and the return value match the types specified in the function signature.

Installation

The source code is currently hosted on GitHub at: https://github.com/ValerioNeriGit/type_asserter

Binary installers for the latest released version are available at the Python package index.

pip3 install type_asserter

Usage

Define your function using the Python3.8 type hints and add the @enforce_type_hints decorator

from type_asserter import enforce_type_hints


@enforce_type_hints
def power(num: int, pow: int, verbose: bool = False) -> int:
    result = num ** pow

    if verbose:
        print("The result is:", result)

    return result


power(3, 4, verbose=True)

When one or more type mismatches are found, for each mismatched parameter is shown its name (as in the function's signature, its expected type, and its actual type:

TypeError: Parameter's type mismatch
   Parameter's name: num, expected: <class 'int'> but got: <class 'str'>
   Parameter's name: pow, expected: <class 'int'> but got: <class 'str'>

Performance

The package endeavors to impact as little as possible on the performance of your program, but still, it introduces some overhead.

You can disable the type check in order to maximize performance setting disable=True as in the example below

If you want to disable the type check for

from type_asserter import enforce_type_hints


@enforce_type_hints(disable=True)
def power(num: int, pow: int) -> int:
    return  num ** pow

Show a warning instead of an error

It is possible to set warning_instead_of_error=True in order to have a non-interrupting warning instead of a TypeError, as in the example below.

from type_asserter import enforce_type_hints


@enforce_type_hints(warning_instead_of_error=True)
def power(num: int, pow: int) -> int:
    return  num ** pow

Author

Valerio Neri - github

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 type-asserter, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size type_asserter-1.0.0.tar.gz (3.3 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