import asyncio import inspect from typing import Union, Awaitable, TypeVar async def async_yield(): # a value of exactly 0 doesn't seem to work as well, so we use 1 microsecond await asyncio.sleep(1e-6) Args = TypeVar('Args') Return = TypeVar('Return') Maywaitable = Union[Return, Awaitable[Return]] async def maywait(obj: Maywaitable): if inspect.isawaitable(obj): obj = await obj return obj