Python package for function argument overload, typechecking and casting
Project description
pyoload
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.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.