From cf24a89b22a57a52b61087e489293fd5f6087b68 Mon Sep 17 00:00:00 2001 From: Niklas Mohrin Date: Sat, 9 Nov 2024 02:51:47 +0100 Subject: [PATCH] Replace `do_not_call_in_templates = True` class variable in enums with instance property (#2325) The current pattern has the following misbehavior: ```python class E(enum.Enum): do_not_call_in_templates = True FOO = 42 assert set(E.__members__) == {"FOO", "do_not_call_in_templates"} ``` The way Django fixes this in their `Choices` class is by using ```python class E(enum.Enum): FOO = 42 @property def do_not_call_in_templates(self): return True ``` This is technically not in line with the Django documentation, which instructs us to "set a do_not_call_in_templates attribute on the callable with the value True", but still works. This is because the Django code to check this is ```python getattr(E, "do_not_call_in_templates", False)` ``` which evaluates to a `` and is therefore truthy. Starting with Python 3.11, we should use ```python class E(enum.Enum): do_not_call_in_templates = enum.nonmember(True)` ``` which is also what Django's `Choices` uses if available. - https://docs.djangoproject.com/en/5.1/ref/templates/api/ --- evap/evaluation/models.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/evap/evaluation/models.py b/evap/evaluation/models.py index c7e4881fd..2ed36f21b 100644 --- a/evap/evaluation/models.py +++ b/evap/evaluation/models.py @@ -471,13 +471,16 @@ class State(models.IntegerChoices): ) class TextAnswerReviewState(Enum): - do_not_call_in_templates = True # pylint: disable=invalid-name NO_TEXTANSWERS = auto() NO_REVIEW_NEEDED = auto() REVIEW_NEEDED = auto() REVIEW_URGENT = auto() REVIEWED = auto() + @property + def do_not_call_in_templates(self): + return True + class Meta: unique_together = [ ["course", "name_de"],