Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,9 +777,14 @@ def __contains__(cls, value):
except ValueError:
pass
try:
return value in cls._hashable_values_ # a set
if value in cls._hashable_values_: # a set
return True
except TypeError:
return value in cls._unhashable_values_ # a list
# an unhashable value can equal a hashable one, e.g. bytearray
for v in cls._hashable_values_:
if v == value:
return True
return value in cls._unhashable_values_ # a list

def __delattr__(cls, attr):
# nicer error message when someone tries to delete an attribute
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -3530,6 +3530,23 @@ class Directions(Enum):
UNRESTRICTED = frozenset({"sc", "cs"})
self.assertIs(Directions({"sc"}), Directions.DOWN_ONLY)

def test_contains_equal_value_of_other_hashability(self):
class Sets(Enum):
A = {1, 2}
class Magic(Enum):
PNG = b'\x89PNG'
class BytesMagic(bytes, Enum):
PNG = b'\x89PNG'
self.assertIn(Sets.A.value, Sets)
self.assertIn({1, 2}, Sets)
self.assertIn(frozenset({1, 2}), Sets)
self.assertNotIn({1, 3}, Sets)
self.assertNotIn(frozenset({1, 3}), Sets)
for enum_class in Magic, BytesMagic:
self.assertIn(bytearray(b'\x89PNG'), enum_class)
self.assertNotIn(bytearray(b'GIF8'), enum_class)
self.assertIs(enum_class(bytearray(b'\x89PNG')), enum_class.PNG)


class TestOrder(unittest.TestCase):
"test usage of the `_order_` attribute"
Expand Down
Loading