This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Improve exception messages for remove and index methods
Type: enhancement Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Non-informative error message in index() and remove() functions
View: 13349
Assigned To: Nosy List: Jim Fasarakis-Hilliard
Priority: normal Keywords:

Created on 2017-03-19 15:09 by Jim Fasarakis-Hilliard, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg289854 - (view) Author: Jim Fasarakis-Hilliard (Jim Fasarakis-Hilliard) * Date: 2017-03-19 15:09
Currently, there's a discrepancy in the exception reporting for the `.index` and `.remove` methods of many objects:

For arrays:

    array.remove(val) -> ValueError: array.remove(x): x not in list 
    array.index(val)  -> ValueError: array.index(x): x not in list

not only is always printing `x` not in list not informative, it's wrong since it isn't a list.

For tuples:

    tuple.index(val)  -> ValueError: tuple.index(x): x not in tuple

For lists:

    list.remove(val)  -> ValueError: list.remove(x): x not in list

list.index(val) produces a more informative message: ValueError: <val> is not in list

For deques:

    deque.remove(val) -> ValueError: deque.remove(x): x not in deque

similarly to lists, `deque.index(val)` prints the actual argument supplied.

I'm not sure if there's valid reasoning behind not providing the repr of the arguments in all `remove` methods but, if there isn't, I'd like to suggest changing all of them to use PyErr_Format and produce more informative messages:

    array.remove(val) -> ValueError: <val> is not in array 
    array.index(val)  -> ValueError: <val> is not in array
    tuple.index(val)  -> ValueError: <val> is not in tuple
    list.remove(val)  -> ValueError: <val> is not in list
    deque.remove(val) -> ValueError: <val> is not in deque
msg289855 - (view) Author: Jim Fasarakis-Hilliard (Jim Fasarakis-Hilliard) * Date: 2017-03-19 15:09
I'd be happy to supply a PR for this if the change seems reasonable.
History
Date User Action Args
2022-04-11 14:58:44adminsetgithub: 74039
2017-03-19 15:19:47serhiy.storchakasetstatus: open -> closed
superseder: Non-informative error message in index() and remove() functions
resolution: duplicate
stage: resolved
2017-03-19 15:09:57Jim Fasarakis-Hilliardsetmessages: + msg289855
2017-03-19 15:09:05Jim Fasarakis-Hilliardcreate