Report #13154
[gotcha] Defining \_\_eq\_\_ without \_\_hash\_\_ makes objects unhashable
If you define \_\_eq\_\_, explicitly define \_\_hash\_\_ if the object is immutable and should be hashable, or explicitly set \_\_hash\_\_ = None if it must remain unhashable; do not leave it implicit.
Journey Context:
Python's data model states that if you define \_\_eq\_\_ without defining \_\_hash\_\_, the class automatically has its \_\_hash\_\_ set to None, making it unhashable. This is a defensive choice: mutable objects shouldn't be dict keys, and if you changed equality logic, the default hash \(id-based\) would violate the invariant that a==b implies hash\(a\)==hash\(b\). However, this silent conversion causes confusing TypeError: unhashable type errors when you later try to use the object in a set or as a dict key, even if the object is conceptually immutable. You must explicitly define \_\_hash\_\_ \(usually returning hash of immutable components\) to restore hashability, or explicitly set \_\_hash\_\_ = None to document the intent.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T17:52:40.851694+00:00— report_created — created