python / python/typeshed

Mypy complains about the `all_equal` recipe from the itertools docs

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

还没有人认领这个 Issue。

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

描述

The following function is given as a recipe in the itertools docs, indicating that it's an idiomatic usage of groupby:

from itertools import groupby

def all_equal(iterable):
    g = groupby(iterable)
    return next(g, True) and not next(g, False)

I think the correct way of adding type annotations to this function would be as follows, since it will work on arbitrary iterables:

from collections.abc import Iterable
from itertools import groupby

def all_equal(iterable: Iterable[object]) -> bool:
    g = groupby(iterable)
    return next(g, True) and not next(g, False)

Unfortunately, however, mypy complains about this function:

error: Argument 1 to "next" has incompatible type "groupby[object, object]"; expected "SupportsNext[bool]"  [arg-type]

(Mypy gives a similar error if I use Iterable[Any] instead of Iterable[object] for the argument annotation.)

Perhaps we should consider copy-and-pasting all the itertools recipes into our test_cases directory. They're all meant to be idiomatic uses of itertools, so if any of them fail to type check, there's probably a problem somewhere.

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先,使用提议的 Iterable 注解和当前的 mypy 行为,复现 itertools 文档中的 all_equal 配方。检查现有的 test_cases 目录,并考虑将 itertools 配方添加到其中,以配方成功通过类型检查作为完成标准。

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

评估

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

把新 issue 发到你的邮箱

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