python
attribute_error
ai_generated
true
AttributeError: <module 'myapp' from '...'> does not have attribute 'config'
ID: python/unittest-mock-patch-object-attribute
80%Fix Rate
86%Confidence
0Evidence
2025-10-12First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
Root Cause
在 patch.object 中指定了不存在的属性,或模块被错误导入。
generic中文
在 patch.object 中指定了不存在的属性,或模块被错误导入。
Workarounds
-
90% success
在测试前 `hasattr(myapp, 'config')`,确保存在再 patch。
-
85% success
`patch('myapp.config')` 如果属性在模块级别。
Dead Ends
Common approaches that don't work:
-
70% fail
修改生产代码,且可能不需要。
-
60% fail
patch 需要字符串路径,可能同样出错。