`unittest.mock.Mock` does not allow name attribute to be mocked in constructor
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Would like to set name attribute of a mocked object directly when creating Mock() object.
Currently, the workaround is to do:
from unittest.mock import Mock
obj = Mock(x=1, y="val")
obj.name = "name"
instead of directly assigning the attribute in the constructor.
Has been mentioned in the Stack Overflow community before in https://stackoverflow.com/questions/62552148/how-to-mock-name-attribute-with-unittest-mock-magicmock-or-mock-classes.
Affected code:
https://github.com/python/cpython/blob/4261b6bffc0b8bb5c6d4d80578a81b7520f4aefc/Lib/unittest/mock.py#L1236-L1238
A suggestion would be to rename the current name attribute to mock_name.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 Lib/unittest/mock.py 的 1236-1238 行附近开始,这里实现了 Mock 的名称处理。确定构造函数关键字属性如何与内部名称值分离,然后使补全意味着 Mock(name="name") 将 name 暴露为模拟属性,同时不丢失现有行为。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- testing-qa
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 48/100