mock.mock_open __exit__ called as instance method from contextlib.ExitStack context
未关闭
还没有人认领这个 Issue。
3.13
3.14
stdlib
type-bug
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
In Python 3.13 onwards:
from contextlib import ExitStack
from unittest import mock
with mock.patch("__main__.open", mock.mock_open()) as m:
with ExitStack() as exit_stack:
with exit_stack.enter_context(open("/tmp/test.txt", "w")):
pass
raises
TypeError: mock_open.<locals>._exit_side_effect() takes 3 positional arguments but 4 were given
The issue seems to come from https://github.com/python/cpython/commit/3f7c0810f6158a7ff37be432f8d7f9511427489f
It appears that in Python versions 3.12 and earlier, mock_open().__exit__ was always called with 4 args (self, exctype, excinst, exctb) when used with contextlib.ExitStack but that used to be a no-op before the exit handler was added.
I'm not sure whether this should be fixed by:
- changing the way
ExitStackcalls its stack of__exit__callbacks - making
mock_openandopenmore similar, or - making
mock_open's exit handler take*args, **kwargssame as the enter handler??
Thanks
CPython versions tested on:
3.12, 3.13, 3.14
Operating systems tested on:
Linux
Linked PRs
- gh-150521
- gh-150535
- gh-151829
- gh-151861
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先使用 contextlib.ExitStack 和 unittest.mock.mock_open 开始复现,然后检查 issue 中描述的 callback 行为。完成标准是解决 Python 3.13+ 中的 TypeError,同时保留预期的 context manager 行为,并为报告的情况添加 coverage。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- testing
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100