summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Pope <nick@nickpope.me.uk>2023-08-23 12:21:23 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-09-12 10:17:47 +0200
commit8aa83464664938367a425f011a7df5663a955d09 (patch)
treecbc16ade70a4d1bae590a1f253023f3ae670bd40
parent64cea1e48f285ea2162c669208d95188b32bbc82 (diff)
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__
-rw-r--r--django/db/models/enums.py13
1 files 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):