Python package for function argument overload, typechecking and casting
Project description
pip-package-template-docker
pyoload has two main functions
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.
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
.
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.gValues("+-*/")
orValues(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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.