From 8aa83464664938367a425f011a7df5663a955d09 Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Wed, 23 Aug 2023 12:21:23 +0100 Subject: [PATCH] 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__ --- django/db/models/enums.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/django/db/models/enums.py b/django/db/models/enums.py index 9a7a2bb70f..94fae4d2f7 100644 --- a/django/db/models/enums.py +++ b/django/db/models/enums.py @@ -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):