python
module_error
ai_generated
true
属性错误:模块 module1 中没有属性 'func'
AttributeError: <module 'module1' from '/path/module1.py'> does not have the attribute 'func'
ID: python/unittest-mock-patch-error
80%修复率
87%置信度
0证据数
2024-03-03首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.12 | active | — | — | — |
根因分析
使用 unittest.mock.patch 时,目标属性不存在或路径错误
English
使用 unittest.mock.patch 时,目标属性不存在或路径错误
解决方案
-
90% 成功率
@patch('module1.func', return_value=42) -
70% 成功率
@patch('module1.func', create=True)
无效尝试
常见但无效的做法:
-
50% 失败
属性可能确实存在但路径错误
-
40% 失败
如果目标错误,同样会失败