Review a generated payment decorator

from Decorators
Python 3.14 advanced 8 min 5 issues to find

Review this generated decorator before it wraps a payment capture function.

Add bounded retries and audit logging while preserving the function contract and keeping distinct payment requests separate.

Python
import time
from functools import wraps
def audited_retry(attempts=3):
    cache = {}
    def decorate(func):
        def wrapper(user, amount, **kwargs):
            print(f"calling {func.__name__}: {user=}, {kwargs=}")
            key = (user["id"], amount)
            if key in cache:
                return cache[key]
            for attempt in range(attempts):
                try:
                    result = func(user, amount, **kwargs)
                    cache[key] = result
                    return result
                except Exception:
                    time.sleep(2 ** attempt)
            return None
        return wrapper
    return decorate
@audited_retry()
def capture_payment(user, amount, *, request_id):
    return gateway.charge(user, amount, request_id=request_id)

generated code is illustrative, not from any one model

Open in playground
Report an error