Method-aware sync/async decorator factories for Python.
Project description
artdeco
artdeco is a tiny, zero-dependency Python library that makes writing decorator factories effortless: works with plain functions and instance methods, for both synchronous and asynchronous code, and for stacking multiple decorators in any combination.
You write one function. artdeco turns it into a fully-featured, stackable
decorator factory.
Why artdeco?
Writing a production-quality decorator factory from scratch is surprisingly tedious:
- You need a descriptor (
__get__) to keepselfworking on methods. - You need
functools.wrapsto preserve__name__,__doc__, etc. - Handling
async defrequires extra work, sinceiscoroutinefunctionstops working once you wrap an async function without explicitly marking the wrapper. - You need to guard against accidentally mixing sync/async.
- You want to stack multiple decorators on the same function.
artdeco handles all of that for you.
Features
- Works on methods: decorators preserve descriptor binding so
selfis always correct. - Sync and async: one API covers both; write
async defto get an async decorator factory. - Stackable: apply as many decorators as you like; ordering is preserved.
- Inspect call internals:
callis a plainfunctools.partial, giving you access tocall.func,call.args, andcall.keywordsat decoration time. - Misuse detection: applying a sync decorator to an
async def(or vice versa) raises a clearDecorationError. - Fully typed: ships with
py.typed; works with Pyright and mypy. - Zero dependencies: pure Python 3.12+, uses only the standard library.
Installation
pip install py-artdeco
Quick Start
Synchronous decorator factory
from artdeco import decorator
@decorator()
def log(call, prefix: str):
print(prefix, "calling")
return call()
@log("DEBUG:")
def add(a: int, b: int) -> int:
return a + b
assert add(1, 2) == 3 # prints "DEBUG: calling"
Async decorator factory
from artdeco import decorator
@decorator()
async def traced(call, prefix: str):
print(prefix, "before")
result = await call()
print(prefix, "after")
return result
@traced("ASYNC")
async def work(x: int) -> int:
return x * 2
Works seamlessly on instance methods
from artdeco import decorator
@decorator()
def retry(call, times: int):
for _ in range(times):
try:
return call()
except Exception:
pass
raise RuntimeError("all retries failed")
class Client:
@retry(times=3)
def fetch(self) -> str: # self is bound correctly
...
Stack multiple decorators
from artdeco import decorator
@decorator()
def log(call, label: str):
print(f"[{label}] calling {call.func.__name__}")
return call()
@decorator()
def validate(call):
result = call()
assert result is not None
return result
@log("INFO")
@validate()
def compute(x: int) -> int:
return x * 2
Inspect the current invocation via call
call is a functools.partial, so the current call's arguments are always
available without any extra machinery:
from artdeco import decorator
@decorator()
def audit(call, label: str):
print(
f"{label} | {call.func.__name__}"
f" args={call.args} kwargs={call.keywords}"
)
return call()
@audit("AUDIT")
def transfer(amount: float, *, currency: str = "USD") -> bool:
return True
transfer(100.0, currency="EUR")
# AUDIT | transfer args=(100.0,) kwargs={'currency': 'EUR'}
Development
This project uses uv for environment and dependency management.
Install tools and dependencies:
uv sync --group dev
Run checks:
uv run ruff check .
uv run pytest
uv run pyright src
Run typing tests:
uv run pyright src tests/typing
Build and validate distribution metadata:
uv build
uv run twine check dist/*
License
MIT, see LICENSE.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_artdeco-0.2.0.tar.gz.
File metadata
- Download URL: py_artdeco-0.2.0.tar.gz
- Upload date:
- Size: 32.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f02af1c876dd4353ff4db85501ee40fecd849762ffbb3e14717fa5f38691b00f
|
|
| MD5 |
2500d7b0a987147d5001c5b4f8b14b66
|
|
| BLAKE2b-256 |
83ef914bd8d498c70d66ad9dfc4198950015ce425095d1e6502aa260d702655e
|
Provenance
The following attestation bundles were made for py_artdeco-0.2.0.tar.gz:
Publisher:
publish.yml on WimYedema/artdeco
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
py_artdeco-0.2.0.tar.gz -
Subject digest:
f02af1c876dd4353ff4db85501ee40fecd849762ffbb3e14717fa5f38691b00f - Sigstore transparency entry: 1239303415
- Sigstore integration time:
-
Permalink:
WimYedema/artdeco@c87574f44c09a220846794ef021aeea6a833325c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/WimYedema
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c87574f44c09a220846794ef021aeea6a833325c -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file py_artdeco-0.2.0-py3-none-any.whl.
File metadata
- Download URL: py_artdeco-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
740bc7e422e0c1d8fe156f6598ce91e166a018b74dbb7b56bc2c512bb6e30509
|
|
| MD5 |
98c93a05a2a612e459743d0916dc4b9f
|
|
| BLAKE2b-256 |
a78a1a3f05314000ee3a471fed137807b318cf9d15ed8e5b1160f901dacaceba
|
Provenance
The following attestation bundles were made for py_artdeco-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on WimYedema/artdeco
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
py_artdeco-0.2.0-py3-none-any.whl -
Subject digest:
740bc7e422e0c1d8fe156f6598ce91e166a018b74dbb7b56bc2c512bb6e30509 - Sigstore transparency entry: 1239303416
- Sigstore integration time:
-
Permalink:
WimYedema/artdeco@c87574f44c09a220846794ef021aeea6a833325c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/WimYedema
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c87574f44c09a220846794ef021aeea6a833325c -
Trigger Event:
workflow_dispatch
-
Statement type: