Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace do_not_call_in_templates = True class variable in enums with instance property #2325

Conversation

niklasmohrin
Copy link
Member

@niklasmohrin niklasmohrin commented Oct 28, 2024

The current pattern has the following misbehavior:

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

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

getattr(E, "do_not_call_in_templates", False)

which evaluates to a <property object ...> and is therefore truthy.

Starting with Python 3.11, we should use

class E(enum.Enum):
    do_not_call_in_templates = enum.nonmember(True)`

which is also what Django's Choices uses if available.

…h instance property

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 `<property object ...>` 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/
Copy link
Collaborator

@Kakadus Kakadus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(as to why codecov won't cover this: django actually never executes the the property, it just checks if the attribute do_not_call_in_template (now a property object) is truthy)

@niklasmohrin
Copy link
Member Author

By the way, I feel that this could be a lint for the Django ruleset of pylint of ruff or so 🤔

Copy link
Member

@richardebeling richardebeling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@niklasmohrin can you keep track of the python3.11 change to nonmember so this doesn't get lost?

@niklasmohrin niklasmohrin merged commit cf24a89 into e-valuation:main Nov 9, 2024
11 of 12 checks passed
@niklasmohrin niklasmohrin deleted the enum-member-do-not-call-in-templates branch November 9, 2024 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants