π Masks fp
Functional wrapping for Python.
Advanced ⬦ Powerful ⬦ Tooling

Background
Wrapping is a technique targeting function extensibility, taking metadata of one function and merging it with another, including any intended additional functionality, forming a new function.
Usage
Mask fp is a powerful tool made for wrapping functions and is built on top of the functools.wraps
function. The masks
function allows for quick creation of wrapper function look-alikes by placing the masks
decorator over target wrapper functions and calling it along with the desired target wrapped function. Furthermore, the masks
function will preserve type hinting, signatures, annotations, and the application of default arg and kwarg values, for an enhanced developer experience and potentially advanced use cases.
Features
- Automatic application of default arguments during calls, just as it would originally.
- Fully applied
__signature__
and __annotations__
for wrappers; parameters of wrapped
and return annotation of wrapper
.
- Type hinting for newly created wrapper functions! β¨
Install
Using pip
pip install masks-fp
Or using Poetry
poetry add masks-fp
Example
from collections.abc import Callable, Iterable
from mask_fp import masks
def decorator(f: Callable[..., Iterable[int]]) -> Callable:
@masks(f)
def wrapper(*args, **kwargs) -> dict[str, int | Iterable[int]]:
"""
From ``wrapper`` ``docstring``. π
"""
print(f"Sum of {f(*args, **kwargs)} is {sum(f(*args, **kwargs))}. π")
return f(*args, **kwargs)
return wrapper
@decorator
def wrapped(a: int, /, b: int = 2, *, c: int = 3) -> tuple[int, int, int]:
"""
From ``wrapped`` ``docstring``. π
"""
return (a, b, c)
Without using default values
>>> print(wrapped(1, 2, c=3))
Sum of (1, 2, 3) is 6. π
(1, 2, 3)
Using default values
>>> print(wrapped(1))
Sum of (1, 2, 3) is 6. π
(1, 2, 3)