Skip to main content

Python package for function argument overload, typechecking and casting

Project description

pyoload

Release Status PyPI package Supported Python versions Build Status Coverage Status

pyoload has two main functions

pyoload.annotate

Is used as a decorator on the function.

from pyoload import annotate

@annotate
def twice(a:int) -> int:
    return a * 2

b = twice(4)

The annotate creates a wrapper over the decorated function which checks in for argument types over each function call using pyoload.matchType(val, spec). The original function is kept in the .__pyod_annotate__ attribute.

pyoload.overload

Implements function overloading in python via a simple decorator

from pyoload import overload
import math
cache = {}

tan_is_real = lambda v: not (v + 90) % 180 == 0

@overload
def tan(num:Validator(tan_is_real, opposite=True)):
    raise ValueError(num)

@overload
def tan(num:int|float) -> float:
    return math.tan(num(

tan(6)

When an overload is registerred, the function name in the form functionModuleName.functionQualName is goten using pyoload.get_name(funcOrClass) an annotate is gotten using pyoload.annotate(func, True) and a new list of overloads is created and stored in pyoload.__overloads__ dictionarry under it's name. A reference to the list of annotated overloads is stored in the functions .__pyod_overloads__.

When the function is called, the wrapper tries all the functions registerred to that name to catch a pyoload.InternalAnnotationError. If none ran succesfully, it raises an pyoload.OverloadError.

Casting

All pyoload.annotate and pyoload.overload both support Cast objects instances of pyoloas.Cast. It uses recursive casting with integrated support for dictionaries, e.g:

dict[
  int,
  tuple[list[float] | float]
]

for a dictionarry mapping of integers to list of floats or floats.

Note: When a union, e.g int | str is passed to Cast, it tries to cast in each of the specified types in the listed order, that is

cast = pyoload.Cast(int|str)
print(repr(cast(3.0)))

Will print '3.0' as 3.0 can not be converted to a complex

Accepted annotations

In addition to supporting normal plain types, pyoload includes support for generic aliasses of iterable types and some other classes:

  • pyoload.Values(iterable) e.g Values("+-*/") or Values(range(6))
  • pyoload.Cast(type) Instructs pyoload to cast to the specified type
  • A string The string contents will be evaluated as soon as first function call.

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-1.0.2.tar.gz (6.5 kB view hashes)

Uploaded Source

Built Distribution

pyoload-1.0.2-py3-none-any.whl (6.2 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