Skip to main content

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


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.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

paramorator-1.0.0-py2.py3-none-any.whl (3.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file paramorator-1.0.0.tar.gz.

File metadata

  • Download URL: paramorator-1.0.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for paramorator-1.0.0.tar.gz
Algorithm Hash digest
SHA256 53ec8a4e1dce882d95a6c8192ee3daf5af2c65a6b2cc3c47cd576296d91c8041
MD5 1caf20fd13cbf0d9ca235e12d4e9f199
BLAKE2b-256 cd2e44eedb9e00978ac14af0fb06a91444f54524148b75fdbea322be8809b297

See more details on using hashes here.

File details

Details for the file paramorator-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for paramorator-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1e25596dd321aeed365557113bf42bd9309f76656291ede1b509e5cf79ff68ca
MD5 e4f48426e9c20a08cfb3b0c595439e51
BLAKE2b-256 6b1844160ea9f19551c997558f2dc9c1e688266afa77f6db9e5b082930d4cae2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page