diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index f4ec511..284bd7c 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -943,6 +943,10 @@ cdef class _DynamicEnum: elif op == 5: # >= return left >= right + def __hash__(_DynamicEnum self): + return hash(self._as_str()) + + cdef class _DynamicEnumField: cdef _init(self, proto): self.thisptr = proto diff --git a/test/test_regression.py b/test/test_regression.py index 78f3fec..93a8a26 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -415,6 +415,12 @@ def check_all_types(reader): assert subSubReader.structField.textField == "really nested" assert subReader.enumField == "baz" + # Check that enums are hashable and can be used as keys in dicts + # interchangably with their string version. + assert hash(subReader.enumField) == hash('baz') + assert {subReader.enumField: 17}.get(subReader.enumField) == 17 + assert {subReader.enumField: 17}.get('baz') == 17 + assert {'baz': 17}.get(subReader.enumField) == 17 check_list(subReader.voidList, [None, None, None]) check_list(subReader.boolList, [False, True, False, True, True])