﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35766	Choice iterator breaks when using slices	David		"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:
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."	Uncategorized	new	Uncategorized	5.0	Normal				Unreviewed	0	0	0	0	0	0
