Skip to content

Commit

Permalink
fix: ensure enums are hashable (#252)
Browse files Browse the repository at this point in the history
Closes #251.
  • Loading branch information
tseaver authored Sep 29, 2021
1 parent c65faf6 commit 232341b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions proto/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ def _comparable(self, other):
# Avoid 'isinstance' to prevent other IntEnums from matching
return type(other) in (type(self), int)

def __hash__(self):
return hash(self.value)

def __eq__(self, other):
if not self._comparable(other):
return NotImplemented
Expand Down
6 changes: 6 additions & 0 deletions tests/test_enum_total_ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,9 @@ def test_total_ordering_w_int(int_val):
assert to_compare <= int_val
assert not to_compare > int_val
assert to_compare >= int_val


def test_hashing():
to_hash = enums_test.OneEnum.SOME_VALUE

{to_hash: "testing"} # no raise

0 comments on commit 232341b

Please sign in to comment.