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
) -> tuple[str, int]:  # 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.

@annotate
def foo(bar: Values(range(5))):
    ...

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.

@annotate
def foo(bar: Cast(tuple[int | str])):
    print(bar)

foo((3, "3"))  # (3, 3)
foo((3j, " "))  # ('3j', ' ')

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):
    ...

If the check name is prepended with a _, it will be negated, and an exception is raised if it fails. 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')):
    ...

Checks can be used as annotations; called using pyoload.Checks as Checks(foo=bar)(val); or Invoked directly using pyoload.Checks as: Check.check(name, param, arg)

len

Receives as argument an integer value specified the expected length or a slice in which the length should be found

gt, lt, eq

Compares grater than, less than and aqual to from the parameter to the value.

func

Uses a function for validation, the function could return a boolean or raise an error. It could be passed directly as positional arguments to pyoload.Checks as: Checks(func1, func2, foo=bar, foo2=bar2)

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])
    

The values are checked or casted on assignment

[!NOTE] The attributes will return None if not yet initialized

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.2.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

pyoload-2.0.2-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file pyoload-2.0.2.tar.gz.

File metadata

  • Download URL: pyoload-2.0.2.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for pyoload-2.0.2.tar.gz
Algorithm Hash digest
SHA256 485c02413b7926ada25ecd39294c2ef2bc844822e7447007d00f3ef489586f05
MD5 44bacdfd488f5b6765244dbea4c23a5b
BLAKE2b-256 535fff814c140958ed98dffe9a0dbfa21ecc37a9f3e891f65a21bc9ebbd42eb1

See more details on using hashes here.

File details

Details for the file pyoload-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: pyoload-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for pyoload-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d443d55ce7f1d5b1cbf405dc58df018baf060ffc317b0e834a5999d80e2c40f3
MD5 68f0cc9eda16358d34b53e29c30e3ff5
BLAKE2b-256 659d95ccfb671f4cd626396547b9b9a2f9dc732f3a427150bc66b921ebee90de

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