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

Commits on Oct 28, 2024

  1. Replace do_not_call_in_templates = True class variable in enums wit…

    …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/
    niklasmohrin committed Oct 28, 2024
    Configuration menu
    Copy the full SHA
    c9da2e9 View commit details
    Browse the repository at this point in the history