Skip to content

Commit 4560269

Browse files
committed
minor updates
1 parent 650b70a commit 4560269

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

Lib/enum.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -799,19 +799,16 @@ def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, s
799799
boundary=boundary,
800800
)
801801

802-
def __contains__(cls, member):
803-
"""Return True if `member` is in `cls`.
802+
def __contains__(cls, value):
803+
"""Return True if `value` is in `cls`.
804804
805-
`member` is in `cls` iff:
806-
1) `member` is a proper member of the `cls` enum, or
807-
2) `member` is the value of a member of the `cls` enum.
808-
809-
Beware that 2) can lead to some confusion if members of different
810-
enums have the same value.
805+
`value` is in `cls` if:
806+
1) `value` is a member of `cls`, or
807+
2) `value` is the value of one of the `cls`'s members.
811808
"""
812-
if isinstance(member, cls):
809+
if isinstance(value, cls):
813810
return True
814-
return member in cls._value2member_map_ or member in cls._unhashable_values_
811+
return value in cls._value2member_map_ or value in cls._unhashable_values_
815812

816813
def __delattr__(cls, attr):
817814
# nicer error message when someone tries to delete an attribute

Lib/test/test_enum.py

+24-14
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,7 @@ def test_changing_member_fails(self):
346346
def test_contains_tf(self):
347347
MainEnum = self.MainEnum
348348
self.assertIn(MainEnum.first, MainEnum)
349-
if type(self) is TestMinimalDate or type(self) is TestMixedDate:
350-
self.assertTrue(date(*self.source_values[0]) in MainEnum)
351-
else:
352-
self.assertTrue(self.source_values[0] in MainEnum)
349+
self.assertTrue(self.values[0] in MainEnum)
353350
if type(self) is not TestStrEnum:
354351
self.assertFalse('first' in MainEnum)
355352
val = MainEnum.dupe
@@ -359,21 +356,37 @@ class OtherEnum(Enum):
359356
one = auto()
360357
two = auto()
361358
self.assertNotIn(OtherEnum.two, MainEnum)
359+
#
360+
if MainEnum._member_type_ is object:
361+
# enums without mixed data types will always be False
362+
class NotEqualEnum(self.enum_type):
363+
this = self.source_values[0]
364+
that = self.source_values[1]
365+
self.assertNotIn(NotEqualEnum.this, MainEnum)
366+
self.assertNotIn(NotEqualEnum.that, MainEnum)
367+
else:
368+
# enums with mixed data types may be True
369+
class EqualEnum(self.enum_type):
370+
this = self.source_values[0]
371+
that = self.source_values[1]
372+
self.assertIn(EqualEnum.this, MainEnum)
373+
self.assertIn(EqualEnum.that, MainEnum)
362374

363375
def test_contains_same_name_diff_enum_diff_values(self):
364376
MainEnum = self.MainEnum
377+
#
365378
class OtherEnum(Enum):
366379
first = "brand"
367380
second = "new"
368381
third = "values"
369-
382+
#
370383
self.assertIn(MainEnum.first, MainEnum)
371384
self.assertIn(MainEnum.second, MainEnum)
372385
self.assertIn(MainEnum.third, MainEnum)
373386
self.assertNotIn(MainEnum.first, OtherEnum)
374387
self.assertNotIn(MainEnum.second, OtherEnum)
375388
self.assertNotIn(MainEnum.third, OtherEnum)
376-
389+
#
377390
self.assertIn(OtherEnum.first, OtherEnum)
378391
self.assertIn(OtherEnum.second, OtherEnum)
379392
self.assertIn(OtherEnum.third, OtherEnum)
@@ -4049,15 +4062,12 @@ class Color(enum.Enum)
40494062
| ----------------------------------------------------------------------
40504063
| Methods inherited from enum.EnumType:
40514064
|\x20\x20
4052-
| __contains__(member) from enum.EnumType
4053-
| Return True if `member` is in `cls`.
4054-
|
4055-
| `member` is in `cls` iff:
4056-
| 1) `member` is a proper member of the `cls` enum, or
4057-
| 2) `member` is the value of a member of the `cls` enum.
4065+
| __contains__(value) from enum.EnumType
4066+
| Return True if `value` is in `cls`.
40584067
|
4059-
| Beware that 2) can lead to some confusion if members of different
4060-
| enums have the same value.
4068+
| `value` is in `cls` if:
4069+
| 1) `value` is a member of `cls`, or
4070+
| 2) `value` is the value of one of the `cls`'s members.
40614071
|\x20\x20
40624072
| __getitem__(name) from enum.EnumType
40634073
| Return the member matching `name`.

0 commit comments

Comments
 (0)