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
Metadata
Release files for paramorator 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| paramorator-1.0.2.tar.gz | 4.5 kB | Without provenance | Details for paramorator-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| paramorator-1.0.2-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for paramorator-1.0.2-py2.py3-none-any.whl |
Total release size: 7.9 kB
Release files / paramorator-1.0.2.tar.gz
| Download URL | paramorator-1.0.2.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2e034ef97edb2debbe7f155b9204195864dc141682c3f01fa59ef89c3b79d24e
|
|
BLAKE2b-256 checksum How to use checksums |
7a0477c18d2d2c68d5b3af85f1d4952ca4d8f8ecded9c27fe203418f848dc76a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.3
|
Release files / paramorator-1.0.2-py2.py3-none-any.whl
| Download URL | paramorator-1.0.2-py2.py3-none-any.whl |
|---|---|
| Size | 3.4 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
ceb908ccb993bcfad153fec69502052cf678d11af0b7a8d122b942233a612652
|
|
BLAKE2b-256 checksum How to use checksums |
39a20bab6d3108121a7356e1c4c2843e956fde39466d65959bc9c1476c77d930
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.3
|
Security for paramorator 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.