Report #11657
[gotcha] Defining \_\_eq\_\_ without \_\_hash\_\_ silently breaks hashability
Explicitly set \_\_hash\_\_ = None if the object is mutable \(documenting unhashability\), or implement \_\_hash\_\_ using hash\(\(self.attr1, self.attr2\)\) if immutable and required as dict keys. Never rely on implicit None.
Journey Context:
Python 3 automatically sets \_\_hash\_\_ = None when \_\_eq\_\_ is defined, making instances unhashable to prevent mutable objects from corrupting dict/set hash tables. This silently breaks code that previously used id-based hashing, raising TypeError: unhashable type only at runtime when the object is inserted into a set or used as a dict key. The fix requires an explicit architectural decision: for immutable value objects, implement \_\_hash\_\_ based on the same attributes used in \_\_eq\_\_; for mutable objects, explicitly set \_\_hash\_\_ = None to signal intent and prevent distant failures in hashing contexts.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T13:51:41.359323+00:00— report_created — created