Skip to content

Instantly share code, notes, and snippets.

@Callek Callek/foo.py
Last active Jan 27, 2020

Embed
What would you like to do?
mypy confusion
import functools
from typing import Any, Awaitable, Callable, Dict, Optional, Type, TypeVar, cast
async def retry_async(func: Callable[..., Awaitable[Any]], *args: Any, **kwargs: Any) -> Any:
return await func(*args, **kwargs)
def retry_async_decorator(retry_exceptions: Optional[Type[BaseException]]=Exception, sleeptime_kwargs: Optional[Dict[str, Any]]=None) -> Callable[..., Awaitable[Any]]:
"""Decorate a function by wrapping ``retry_async`` around.
Args:
retry_exceptions (list or exception, optional): the exception(s) to retry on.
Defaults to ``Exception``.
sleeptime_kwargs (dict, optional): the kwargs to pass to ``sleeptime_callback``.
If None, use {}. Defaults to None.
Returns:
function: the decorated function
"""
def wrap(async_func: Callable[..., Awaitable[Any]]) -> Callable[..., Awaitable[Any]]:
@functools.wraps(async_func)
async def wrapped(*args: Any, **kwargs: Any) -> Any:
return await retry_async(async_func, retry_exceptions=retry_exceptions, args=args, kwargs=kwargs, sleeptime_kwargs=sleeptime_kwargs)
return wrapped
return wrap
class Foo:
@retry_async_decorator()
def bar(self, x: int) -> int:
return x+2
@retry_async_decorator(retry_exceptions=AttributeError, sleeptime_kwargs={'a': 10})
def baz(self, s: str) -> str:
return "{}foo".format(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.