python / python/typing

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

未关闭
#542 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

topic: feature
主要语言
Python
星标
1.8k
派生
302
平均合并
23 小时
30 天内合并 PR
8

描述

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.

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先阅读 issue 中描述的 Awaitable 定义和 await 协议,然后比较 Future、Deferred、curio traps 和 Trio traps 的表示方式。只有在形成了一个关于如何表达不兼容的 scheduling 协议、同时保留兼容的 awaitables 的共识模型后,工作才算完成;未指定文件或测试。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
tooling
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。