[Django] #32460: TextChoices/IntegerChoices can not have a member with name `label`

181 views
Skip to first unread message

Django

unread,
Feb 19, 2021, 1:33:47 AM2/19/21
to django-...@googlegroups.com
#32460: TextChoices/IntegerChoices can not have a member with name `label`
-------------------------------------+-------------------------------------
Reporter: elonzh | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 3.0
layer (models, ORM) |
Severity: Normal | Keywords: Choices
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
{{{
import enum
from django.db.models import TextChoices, IntegerChoices


class A(enum.Enum):
label = "label"


print("case A, builtin Enum type:", A.label)


class B(TextChoices):
LABEL = "label"


print("case B, TextChoices with a choice `LABEL`:", B.LABEL)

try:
class C(TextChoices):
label = "label"

except Exception as e:
print("case C, TextChoices with a choice `label`:", e)


try:
class D(IntegerChoices):
label = 0

except Exception as e:
print("case D, IntegerChoices with a choice `label`:", e)

}}}


output:


{{{
case A, builtin Enum type: A.label
case B, TextChoices with a choice `LABEL`: label
case C, TextChoices with a choice `label`: Cannot reassign members.
case D, IntegerChoices with a choice `label`: Cannot reassign members.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32460>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 19, 2021, 9:35:54 AM2/19/21
to django-...@googlegroups.com
#32460: TextChoices/IntegerChoices can not have a member with name `label`
-------------------------------------+-------------------------------------
Reporter: elonzh | Owner: starryrbs
Type: Bug | Status: assigned
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: Choices | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by starryrbs):

* owner: nobody => starryrbs
* status: new => assigned


Comment:

I checked the source code of django, in django.db.models.enums path:

{{{
cls.label = property(lambda self: cls._value2label_map_.get(self.value))
cls.do_not_call_in_templates = True
}}}
{{{label}}} and {{{do_not_call_in_templates}}} cannot be used as enum
keys.
We may need to add this to the Django document or change the label to
configurable.
I submitted a pull request to support this feature.
[https://github.com/django/django/pull/14020]

--
Ticket URL: <https://code.djangoproject.com/ticket/32460#comment:1>

Django

unread,
Feb 19, 2021, 12:23:00 PM2/19/21
to django-...@googlegroups.com
#32460: TextChoices/IntegerChoices can not have a member with name `label`
-------------------------------------+-------------------------------------
Reporter: elonzh | Owner: Nick Pope

Type: Bug | Status: assigned
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: Choices | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Nick Pope):

* owner: starryrbs => Nick Pope
* has_patch: 0 => 1
* stage: Unreviewed => Accepted


Comment:

Hi. Thanks for the report.

So looking at this, I don't think that making it possible to customise the
`.label` property name is the right approach.

`Enum` doesn't have a problem providing `.name` or `.value` attributes on
the members while still allowing those names to be used as member names.
I have something sorted out that will fix this for `.label` and
`.do_not_call_in_templates` (although the latter is rather niche!)

This is also broken for `.names`, `.values`, `.labels`, and `.choices`.
These are, however, probably not fixable as they must exist on the top-
level class itself and not the members, so they would collide with the
member names. Perhaps this part of the problem can be addressed with some
documentation.

Alternate [https://github.com/django/django/pull/14022 PR].

--
Ticket URL: <https://code.djangoproject.com/ticket/32460#comment:2>

Django

unread,
Feb 19, 2021, 9:24:07 PM2/19/21
to django-...@googlegroups.com
#32460: TextChoices/IntegerChoices can not have a member with name `label`
-------------------------------------+-------------------------------------
Reporter: elonzh | Owner: Nick Pope
Type: Bug | Status: assigned
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: Choices | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by elonzh):

Replying to [comment:2 Nick Pope]:


> Hi. Thanks for the report.
>
> So looking at this, I don't think that making it possible to customise
the `.label` property name is the right approach.
>
> `Enum` doesn't have a problem providing `.name` or `.value` attributes
on the members while still allowing those names to be used as member
names.
> I have something sorted out that will fix this for `.label` and
`.do_not_call_in_templates` (although the latter is rather niche!)
>
> This is also broken for `.names`, `.values`, `.labels`, and `.choices`.
These are, however, probably not fixable as they must exist on the top-
level class itself and not the members, so they would collide with the
member names. Perhaps this part of the problem can be addressed with some
documentation.
>
> Alternate [https://github.com/django/django/pull/14022 PR].

using methods to get such members? like `get_labels()`, these methods
introduce API changes but I think it's a clear solution?

--
Ticket URL: <https://code.djangoproject.com/ticket/32460#comment:3>

Django

unread,
Feb 20, 2021, 5:40:40 AM2/20/21
to django-...@googlegroups.com
#32460: TextChoices/IntegerChoices can not have a member with name `label`
-------------------------------------+-------------------------------------
Reporter: elonzh | Owner: Nick Pope
Type: Bug | Status: assigned
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: Choices | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Nick Pope):

Replying to [comment:3 elonzh]:


> using methods to get such members? like `get_labels()`, these methods
introduce API changes but I think it's a clear solution?

Every example in the [https://docs.python.org/3/library/enum.html enum]
documentation and the
[https://docs.djangoproject.com/en/3.1/ref/models/fields/#field-choices-
enum-types Field.choices] documentation uses the convention of uppercase
member names. Maybe I was naive to assume that people wouldn't use
lowercase member names.

While we could rename these, I'm not sure it is worth the change. We'd
need to deprecate first for a few releases. It will annoy lots of people
who will then have to update this for a small edge case. On top of that,
the names `get_labels`, etc. would also then not be allowed, merely
shifting the problem. At the end of the day, these choices helper classes
are not compulsory and, as documented, a sequence can be used.

--
Ticket URL: <https://code.djangoproject.com/ticket/32460#comment:4>

Django

unread,
Mar 23, 2021, 5:27:59 AM3/23/21
to django-...@googlegroups.com
#32460: TextChoices/IntegerChoices can not have a member with name `label`
-------------------------------------+-------------------------------------
Reporter: elonzh | Owner: Nick Pope
Type: Bug | Status: assigned
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: Choices | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/32460#comment:5>

Django

unread,
Mar 24, 2021, 4:14:08 AM3/24/21
to django-...@googlegroups.com
#32460: TextChoices/IntegerChoices can not have a member with name `label`
-------------------------------------+-------------------------------------
Reporter: elonzh | Owner: Nick Pope
Type: Bug | Status: assigned
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: Choices | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"41e39c41c9225bc3cbd5f333694e0a4d113136be" 41e39c41]:
{{{
#!CommitTicketReference repository=""
revision="41e39c41c9225bc3cbd5f333694e0a4d113136be"
Refs #32460 -- Doc'd and tested that property names of model choice enums
cannot be used as members.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32460#comment:6>

Django

unread,
Mar 24, 2021, 4:14:09 AM3/24/21
to django-...@googlegroups.com
#32460: TextChoices/IntegerChoices can not have a member with name `label`
-------------------------------------+-------------------------------------
Reporter: elonzh | Owner: Nick Pope
Type: Bug | Status: closed

Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: Choices | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"a96c730431196b119559bbb18a0e85e6ee8b2597" a96c7304]:
{{{
#!CommitTicketReference repository=""
revision="a96c730431196b119559bbb18a0e85e6ee8b2597"
Fixed #32460 -- Allowed "label"/"do_not_call_in_templates" members in
model choice enums.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32460#comment:7>

Reply all
Reply to author
Forward
0 new messages