python
type_error
ai_generated
true
AttributeError: 'dict' object has no attribute 'items'
ID: python/pytest-mock-patch-dict
80%Fix Rate
82%Confidence
0Evidence
2026-01-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
A dictionary was patched with a Mock, but the code expects a real dictionary and calls dict methods like .items().
generic中文
字典被 Mock 替换,但代码期望一个真实的字典并调用了 .items() 等字典方法。
Workarounds
-
95% success
patch.dict('module.my_dict', {'key': 'value'}) or use a simple class that implements the required methods. -
85% success
mock_dict = Mock() mock_dict.items.return_value = [('key', 'value')] patch('module.my_dict', mock_dict)
Dead Ends
Common approaches that don't work:
-
70% fail
MagicMock supports magic methods but not dict-specific methods like .items() unless configured.
-
90% fail
This defeats the purpose of the test and may cause other failures.