A feature-store client sometimes fails with a transient error, and you want every call to it retried a few times with a growing pause. Write a parameterized decorator that adds this behaviour to any function.
Implement retry(max_attempts=3, delay=1.0, backoff=2.0, exceptions=(Exception,), sleep=None). Calling it returns a decorator; applying that decorator to a function f returns a wrapper with the same call signature.
max_attempts is the total number of calls allowed, including the first one. If it is less than 1, retry(...) itself raises ValueError (before any function is decorated).f(*args, **kwargs). If the call returns, the wrapper returns that value.exceptions and attempts remain, the wrapper calls sleep(wait) and tries again. The first wait is delay; after each wait it is multiplied by backoff, so the waits are exceptions, it propagates at once, with no sleep and no retry.sleep defaults to time.sleep when it is None. Tests pass a function that records the waits instead of pausing.__name__ and __doc__, and sets an attribute wrapper.attempts to the number of calls made during the most recent invocation.waits = []
@retry(max_attempts=4, delay=0.5, backoff=2.0, sleep=waits.append)
def fetch():
... # fails twice with ValueError, then returns 42
fetch() # 42
waits # [0.5, 1.0]
fetch.attempts # 3