tuple.index vs list.index different value errors
Bug report
Bug description:
().index(0) # outputs "tuple.index(x): x not in tuple"
[].index(0) # outputs "0 is not in list"
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
- gh-121308
Yes, the output from list.index is more obvious, in my opinion.
I think we should treat this issue as a type-feature rather than a type-bug, since there is no bug, it's just a place where we can improve the behavior.
+1 SGTM
I'll take that one if possible
By 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.
After digging up a bit, I've concluded that this proposal has more or less been rejected previously (see https://github.com/python/cpython/issues/57558#issuecomment-1093561660). 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:
- https://github.com/python/cpython/issues/49745
- https://github.com/python/cpython/issues/57558
- https://github.com/python/cpython/issues/74039
- https://github.com/python/cpython/issues/74662
- https://github.com/python/cpython/issues/77741
- https://github.com/python/cpython/issues/80623
- https://github.com/python/cpython/issues/83680
I think that we should close it from other way: remove the repr of the value from error message for list.index().
On 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, bytes and bytearray the 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(), and bytearray.index().
Latest attempt to include repr(obj) in list error message went bad:
- https://github.com/python/cpython/issues/116792
- https://github.com/python/cpython/pull/116956
- https://github.com/python/cpython/issues/96844
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.