1
0
mirror of https://github.com/django/django.git synced 2025-06-05 11:39:13 +00:00

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

View File

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