My proposal is to make retry function possible to be used as context manager which wraps original function in the with block:
>>> from asynctest import CoroutineMock
>>> async def case1():
... fn = CoroutineMock(side_effect=[RuntimeError, 'success'])
... return await fn()
>>> async def case2():
... fn = CoroutineMock(side_effect=[RuntimeError, 'success'])
... with retry(fn) as wrapped:
... return await wrapped()
>>> asyncio.get_event_loop().run_until_complete(case1())
RuntimeError:
...
>>> asyncio.get_event_loop().run_until_complete(case2())
'success'
My proposal is to make
retryfunction possible to be used as context manager which wraps original function in thewithblock: