Skip to content

Add start and stop parameters to the range.index() ABC method #72384

Closed as not planned

Description

@vedgar
mannequin
BPO 28197
Nosy @rhettinger, @ssbr, @serhiy-storchaka, @vedgar, @nitishch, @shreyanavigyan
PRs
  • bpo-28197: Add start and stop keywords to range.index() method #4378
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2016-09-18.20:50:32.845>
    labels = ['interpreter-core', 'easy', 'type-feature', '3.7']
    title = 'Add start and stop parameters to the range.index() ABC method'
    updated_at = <Date 2021-04-14.18:06:12.283>
    user = 'https://github.com/vedgar'

    bugs.python.org fields:

    activity = <Date 2021-04-14.18:06:12.283>
    actor = 'shreyanavigyan'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2016-09-18.20:50:32.845>
    creator = 'veky'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 28197
    keywords = ['patch', 'easy (C)']
    message_count = 12.0
    messages = ['276908', '276912', '276930', '276933', '276946', '277156', '277162', '277163', '277179', '277198', '305548', '307833']
    nosy_count = 7.0
    nosy_names = ['rhettinger', 'Devin Jeanpierre', 'docs@python', 'serhiy.storchaka', 'veky', 'nitishch', 'shreyanavigyan']
    pr_nums = ['4378']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue28197'
    versions = ['Python 3.7']

    Activity

    1. vedgar commented on Sep 18, 2016

      vedgarmannequin
      MannequinAuthor

      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. :-)

    2. added
      type-bug
      docs
      stdlib
      on Sep 18, 2016
    3. serhiy-storchaka commented on Sep 18, 2016

      @serhiy-storchaka
      Member

      Parameters start and stop were added to the Sequence.index() mixin method in bpo-23086. These broke some concrete Sequence implementations.

    4. vedgar commented on Sep 19, 2016

      vedgarmannequin
      MannequinAuthor

      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.

    5. rhettinger commented on Sep 19, 2016

      @rhettinger
      Contributor

      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.

    6. vedgar commented on Sep 19, 2016

      vedgarmannequin
      MannequinAuthor

      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.

    7. rhettinger commented on Sep 21, 2016

      @rhettinger
      Contributor

      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.

    8. added
      type-feature
      and removed
      type-bug
      on Sep 21, 2016
    9. serhiy-storchaka commented on Sep 21, 2016

      @serhiy-storchaka
      Member

      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.

    10. 1 remaining item

    11. vedgar commented on Sep 21, 2016

      vedgarmannequin
      MannequinAuthor

      Why can't this go into 3.6? To me this situation seems like a bug.

    12. rhettinger commented on Sep 22, 2016

      @rhettinger
      Contributor

      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.

    13. self-assigned this
      on Sep 22, 2016
    14. removed their assignment
      on Oct 29, 2017
    15. added
      interpreter-core
      and removed
      docs
      stdlib
      on Nov 4, 2017
    16. changed the title [-]range.index mismatch with documentation[/-] [+]Add start and stop parameters to the range.index() ABC method[/+] on Nov 4, 2017
    17. serhiy-storchaka commented on Nov 4, 2017

      @serhiy-storchaka
      Member

      Opened bpo-31942 for the documenting part.

    18. nitishch commented on Dec 8, 2017

      nitishchmannequin
      Mannequin

      Any comments on the PR?

    19. transferred this issue fromon Apr 10, 2022
    20. iritkatriel commented on Oct 30, 2022

      @iritkatriel
      Member

      The docstring was corrected in #79029 to remove start and stop. I think this issue can be closed, and if someone wants to suggest it as a feature request, they would write to python ideas first and then create a new issue.

    21. added
      pending
      on Oct 30, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)easyinterpreter-corependingtype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions