python
runtime_error
ai_generated
true
AssertionError: assert 'key' in {'a': 1}
ID: python/assertionerror-assert-in-dict
80%Fix Rate
85%Confidence
0Evidence
2024-08-30First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
Root Cause
The key is not present in the dictionary, causing the 'in' operator to return False.
generic中文
键不在字典中,导致 'in' 运算符返回 False。
Workarounds
-
95% success Add the key to the dictionary before assertion
d = {'a': 1}; d['key'] = 'value'; assert 'key' in d -
90% success Use assertIn for better error messages
self.assertIn('key', {'a': 1})
Dead Ends
Common approaches that don't work:
-
Checking with .get() and ignoring None
60% fail
.get() returns None for missing keys, which may mask the absence.
-
Using assertEqual with dict.keys()
40% fail
dict.keys() returns a view; comparison may be misleading.