`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