Repository navigation
tuple.index vs list.index different value errors #121288
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jul 2, 2024 Yes, the output from list.index is more obvious, in my opinion.
I think we should treat this issue as atype-featurerather than atype-bug, since there is no bug, it's just a place where we can improve the behavior.- addedtype-featureA feature request or enhancementA feature request or enhancementand removedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jul 2, 2024 +1 SGTM
I'll take that one if possible
Reacted by Kirill PodoprigoraBy the way, for
list.remove:PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
So, should I also change this one?
Yes, let's also improve that one.
Reacted by Bénédikt TranAfter digging up a bit, I've concluded that this proposal has more or less been rejected previously (see #57558 (comment)). So I'm not sure I want to change it now.
Thanks for taking the time to look through previous issues. This tends to be forgotten.
The same issue is reported regularly:
- ValueError exception of tuple.index(x) gives imprecise error message #49745
- Non-informative error message in index() and remove() functions #57558
- Improve exception messages for remove and index methods #74039
- tuple.index error message improvement #74662
- tuple.index() could return a more explicit error message #77741
- Different ValueError for the same operation in List and Tuple #80623
- ValueError using index on tuple is not showing the tuple value #83680
I think that we should close it from other way: remove the repr of the value from error message for
list.index().Reacted by Nice Zombies and Gregory P. SmithOn other hand, error message like "tuple.index(x): x not in tuple" is not especially good:
- it repeats the type name twice
- it uses "x"
Something like "value not found in ..." may look better. But I am not sure that it is worth to change error messages in all
index()methods.BTW, for
str,bytesandbytearraythe error messages are different: "substring not found" and "subsection not found".How about "value not found"? That would also be consistent with
str.index(),bytes.index(), andbytearray.index().Latest attempt to include repr(obj) in list error message went bad:
Note that the issue was:
tuple.index vs list.index different value error
That's now fixed, so this should be closed as "completed".
I didn't see that a PR was merged.
Bug report
Bug description:
i think it'd be useful to normalize them to have the same value error, in tuple you can see that the item's value or name isn't even mentioned, unlike in list that the item's value is mentioned, although the name is not.
CPython versions tested on:
3.11
Operating systems tested on:
Windows
Linked PRs
tuple.index,list.indexandlist.remove#121308