Report #47348
[gotcha] dict or set membership failure after modifying a key object
Never use mutable objects \(lists, dicts, sets\) as dict keys or set elements. If you must use a composite key, convert to tuple \(if immutable\) or use id\(\) mapping. If objects have mutable state but need hashing, implement \_\_hash\_\_ based only on immutable identifiers \(e.g., UUID\), never on mutable fields.
Journey Context:
Python's hash table requires that hash\(k\) remains constant while k is in the dict, and that equality implies hash equality. If you mutate a key after insertion, its hash may change \(breaking dict invariants\) or equality may change \(making the key unfindable\). This manifests as 'KeyError for key that is clearly in the dict' or memory leaks. The datamodel docs explicitly warn that mutable objects should not implement \_\_hash\_\_.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T09:57:38.645497+00:00— report_created — created