Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-88123: Implement new Enum __contains__ #93298

Merged
merged 2 commits into from
Jun 22, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
minor updates
  • Loading branch information
ethanfurman committed Jun 22, 2022

Unverified

This user has not yet uploaded their public signing key.
commit 4560269580dcc31425ad73efd71de3cda137dba2
17 changes: 7 additions & 10 deletions Lib/enum.py
Original file line number Diff line number Diff line change
@@ -799,19 +799,16 @@ def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, s
boundary=boundary,
)

def __contains__(cls, member):
"""Return True if `member` is in `cls`.
def __contains__(cls, value):
"""Return True if `value` is in `cls`.

`member` is in `cls` iff:
1) `member` is a proper member of the `cls` enum, or
2) `member` is the value of a member of the `cls` enum.

Beware that 2) can lead to some confusion if members of different
enums have the same value.
`value` is in `cls` if:
1) `value` is a member of `cls`, or
2) `value` is the value of one of the `cls`'s members.
"""
if isinstance(member, cls):
if isinstance(value, cls):
return True
return member in cls._value2member_map_ or member in cls._unhashable_values_
return value in cls._value2member_map_ or value in cls._unhashable_values_

def __delattr__(cls, attr):
# nicer error message when someone tries to delete an attribute
38 changes: 24 additions & 14 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
@@ -346,10 +346,7 @@ def test_changing_member_fails(self):
def test_contains_tf(self):
MainEnum = self.MainEnum
self.assertIn(MainEnum.first, MainEnum)
if type(self) is TestMinimalDate or type(self) is TestMixedDate:
self.assertTrue(date(*self.source_values[0]) in MainEnum)
else:
self.assertTrue(self.source_values[0] in MainEnum)
self.assertTrue(self.values[0] in MainEnum)
if type(self) is not TestStrEnum:
self.assertFalse('first' in MainEnum)
val = MainEnum.dupe
@@ -359,21 +356,37 @@ class OtherEnum(Enum):
one = auto()
two = auto()
self.assertNotIn(OtherEnum.two, MainEnum)
#
if MainEnum._member_type_ is object:
# enums without mixed data types will always be False
class NotEqualEnum(self.enum_type):
this = self.source_values[0]
that = self.source_values[1]
self.assertNotIn(NotEqualEnum.this, MainEnum)
self.assertNotIn(NotEqualEnum.that, MainEnum)
else:
# enums with mixed data types may be True
class EqualEnum(self.enum_type):
this = self.source_values[0]
that = self.source_values[1]
self.assertIn(EqualEnum.this, MainEnum)
self.assertIn(EqualEnum.that, MainEnum)

def test_contains_same_name_diff_enum_diff_values(self):
MainEnum = self.MainEnum
#
class OtherEnum(Enum):
first = "brand"
second = "new"
third = "values"

#
self.assertIn(MainEnum.first, MainEnum)
self.assertIn(MainEnum.second, MainEnum)
self.assertIn(MainEnum.third, MainEnum)
self.assertNotIn(MainEnum.first, OtherEnum)
self.assertNotIn(MainEnum.second, OtherEnum)
self.assertNotIn(MainEnum.third, OtherEnum)

#
self.assertIn(OtherEnum.first, OtherEnum)
self.assertIn(OtherEnum.second, OtherEnum)
self.assertIn(OtherEnum.third, OtherEnum)
@@ -4049,15 +4062,12 @@ class Color(enum.Enum)
| ----------------------------------------------------------------------
| Methods inherited from enum.EnumType:
|\x20\x20
| __contains__(member) from enum.EnumType
| Return True if `member` is in `cls`.
|
| `member` is in `cls` iff:
| 1) `member` is a proper member of the `cls` enum, or
| 2) `member` is the value of a member of the `cls` enum.
| __contains__(value) from enum.EnumType
| Return True if `value` is in `cls`.
|
| Beware that 2) can lead to some confusion if members of different
| enums have the same value.
| `value` is in `cls` if:
| 1) `value` is a member of `cls`, or
| 2) `value` is the value of one of the `cls`'s members.
|\x20\x20
| __getitem__(name) from enum.EnumType
| Return the member matching `name`.