Skip to main content

Supports enforcing type annotations on functions and coroutines. Complete support for types from typing module.

Project description

type-enforce-py

Supports enforcing type annotations on functions and coroutines. Complete support for types from typing module.

Installation

Windows
py -m pip install type-enforce
Unix/MacOS
python3 -m pip install type-enforce

Examples

1. Basic usage
from type_enforce import enforce_type

@enforce_type
def my_typed_fn(text: str, age: int, time: datetime.datetime.fromisoformat):
    print(type(text), type(age), type(time))

my_typed_fn(234324, "47539", '2021-09-15T07:49:38.412586') #time in isoformat is passed to the fromisoformat classmethod of datetime class which converts it to an actual datetime class, the same goes with the first two arguments
2. Usage with typing.Union, typing.Literal and other types.
@enforce_type
def myfn(val: typing.Dict[str, typing.Union[float, int]]):
    print(type(val), val)
    print(" ".join([str(type(i))+" + "+str(type(j)) for i, j in val.items()]))

myfn({'hello':'3453', True: '345.345'}) # Output: <class 'dict'> {'hello':'3453', True: '345.345'} \n <class 'str'> + <class 'int'> <class 'str'> + <class 'float'>

@enforce_type
def literal_example(value: typing.Literal['no', 'yes']):
    print(value)

literal_example('yes') #Ok
literal_example('yas') #Error
literal_example('no') #Ok

What this does under the hood ?

First checks if the type of the value is one of the annotated types, if yes then returns it else tries to convert it according to the annotation if at all it is convertible if it fails, a TypeError is thrown most probably or the callable to which the value was passed could even throw a custom error for example: datetime.datetime.fromisoformat.

Enjoy making your functions statically / strongly typed.

Project details


Download files

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

Source Distribution

type-enforce-3.9.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

type_enforce-3.9.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file type-enforce-3.9.0.tar.gz.

File metadata

  • Download URL: type-enforce-3.9.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for type-enforce-3.9.0.tar.gz
Algorithm Hash digest
SHA256 4f33eeb40000787c8e43b653949f200b675157cb5848456a95d355080de587a6
MD5 8eb9b50aa814bcbf8e701ada8f265a08
BLAKE2b-256 aba24f1ef048b66ec034645773f066b26c09e9e795d75b7d43cfafb69b18fde8

See more details on using hashes here.

File details

Details for the file type_enforce-3.9.0-py3-none-any.whl.

File metadata

  • Download URL: type_enforce-3.9.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for type_enforce-3.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e1f43665b03a645162ec84ef29ed028b5b0bdd00fe9d42fc324616d2425ce08d
MD5 41d4609f82349a054499d7dac78ace4d
BLAKE2b-256 fcc7054534aab40cdaa89430dd6116a570f07680aa548a3679a8c28674ac2b82

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page