python / python/typeshed

Awaitable.__await__ expects a Generator where an Iterator will do

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

还没有人认领这个 Issue。

status: deferred stubs: false positive
主要语言
Python
星标
5.1k
派生
2.1k
平均合并
1 天 19 小时
30 天内合并 PR
82

描述

In Twisted, the Deferred class has set __await__ = __iter__, where __iter__ is typed thusly:

    def __iter__(self) -> "Deferred[_DeferredResultT]":

Deferred, insofar as it has an __iter__ method, is thus itself an Iterable. It also implements __next__, so is an Iterator.

Mypy is yielding this error:

src/twisted/internet/defer.py:919:17: error: Incompatible types in assignment (expression has type "Callable[[], Deferred[_DeferredResultT]]", base class "Awaitable" defined the type as
"Callable[[], Generator[Any, None, _DeferredResultT]]")  [assignment]

I suspect that perhaps Awaitable.__await__ could instead be more flexibly typed to return a Iterator[_T] instead of a Generator[Any, None, _T]… as that (a) seemingly works in this case and (b) the documentation for object.__await__ and PEP 492 say that it must return an iterator and not specifically a generator.


Deferred: mypy problem: https://github.com/python/typeshed/pull/5194#issuecomment-816339179

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 mypy 诊断中涉及的 Awaitable 定义开始,然后将其与 src/twisted/internet/defer.py 中约第 894-920 行的 Deferred.iterawait 进行比较。当声明的返回类型能够接受此迭代器实现而不会出现所报告的赋值错误,并且相关类型检查通过时,即视为完成。

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

评估

技术栈
python
领域
devtools
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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