python / python/typing

"Awaitable" is insufficiently expressive, as one may await something other than Futures

Open
#542 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: feature
Dominant language
Python
Stars
1.8k
Forks
302
Avg merge
23h
Merged PRs (30d)
8

Description

In the wild, today, if you await something, there are several potentially valid things to bubble back out from the innermost await

  • an asyncio.Future
  • a curio "trap" (which I gather is defined as something like Tuple[curio.traps.Traps, Any, ...])
  • a Trio trap, (which I gather is defined as something like Iterable[WaitTaskRescheduled] although I haven't quite found the bottom of that stack)
  • a Deferred

Not all of these are mutually compatible. However, Awaitable is parameterized only on one type - the type of its result. This results in code like this:

def some_deferred() -> Awaitable[int]: ...
def some_future() -> Awaitable[int]: ...

async def x() -> Awaitable[List[int]]:
    y = []
    y.append(await some_deferred())
    y.append(await some_future())
    return y

type-checking even though it should fail.

I'd like to be able to express this as

def some_deferred() -> Awaitable[int, Deferred]: ...
def some_future() -> Awaitable[int]: ... # presumably Future is the default

async def x() -> Awaitable[List[int]]:
    y = []
    y.append(await some_deferred()) # <-- automatically specialize to `Awaitable[List[int], Deferred]` here, if possible?
    y.append(await some_future()) # <-- type error!
    return y

This is made slightly more complex by the fact that some awaitables are mutually intelligible - for example, anything that awaits a Deferred should eventually be able to await a Future directly, once we've made a few more changes to Twisted. We could also do this in the other direction if __await__ were changed to pass the loop along. Whereas, some of these types are incompatible by design; my understanding is that await implies a completely different, and somewhat higher-level, scheduling protocol in Trio, for example.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the Awaitable definition and the await protocol described in the issue, then compare how Future, Deferred, curio traps, and Trio traps are represented. The work is complete only when there is an agreed model for expressing incompatible scheduling protocols while preserving compatible awaitables; no file or test is identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.