pythonpython-asynciofuturepython-typingconcurrent.futures

Python typings and futures


I'm very glad to use typing module in Python 3. Also, I'm very glad to use asyncio instead of twisted, tornado and alternatives.

My question is how to define result of a coroutine properly?

Should we tell it's just a coroutine? Example 1:

async def request() -> asyncio.Future:
    pass

Or should we define type of result of coroutine as type of returning value? Example 2:

async def request() -> int:
    pass

If yes, then how to be with plain functions, which return futures? Example 3:

def request() -> asyncio.Future:
    f = asyncio.Future()
    # Do something with the future
    return f

Is it a right way? How then we can tell what is expected to be a result of the future?


Solution

  • As @jonrsharpe said, typing.Awaitable perfectly suits the task.