#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070191fb061087-4a59f2f4-4d03-4b7b-9cf5-197338a13915-000000%40eu-central-1.amazonses.com.

Reply via email to