[Django] #35766: Choice iterator breaks when using slices

22 views
Skip to first unread message

Django

unread,
Sep 16, 2024, 9:29:28 AM9/16/24
to django-...@googlegroups.com
#35766: Choice iterator breaks when using slices
-----------------------+-----------------------------------------
Reporter: David | Type: Uncategorized
Status: new | Component: Uncategorized
Version: 5.0 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------+-----------------------------------------
Currently the choice-iterator classes introduced in
https://code.djangoproject.com/ticket/24561 are designed to work with
index-based access.

{{{#!python


class MyModel(models.Model):
field = models.IntegerField(choices=lambda: range(10))


the_field = MyModel._meta.get_field("field")
the_field.choices[2]
#> 3
}}}

Since choices has been out for long time accepting there are libraries in
which the `field.choices` attribute was used as it it was a tuple/list,
which can be accessed also with slicing syntax (see [https://github.com
/sphinx-doc/sphinxcontrib-
django/blob/e9d43085ffc9b921fd7d880ff38d4e1d24d8d791/sphinxcontrib_django/docstrings/attributes.py#L123-L127
sphinxcontrib-django ]), this now raises an error:

{{{#!python
the_field.choices[:2]
---------------------------------------------------------------------------
TypeError Traceback (most recent call
last)
Cell In[13], line 1
----> 1 the_field.choices[:2]

File /opt/venv/lib/python3.10/site-packages/django/utils/choices.py:24, in
BaseChoiceIterator.__getitem__(self, index)
23 def __getitem__(self, index):
---> 24 if index < 0:
25 # Suboptimally consume whole iterator to handle negative
index.
26 return list(self)[index]
27 try:
}}}


The
[https://docs.python.org/3.10/reference/datamodel.html?highlight=slicing#object.__getitem__
__getitem__] states that the management of key type should be handled in
the implementation.

It should be choosen if slices are going to be supported or if only
integers are going to be supported by this class.
--
Ticket URL: <https://code.djangoproject.com/ticket/35766>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 16, 2024, 9:32:26 AM9/16/24
to django-...@googlegroups.com
#35766: Choice iterator breaks when using slices
-------------------------------+--------------------------------------
Reporter: David | Owner: (none)
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by David:

Old description:
New description:
TypeError: '<' not supported between instances of 'slice' and 'int'
}}}


The
[https://docs.python.org/3.10/reference/datamodel.html?highlight=slicing#object.__getitem__
__getitem__] states that the management of key type should be handled in
the implementation.

It should be choosen if slices are going to be supported or if only
integers are going to be supported by this class.

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

Django

unread,
Sep 17, 2024, 6:43:35 AM9/17/24
to django-...@googlegroups.com
#35766: Choice iterator breaks when using slices
-------------------------------------+-------------------------------------
Reporter: David | Owner: Sarah
| Boyce
Type: Bug | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* cc: Natalia Bidart, Nick Pope (added)
* component: Uncategorized => Database layer (models, ORM)
* has_patch: 0 => 1
* owner: (none) => Sarah Boyce
* stage: Unreviewed => Accepted
* status: new => assigned
* type: Uncategorized => Bug

Comment:

Thank you, replicated
I imagine we do want to support it (but I might be wrong) - made a small
patch
--
Ticket URL: <https://code.djangoproject.com/ticket/35766#comment:2>

Django

unread,
Sep 18, 2024, 10:45:47 AM9/18/24
to django-...@googlegroups.com
#35766: Choice iterator breaks when using slices
-------------------------------------+-------------------------------------
Reporter: David | Owner: Sarah
| Boyce
Type: Bug | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | 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 Sarah Boyce):

* stage: Accepted => Ready for checkin

Comment:

PR received approval
--
Ticket URL: <https://code.djangoproject.com/ticket/35766#comment:3>

Django

unread,
Sep 18, 2024, 10:46:03 AM9/18/24
to django-...@googlegroups.com
#35766: Choice iterator breaks when using slices
-------------------------------------+-------------------------------------
Reporter: David | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | 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 Sarah Boyce <42296566+sarahboyce@…>):

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

Comment:

In [changeset:"ae1ee24178ecd53ac5ef3fc2055e4be8b9602ac9" ae1ee241]:
{{{#!CommitTicketReference repository=""
revision="ae1ee24178ecd53ac5ef3fc2055e4be8b9602ac9"
Fixed #35766 -- Handled slices in BaseChoiceIterator.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35766#comment:4>

Django

unread,
Sep 19, 2024, 1:56:03 AM9/19/24
to django-...@googlegroups.com
#35766: Choice iterator breaks when using slices
-------------------------------------+-------------------------------------
Reporter: David | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | 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 Claude Paroz):

Sarah, isn't this a regression in the last Django version that warrants a
backport?
--
Ticket URL: <https://code.djangoproject.com/ticket/35766#comment:5>

Django

unread,
Sep 19, 2024, 8:16:48 AM9/19/24
to django-...@googlegroups.com
#35766: Choice iterator breaks when using slices
-------------------------------------+-------------------------------------
Reporter: David | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | 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 Natalia Bidart):

Replying to [comment:5 Claude Paroz]:
> Sarah, isn't this a regression in the last Django version that warrants
a backport?

Hey Claude, thanks for keeping an eye on things!
But the feature was introduced in 5.0 so it does not qualify for a
backport at this time.
--
Ticket URL: <https://code.djangoproject.com/ticket/35766#comment:6>

Django

unread,
Sep 19, 2024, 2:22:51 PM9/19/24
to django-...@googlegroups.com
#35766: Choice iterator breaks when using slices
-------------------------------------+-------------------------------------
Reporter: David | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | 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 Claude Paroz):

Isn't the related commit 07fa79ef2bb3e8cace7bd87b292c6c85230eed05 ?
--
Ticket URL: <https://code.djangoproject.com/ticket/35766#comment:7>

Django

unread,
Sep 20, 2024, 3:13:00 AM9/20/24
to django-...@googlegroups.com
#35766: Choice iterator breaks when using slices
-------------------------------------+-------------------------------------
Reporter: David | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | 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 Sarah Boyce):

Yes you're right - I missed this. I will create a release note a back port
shortly
--
Ticket URL: <https://code.djangoproject.com/ticket/35766#comment:8>

Django

unread,
Sep 20, 2024, 3:35:54 AM9/20/24
to django-...@googlegroups.com
#35766: Choice iterator breaks when using slices
-------------------------------------+-------------------------------------
Reporter: David | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | 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 Sarah Boyce):

[https://github.com/django/django/pull/18604 PR] - have some questions as
to whether it is required for the backport
--
Ticket URL: <https://code.djangoproject.com/ticket/35766#comment:9>

Django

unread,
Sep 20, 2024, 9:02:56 AM9/20/24
to django-...@googlegroups.com
#35766: Choice iterator breaks when using slices
-------------------------------------+-------------------------------------
Reporter: David | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | 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 Natalia Bidart):

I still think that this does not qualify for a backport:
07fa79ef2bb3e8cace7bd87b292c6c85230eed05 was landed in `main` right before
the 5.0 beta release, that means during the 5.0 alpha-beta phase. Thus, it
was backported to the `stable/5.0.x` branch due to the "alpha" backporting
policy. Therefore, this commit is really a "5.0 feature" and it's my
understanding that this does not qualify for a 5.1 backport.
--
Ticket URL: <https://code.djangoproject.com/ticket/35766#comment:10>
Reply all
Reply to author
Forward
0 new messages