[New-bugs-announce] [issue13349] Uninformal error message in index() and remove() functions

Petri Lehtinen report at bugs.python.org
Sat Nov 5 21:49:37 CET 2011


New submission from Petri Lehtinen <petri at digip.org>:

For example:

>>> (1, 2, 3).index(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: tuple.index(x): x not in tuple

The "x not in tuple" error message should be replaced with "4 is not in tuple". list.index() already does this (see #7252):

>>> [1, 2, 3].index(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 4 is not in list

Although in #7252 it was claimed that no other place in stdlib has this error message, I found many occurences:

Modules/_collectionsmodule.c:    PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque"
Modules/_elementtree.c:            "list.remove(x): x not in list"
Modules/_elementtree.c:            "list.remove(x): x not in list"
Modules/arraymodule.c:    PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
Modules/arraymodule.c:    PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
Objects/abstract.c:                    "sequence.index(x): x not in sequence");
Objects/listobject.c:    PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Objects/tupleobject.c:    PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");

There's also documentation and tests that depend on this actual error message:

Doc/library/doctest.rst:   ValueError: list.remove(x): x not in list
Lib/test/test_xml_etree.py:    ValueError: list.remove(x): x not in list

#7252 was fixed in r76058, and it's quite a lot of code. I think it could be done more easily using PyUnicode_FromFormat() and the %R format.

----------
messages: 147109
nosy: petri.lehtinen
priority: normal
severity: normal
status: open
title: Uninformal error message in index() and remove() functions

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13349>
_______________________________________


More information about the New-bugs-announce mailing list