Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 494
oblalex Avatar asked Aug 03 '26 02:08

oblalex


1 Answers

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

like image 115
oblalex Avatar answered Aug 05 '26 20:08

oblalex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!