Unitest.mock.MagicMock raises a StopIteration - not following PEP 479

未关闭
#98,362 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

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

调研方向

从 unittest.mock.MagicMock 开始,重现使用耗尽的 iterable 进行重复调用的情况,包括在列表推导式中的使用。结合 mock 的 iterable side effect 行为阅读 PEP 479;当耗尽的 iterable 的预期行为得到确立并由回归测试覆盖时,即视为完成。

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

描述

stdlib

It looks like Unitest.mock.MagicMock raises a StopIteration when repeated calls to the mock are made and an iterable is exhausted. After Python 3.7+, due to PEP 479, this raises a Runtime Error. This makes unit tests that worked in Python 3.6 fail, including any list comprehensions on MagicMock results. These can be easily caught with a try/except, but it's a bit clunky.

According to PEP 479 instead of StopIteration MagicMock should just return return when the iterable is exhausted. See here: https://peps.python.org/pep-0479/

Please let me know if I missed something on my end. Thanks for looking into this.

主要语言
Python
星标
77.2k
派生
36k
平均合并
1 天 9 小时
30 天内合并 PR
558

贡献指南

打开贡献指南

从这里开始

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

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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