summaryrefslogtreecommitdiffstats
path: root/Lib/enum.py
diff options
context:
space:
mode:
authorCarl Bordum Hansen <carl@bordum.dk>2022-06-22 07:04:04 (GMT)
committerGitHub <noreply@github.com>2022-06-22 07:04:04 (GMT)
commit9a479c3c1063f434629a1f7553f5a4715d738f30 (patch)
treec7923f18e67f7ed4782d328b62b4d77d1ff4c261 /Lib/enum.py
parent6575841266b83f3121c188695c7513e551ade034 (diff)
downloadcpython-9a479c3c1063f434629a1f7553f5a4715d738f30.zip
cpython-9a479c3c1063f434629a1f7553f5a4715d738f30.tar.gz
cpython-9a479c3c1063f434629a1f7553f5a4715d738f30.tar.bz2
gh-88123: Implement new Enum __contains__ (GH-93298)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
Diffstat (limited to 'Lib/enum.py')
-rw-r--r--Lib/enum.py26
1 files changed, 8 insertions, 18 deletions
diff --git a/Lib/enum.py b/Lib/enum.py
index 20fad97..decb601 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -799,26 +799,16 @@ class EnumType(type):
boundary=boundary,
)
- def __contains__(cls, member):
- """
- Return True if member is a member of this enum
- raises TypeError if member is not an enum member
+ def __contains__(cls, value):
+ """Return True if `value` is in `cls`.
- note: in 3.12 TypeError will no longer be raised, and True will also be
- returned if member is the value of a member in this enum
+ `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 not isinstance(member, Enum):
- import warnings
- warnings.warn(
- "in 3.12 __contains__ will no longer raise TypeError, but will return True or\n"
- "False depending on whether the value is a member or the value of a member",
- DeprecationWarning,
- stacklevel=2,
- )
- raise TypeError(
- "unsupported operand type(s) for 'in': '%s' and '%s'" % (
- type(member).__qualname__, cls.__class__.__qualname__))
- return isinstance(member, cls) and member._name_ in cls._member_map_
+ if isinstance(value, cls):
+ return True
+ 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