Skip to main content

Python package for function argument overload, typechecking and casting

Project description

Release status PyPI package Supported Python versions Build Status Coverage Status Documentation Status Pypi downloads Pypi downloads

pyoload

Hy pythonista! I'm happy to present to you pyoload, as from my words:

A python module for extended and recursive type checking and casting of function arguments and class attributes during runtime

Here we use some of the beautiful and clean features offered by python, including decorators and descriptors to help you type check during runtime

Here are some simple usage examples to fill this pypi page.

annotate

This decorator function uses the power of inspect.signature to check the arguments passed to the function using it's annotations with support for default values, generic aliase and annotations adding some more annotation types for convenience, lower some code.

from pyoload import *

@annotate
def foo(
    a: str,     # this has an annotation
    b=3,        # this uses a default value
    c: int = 0  # here both
) -> None:  # The return type
    ...
from pyoload import *

@annotate
def foo(
    b=dict[str | int, float],     # here a GenericAlias
    c: Cast(list[int]) = '12345'  # here a recursive cast
):  # No return type
    ...

multimethod

This uses the same principles as annotate but allows multiple dispatching (a.k.a runtime overloading?) of functions.

from pyoload import *

@multimethod
def foo(a, b):
    print("two arguments")

@multimethod
def foo(a: Values((1, 2, 3))):
    print('either 1, 2 or 3')

@foo.overload
def _(a: Any):
    raise ValueError()

annotations

These are what pyoload adds to the standard annotations:

[!NOTE] The added annotations are still not mergeable with the standard types.

pyoload.Values

A simple tuple subclass, use them as annotation and it will validate only included values.

pyoload.Cast

This performs recursive casting of the passed arguments into the specified type It supports dict generic aliases as dict[str, int | str] and tries cast in the specified order when the type is a Union.

pyoload.Checks

Permits You tou use custom checker methods, e.g

from pyoload import *

test = lambda val: True  # put your check here

def foo(a: Checks(func=test):
    ...

You can register your own checks using Check.register, as

@Check.register('mycheck')
def _(param, value):
    print(param, value)

Checks(mycheck='param')('val')  # Will raise error on check failure

@annotate
def foo(a: Checks(mycheck='param')):
    ...

Checked and casted attributes

CheckedAttr and CastedAttr, are simple descriptors which will perform the casting or checks on assignment.

from pyoload import *

class address:
    number = CastedAttr(tuple[int])
    

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

pyoload-2.0.0.tar.gz (15.2 kB view hashes)

Uploaded Source

Built Distribution

pyoload-2.0.0-py3-none-any.whl (10.6 kB view hashes)

Uploaded Python 3

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