Removed ChoicesMeta.__contains__() for Python 3.12+.

In Python 3.12 it is possible to check containment using member values,
not just the members themselves.

https://docs.python.org/3/library/enum.html#enum.EnumType.__contains__
This commit is contained in:
Nick Pope 2023-08-23 12:21:23 +01:00 committed by Mariusz Felisiak
parent 64cea1e48f
commit 8aa8346466
1 changed files with 8 additions and 5 deletions

View File

@ -2,6 +2,7 @@ import enum
from types import DynamicClassAttribute
from django.utils.functional import Promise
from django.utils.version import PY312
__all__ = ["Choices", "IntegerChoices", "TextChoices"]
@ -31,11 +32,13 @@ class ChoicesMeta(enum.EnumMeta):
member._label_ = label
return enum.unique(cls)
def __contains__(cls, member):
if not isinstance(member, enum.Enum):
# Allow non-enums to match against member values.
return any(x.value == member for x in cls)
return super().__contains__(member)
if not PY312:
def __contains__(cls, member):
if not isinstance(member, enum.Enum):
# Allow non-enums to match against member values.
return any(x.value == member for x in cls)
return super().__contains__(member)
@property
def names(cls):