classification
Title: Add start and stop parameters to the range.index() ABC method
Type: enhancement Stage: patch review
Components: Interpreter Core Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Devin Jeanpierre, docs@python, rhettinger, serhiy.storchaka, veky
Priority: normal Keywords: easy (C), patch

Created on 2016-09-18 20:50 by veky, last changed 2017-11-12 11:21 by nitishch.

Pull Requests
URL Status Linked Edit
PR 4378 open nitishch, 2017-11-12 11:21
Messages (11)
msg276908 - (view) Author: Vedran Čačić (veky) * Date: 2016-09-18 20:50
Look at this:

    >>> from collections.abc import Sequence

    >>> help(Sequence.index)
    index(self, value, start=0, stop=None)
        S.index(value, [start, [stop]]) -> integer -- return first index of value.
        Raises ValueError if the value is not present.

    >>> issubclass(range, Sequence)
    True

    >>> help(range.index)
    index(...)
        rangeobject.index(value, [start, [stop]]) -> integer -- return index of value.
        Raise ValueError if the value is not present.

So far, so good. But:

    >>> range(9).index(2, 1, 5)
    TypeError: index() takes exactly one argument (3 given)

Of course it's not essential, but the docs shouldn't lie. And if range _is_ a Sequence, then it should have the complete interface of a Sequence. Including start and end arguments for .index: they are optional from the point of call, not from the point of implementation. :-)
msg276912 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-09-18 21:03
Parameters start and stop were added to the Sequence.index() mixin method in issue23086. These broke some concrete Sequence implementations.
msg276930 - (view) Author: Vedran Čačić (veky) * Date: 2016-09-19 01:13
Yes, that's the precise reason I caught this. I was implementing some tools to do with Sequences (seqtools, like itertools but having non-ephemeral output given non-ephemeral input), and the Sequence that was easiest to test quickly was range. In `positions` (like .index but gives all the places a value appears), it raised TypeError.
msg276933 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-09-19 03:52
> These broke some concrete Sequence implementations.

Poor choice of words.  The concrete implementations didn't change at all. 

Perhaps the concrete implementation need to be brought more in-sync with the ABC.  That would be reasonable; afteralll the goal is substitutability  -- that is the only reason that the old xrange morphed into something with nearly useless count() and index() methods in the first place.
msg276946 - (view) Author: Vedran Čačić (veky) * Date: 2016-09-19 05:15
Yes, I agree these are useless _if you know you're dealing with range_. However, if you have a Sequence, it would be very useful not to have range be a special case.

Of course, one solution is to have a default .index implementation in the Sequence ABC itself, but still I'd argue that range can implement the 3-arg .index much better than generic Sequence.
msg277156 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-09-21 15:13
> I'd argue that range can implement the 3-arg .index much better 
> than generic Sequence.

Yes, the range index() method should implement all three arguments.  That shouldn't be difficult.
msg277162 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-09-21 15:28
Sorry for poor words. I was not going to blame anybody. But changing interface always is dangerous. It makes third-party classes that implemented the interface no longer valid substitutions.

I think two things are worth to be done:

1) Explicitly document (including the docstring) that the support of stop and start arguments is optional. Not all sequences implements it. 3.5+.

2) Add the support of stop and start arguments to range() in 3.7.

And would be nice to provide a way for testing if the sequence supports extended index(). If this is possible.
msg277163 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-09-21 15:36
>  2) Add the support of stop and start arguments to range() in 3.7.

Yes, the whole point of adding these bogus methods in the first place was to harmonize the range object with other sequence types.
msg277179 - (view) Author: Vedran Čačić (veky) * Date: 2016-09-21 19:17
Why can't this go into 3.6? To me this situation seems like a bug.
msg277198 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-09-22 06:38
> Why can't this go into 3.6?

Because it is a new feature and we're already past beta 1; because nothing is actually broken; and because it is a very minor issue (x isn't 100% consistent with y); because this would depend on new code and tests that we haven't written yet; and because the use case for the optional arguments is to do repeated searches and that doesn't make much sense in the context of a range object.  That said, I think it is harmless to add this to 3.7 even though it isn't very useful.
msg305548 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-11-04 09:12
Opened issue31942 for the documenting part.
History
Date User Action Args
2017-11-12 11:21:46nitishchsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request4326
2017-11-04 09:12:02serhiy.storchakasetmessages: + msg305548
2017-11-04 09:11:00serhiy.storchakasetkeywords: + easy (C)
components: + Interpreter Core, - Documentation, Library (Lib)
title: range.index mismatch with documentation -> Add start and stop parameters to the range.index() ABC method
2017-10-29 00:59:29rhettingersetassignee: rhettinger ->
2017-10-22 08:11:30serhiy.storchakasetstage: needs patch
2016-09-22 06:38:48rhettingersetassignee: rhettinger
messages: + msg277198
2016-09-21 19:17:53vekysetmessages: + msg277179
2016-09-21 15:36:36rhettingersetmessages: + msg277163
2016-09-21 15:28:54serhiy.storchakasetmessages: + msg277162
2016-09-21 15:13:00rhettingersetassignee: docs@python -> (no value)
type: behavior -> enhancement
messages: + msg277156
versions: - Python 3.5, Python 3.6
2016-09-19 05:15:53vekysetmessages: + msg276946
2016-09-19 03:52:52rhettingersetmessages: + msg276933
2016-09-19 01:13:10vekysetmessages: + msg276930
2016-09-18 21:03:48serhiy.storchakasetnosy: + rhettinger, serhiy.storchaka, Devin Jeanpierre
messages: + msg276912
2016-09-18 20:55:18SilentGhostsetversions: + Python 3.7
2016-09-18 20:50:32vekycreate