Repository navigation
[mypyc] Allow class with only non-eq comparison methods to be hashable - #22347
Merged
Merged
Conversation
…shable Fixes mypyc/mypyc#1173. A native class that defines `__lt__` (or another comparison method other than `__eq__`) without `__hash__` was unhashable when compiled: ```python class C: def __lt__(self, other: C) -> bool: return True hash(C()) # TypeError: unhashable type: 'C' ``` Only defining `__eq__` should make a class unhashable. CPython doesn't inherit `tp_hash` if a type defines `tp_richcompare`, and this happens for all comparison methods. Now we explicitly use the default `object` hash function in this case, unless `__eq__` is also defined.
p-sawicki
approved these changes
Nov 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes mypyc/mypyc#1173.
A native class that defines
__lt__(or another comparison method other than__eq__) without__hash__was unhashable when compiled:Only defining
__eq__should make a class unhashable. CPython doesn't inherittp_hashif a type definestp_richcompare, and this happens for all comparison methods. Now we explicitly use the defaultobjecthash function in this case, unless__eq__is also defined.