cpython icon indicating copy to clipboard operation
cpython copied to clipboard

tuple.index vs list.index different value errors

Open xSpecialFoodx opened this issue 1 year ago • 7 comments

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

xSpecialFoodx avatar Jul 02 '24 19:07 xSpecialFoodx

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.

Eclips4 avatar Jul 02 '24 20:07 Eclips4

+1 SGTM

kumaraditya303 avatar Jul 03 '24 07:07 kumaraditya303

I'll take that one if possible

picnixz avatar Jul 03 '24 07:07 picnixz

By the way, for list.remove:

PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");

So, should I also change this one?

picnixz avatar Jul 03 '24 07:07 picnixz

Yes, let's also improve that one.

Eclips4 avatar Jul 03 '24 07:07 Eclips4

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.

picnixz avatar Jul 03 '24 09:07 picnixz

Thanks for taking the time to look through previous issues. This tends to be forgotten.

nineteendo avatar Jul 03 '24 11:07 nineteendo

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().

serhiy-storchaka avatar Jul 05 '24 08:07 serhiy-storchaka

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".

serhiy-storchaka avatar Jul 05 '24 09:07 serhiy-storchaka

How about "value not found"? That would also be consistent with str.index(), bytes.index(), and bytearray.index().

nineteendo avatar Jul 05 '24 09:07 nineteendo

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

vstinner avatar Jul 05 '24 19:07 vstinner

Note that the issue was:

tuple.index vs list.index different value error

That's now fixed, so this should be closed as "completed".

nineteendo avatar Jul 06 '24 15:07 nineteendo

I didn't see that a PR was merged.

vstinner avatar Jul 06 '24 17:07 vstinner