classification
Title: range.index doesn't support start/stop optional arguments
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Add start and stop parameters to the range.index() ABC method
View: 28197
Assigned To: Nosy List: ammar2, ktbarrett, mark.dickinson
Priority: normal Keywords:

Created on 2021-04-14 04:11 by ktbarrett, last changed 2021-04-14 18:02 by ammar2. This issue is now closed.

Messages (4)
msg391029 - (view) Author: Kaleb Barrett (ktbarrett) Date: 2021-04-14 04:11
The range builtin type is a collections.abc.Sequence, which in Python 3.5 added the optional start and stop arguments to the index method of Sequences. However, range.index does not support these optional arguments required by Sequence.

I noticed this when creating a wrapper type around range which was a  Sequence. mypy complained 'Signature of "index" incompatible with supertype "Sequence"', but attempting to pass the values raised 'TypeError: index() takes exactly one argument (3 given)'.
msg391086 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-04-14 15:00
Duplicate of #28197?
msg391089 - (view) Author: Kaleb Barrett (ktbarrett) Date: 2021-04-14 16:31
And so it is... There is also a PR open with a solution from 2017. 
https://github.com/python/cpython/pull/4378/files. Can this get reviewed?

The rationalizations against solving this bug stated in the original issue are weak, range.index *is* useful. I use range objects to describe alternate indexing schemes for an array type (to model HDL datatypes that allow arbitrary indexing schemes). range.index is used to translate user supplied indexes into 0-based indexes to index into a parallel list with the array values.
https://github.com/cocotb/cocotb/pull/2510/files#diff-62a4545e5bbb9291f2bdb820609b2d68c69cbafe64faea83beb380d01c02fb5aR315-R319

Additionally, this causes issues with mypy. If you subclass Sequence, mypy will complain if you don't have the optional start and stop arguments in the index method. I would need to feed them into a range object in my implementation. I guess in my case I'm expected to just reject them? This breaks substitutability in my class.

It also breaks substitutability with list, tuples, every built-in Sequence type as well.
msg391091 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2021-04-14 18:02
Marked as a duplicate.

Kaleb, would you mind posting your comment on the original bug #28197?
History
Date User Action Args
2021-04-14 18:02:32ammar2setstatus: open -> closed

superseder: Add start and stop parameters to the range.index() ABC method

nosy: + ammar2
messages: + msg391091
resolution: duplicate
stage: resolved
2021-04-14 16:31:35ktbarrettsetmessages: + msg391089
2021-04-14 15:00:20mark.dickinsonsetnosy: + mark.dickinson
messages: + msg391086
2021-04-14 04:11:46ktbarrettcreate