jd / tenacity

Retrying library for Python

Home Page:http://tenacity.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add type overloads for async `sleep` function

Zac-HD opened this issue · comments

import trio  # or asyncio, etc.
import tenacity

@tenacity.retry(
    sleep=trio.sleep,  # typecheckers will error here!
)
async def foo():
    pass

The type annotation here

@t.overload
def retry(
sleep: t.Callable[[t.Union[int, float]], None] = sleep,

should be t.Callable[[t.Union[int, float]], t.Union[None, t.Awaitable[None]], since AsyncRetrying allows async sleep functions.

Going further, we could define an additional AsyncWrappedFn typevar and propagate that though to ensure that the sleep callback is always awaitable when retrying for a coroutine function. This is a pretty important performance footgun to avoid!