A dead simple utility for defining decorators with parameters that make type checkers happy.
Project description
Paramorator
A dead simple utility for defining decorators with parameters that make type checkers happy.
Installation
pip install paramorator
Usage
from typing import Callable, ParamSpec
from paramorator import paramorator
P = ParamSpec("P")
@paramorator
def multiply(func: Callable[P, float], factor: float = 2) -> Callable[P, float]:
def wrapper(*args: P.args, **kwargs: P.kwargs) -> float:
return factor * func(*args, **kwargs)
return wrapper
@multiply(factor=3)
def add_then_triple(a: float, b: float) -> float:
return a + b
assert add_then_triple(2, 3) == 15
# also supports inline usage
sub_then_double = multiply(lambda a, b: a - b, factor=2)
assert sub_then_double(5, 3) == 4
This isn't exactly rocket science, but to achieve the same result without paramorator
,
you need to write a bunch boilerplate code just to satisfy your type checker. Here is
the equivalent multiple
decorator written without paramorator
:
from typing import Any, ParamSpec, Callable, overload, cast
P = ParamSpec("P")
@overload
def multiply(func: Callable[P, float], /, factor: float = ...) -> Callable[P, float]:
...
@overload
def multiply(func: None = ..., /, factor: float = ...) -> Callable[[Callable[P, float]], Callable[P, float]]:
...
def multiply(
func: Callable[P, float] | None = None,
/,
factor: float = 2,
) -> Callable[P, float] | Callable[[Callable[P, float]], Callable[P, float]]:
def decorator(func: Callable[P, float]) -> Callable[P, float]:
def wrapper(*args: P.args, **kwargs: P.kwargs) -> float:
return factor * func(*args, **kwargs)
return wrapper
return decorator(func) if func else decorator
Development
Install flit
and run:
flit install
To run tests:
python tests.py
Check the types with Pyright:
pyright paramorator.py tests.py
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.
Source Distribution
paramorator-1.0.1.tar.gz
(4.5 kB
view details)
Built Distribution
File details
Details for the file paramorator-1.0.1.tar.gz
.
File metadata
- Download URL: paramorator-1.0.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05a600eb881ea65f2cf62c8e11505f1b48773bd90b1c8160df126197e28fe72a |
|
MD5 | fcf4d928935a25e57136846426939f28 |
|
BLAKE2b-256 | cd6d590729e40efc8f3342e465a645dd575630b8ba7cc936ac67146f8e8de250 |
File details
Details for the file paramorator-1.0.1-py2.py3-none-any.whl
.
File metadata
- Download URL: paramorator-1.0.1-py2.py3-none-any.whl
- Upload date:
- Size: 3.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6c66167006684228dfd3c4f1209b97979aef528b749e19fa3788e57591d6d24 |
|
MD5 | 7a8fdef307ee068646c041fd5e8d29b5 |
|
BLAKE2b-256 | ffad8abdbb6d42f354b6d2b2f6ffb9c8a394e6988b2faae1db1b57bd833e9be3 |