Skip to main content

A Python decorators allowing to check and/or enforce types in functions' arguments based on typing hints

Project description

doc License: GPL v3

Status

Pytests push

maintained issues pr

Compatibilities

ubuntu unix

python

Contact

linkedin website mail

AdTypingDecorators

Python decorators allowing to check and/or enforce types in functions' arguments based on typing hints.

Installation

pip install adtypingdecorators

Usage

import numpy as np
import pandas as pd
from adtypingdecorators import typing_raise, typing_convert, typing_warn, typing_custom


def to_array(a: int):
    return np.array([a, 2 * a])


def to_array_2(a: int):
    return np.array([a, 3 * a])


@typing_raise
def f_raise(a: int):
    return a + 1


@typing_convert
def f_convert(a: int):
    return a + 1


@typing_warn
def f_warn(a: int):
    return a + 1


@typing_custom(
    convertors={int: to_array, "b": to_array_2},
    exclude=["c", pd.DataFrame]
)
def f_custom(a: np.ndarray, b: np.ndarray, c: np.ndarray, d: np.ndarray):
    return a + 1, b + 1, c + 1, d + 1


f_raise(1)  # Returns 2, as expected
# noinspection PyTypeChecker
f_raise(1.5)  # Raises TypeError

# noinspection PyTypeChecker
f_convert(1.5)  # Returns 2 (converted 1.5 into 1)
# noinspection PyTypeChecker
f_convert("foo")  # Raises ValueError (while trying to convert 'foo' to interger)

# noinspection PyTypeChecker
f_warn(1.5)  # Returns 2.5, and warns

# noinspection PyTypeChecker
a_, b_, c_, d_ = f_custom(1, 2, 3, pd.DataFrame([4]))
# a_ is np.array([2, 3])
# b_ is np.array([3, 7])
# c_ is 4
# d_ is pd.DataFrame([5])

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

adtypingdecorators-0.1.10.tar.gz (116.0 kB view details)

Uploaded Source

File details

Details for the file adtypingdecorators-0.1.10.tar.gz.

File metadata

  • Download URL: adtypingdecorators-0.1.10.tar.gz
  • Upload date:
  • Size: 116.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for adtypingdecorators-0.1.10.tar.gz
Algorithm Hash digest
SHA256 ffd1e39b5248a3281d625a15a6e79bceadde90e35aaba76c3cd43e01c570f363
MD5 7c60a62348c113282dd275710530a4eb
BLAKE2b-256 6b771534d32379aca6e22774c036cba16637d54c98f226da81bdf91c29ead5d4

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