Skip to content

list.index does not accept None as start or stop #57549

Description

@cfbolz
mannequin
BPO 13340
Nosy @rhettinger, @amauryfa, @mdickinson, @cfbolz, @alex, @bitdancer, @asmeurer, @akheron, @berkerpeksag, @serhiy-storchaka, @iritkatriel

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 = <Date 2020-09-16.07:15:26.001>
created_at = <Date 2011-11-04.09:23:03.449>
labels = ['interpreter-core', 'type-bug']
title = 'list.index does not accept None as start or stop'
updated_at = <Date 2020-09-16.07:15:25.999>
user = 'https://github.com/cfbolz'

bugs.python.org fields:

activity = <Date 2020-09-16.07:15:25.999>
actor = 'serhiy.storchaka'
assignee = 'none'
closed = True
closed_date = <Date 2020-09-16.07:15:26.001>
closer = 'serhiy.storchaka'
components = ['Interpreter Core']
creation = <Date 2011-11-04.09:23:03.449>
creator = 'Carl.Friedrich.Bolz'
dependencies = []
files = []
hgrepos = []
issue_num = 13340
keywords = []
message_count = 19.0
messages = ['147000', '147108', '147111', '147113', '147114', '147116', '147117', '147120', '147121', '147151', '147153', '147162', '147163', '147171', '147172', '228937', '263582', '376958', '376977']
nosy_count = 12.0
nosy_names = ['rhettinger', 'amaury.forgeotdarc', 'mark.dickinson', 'Carl.Friedrich.Bolz', 'alex', 'r.david.murray', 'Aaron.Meurer', 'python-dev', 'petri.lehtinen', 'berker.peksag', 'serhiy.storchaka', 'iritkatriel']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue13340'
versions = ['Python 2.7', 'Python 3.5', 'Python 3.6']

Activity

  1. cfbolz commented on Nov 4, 2011

    cfbolzmannequin
    MannequinAuthor

    The list.index method does not accept None as start and stop, which makes the error message quite confusing:

    >>> [1, 2, 3].index(2, None, None)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: slice indices must be integers or None or have an __index__ method

    I checked this in 3.2.2 and 2.7.2. Seems similar to bpo-12163.

  2. added
    type-bugAn unexpected behavior, bug, or error
    on Nov 4, 2011
  3. akheron commented on Nov 5, 2011

    @akheron
    Member

    The same issue exists for tuples:

    >>> (1, 2, 3).index(2, None)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: slice indices must be integers or None or have an __index__ method
  4. python-dev commented on Nov 5, 2011

    python-devmannequin
    Mannequin

    New changeset 0f0eda4daac7 by Petri Lehtinen in branch '2.7':
    Accept None as start and stop parameters for list.index() and tuple.index()
    http://hg.python.org/cpython/rev/0f0eda4daac7

    New changeset 5c1fcaf3cf1c by Petri Lehtinen in branch '3.2':
    Accept None as start and stop parameters for list.index() and tuple.index()
    http://hg.python.org/cpython/rev/5c1fcaf3cf1c

    New changeset c33aa14f4edb by Petri Lehtinen in branch 'default':
    Accept None as start and stop parameters for list.index() and tuple.index().
    http://hg.python.org/cpython/rev/c33aa14f4edb

  5. akheron commented on Nov 5, 2011

    @akheron
    Member

    Committed a fix for both list and tuple. I considered this a bug fix rather than a new feature based on discussion in bpo-11828, especially msg133532, and applied the fix to 2.7 and 3.2, too.

  6. rhettinger commented on Nov 5, 2011

    @rhettinger
    Contributor

    The relevant code is in _PyEval_SliceIndex() in Python/ceval.c.

  7. rhettinger commented on Nov 5, 2011

    @rhettinger
    Contributor

    I think this "fix" was too hastily committed.

    1. It was an API change.
    2. It probably should have been done in _PyEval_SliceIndex().

    Be careful. Don't rush to commit. Especially for backports.

  8. akheron commented on Nov 5, 2011

    @akheron
    Member
    1. It probably should have been done in _PyEval_SliceIndex().

    I saw that other code used the same approach as I used in the fix.

    The comment above _PyEval_SliceIndex() suggests it's used in other
    contexts too. It seems that 2.7 uses it to implement the SLICE opcode,
    while in 3.x it's only used to convert slice-like arguments.

    What do you suggest? Doing it in _PyEval_SliceIndex() in 2.7 is
    problematic, as we don't want x[None:2], right? :)

  9. rhettinger commented on Nov 5, 2011

    @rhettinger
    Contributor

    The API in 2.7 shouldn't be changed.
    The error message can be fixed though.

    Also, it is not clear that the API should change even in 3.3. The list.index() method is not required to accept None. It is not different than other APIs that use PyArg_ParseTuple() with an "i" field for a start, stop, or step argument.

  10. amauryfa commented on Nov 5, 2011

    @amauryfa
    Contributor

    str.index does accept None, though

  11. mdickinson commented on Nov 6, 2011

    @mdickinson
    Member

    What do you suggest? Doing it in _PyEval_SliceIndex() in 2.7 is
    problematic, as we don't want x[None:2], right? :)

    Eh? Don't we already have this?

    Python 2.7.2 (default, Aug 22 2011, 13:53:27) 
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> range(5)[None:2]
    [0, 1]

    Or am I misunderstanding?

  12. akheron commented on Nov 6, 2011

    @akheron
    Member

    Or am I misunderstanding?

    Ah, no, sorry. I wasn't aware of this.

    Now the error message set by _PyEval_SliceIndex() makes sense. It doesn't itself accept None, but apply_slice() and assign_slice() handle the None case.

    There's still the question whether {list,tuple}.index() should accept None or not.

  13. rhettinger commented on Nov 6, 2011

    @rhettinger
    Contributor

    There's still the question whether {list,tuple}.index() should accept None or not.

    The API should not be changed for Py2.7 and Py3.2. Those changesets should be reverted.

    For Py3.3, it is open to discussion, but we probably don't need the change (making every other implementation also change for nearly zero benefit).

  14. rhettinger commented on Nov 6, 2011

    @rhettinger
    Contributor

    One other thought: the API for list.index() doesn't exist in isolation. There is also str.index, the sequence abstract base class, and tons of code that has been written to emulate lists. This is an ancient API (approx 20 years) and should only be changed with care.

    IOW, I don't think this change should have been made at all, at least not without a discussion on python-dev and motivating use cases.

  15. python-dev commented on Nov 6, 2011

    python-devmannequin
    Mannequin

    New changeset 19ffa12ffdd4 by Petri Lehtinen in branch '2.7':
    Revert "Accept None as start and stop parameters for list.index() and tuple.index()"
    http://hg.python.org/cpython/rev/19ffa12ffdd4

    New changeset ed0e85efac47 by Petri Lehtinen in branch '3.2':
    Revert "Accept None as start and stop parameters for list.index() and tuple.index()"
    http://hg.python.org/cpython/rev/ed0e85efac47

    New changeset 106f9e1ad7ab by Petri Lehtinen in branch 'default':
    Revert "Accept None as start and stop parameters for list.index() and tuple.index()"
    http://hg.python.org/cpython/rev/106f9e1ad7ab

  16. akheron commented on Nov 6, 2011

    @akheron
    Member

    It's now reverted on all branches. I posted to python-dev alreay earlier.

  17. bitdancer commented on Oct 10, 2014

    @bitdancer
    Member

    Per discussion in the issue, I'm changing this to a bugfix on the message text. If the python-dev discussion resulted (or results in the future) in a desire to change the API, that should be a new issue.

  18. berkerpeksag commented on Apr 16, 2016

    @berkerpeksag
    Member
  19. iritkatriel commented on Sep 15, 2020

    @iritkatriel
    Member

    The error message was fixed under bpo-29935, so I think this issue can now be closed.

  20. serhiy-storchaka commented on Sep 16, 2020

    @serhiy-storchaka
    Member

    Thank you for the reminder Irit. Your comments for issues and PRs are really helpful. Thank you for all this!

  21. transferred this issue fromon Apr 10, 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

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions