Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tuple.index vs list.index different value errors #121288

Closed
xSpecialFoodx opened this issue Jul 2, 2024 · 13 comments
Closed

tuple.index vs list.index different value errors #121288

xSpecialFoodx opened this issue Jul 2, 2024 · 13 comments
Labels
type-feature A feature request or enhancement

Comments

@xSpecialFoodx
Copy link

xSpecialFoodx commented Jul 2, 2024

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

@xSpecialFoodx xSpecialFoodx added the type-bug An unexpected behavior, bug, or error label Jul 2, 2024
@Eclips4
Copy link
Member

Eclips4 commented Jul 2, 2024

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 Eclips4 added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels Jul 2, 2024
@kumaraditya303
Copy link
Contributor

+1 SGTM

@picnixz
Copy link
Contributor

picnixz commented Jul 3, 2024

I'll take that one if possible

@picnixz
Copy link
Contributor

picnixz commented Jul 3, 2024

By the way, for list.remove:

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

So, should I also change this one?

@Eclips4
Copy link
Member

Eclips4 commented Jul 3, 2024

Yes, let's also improve that one.

@picnixz
Copy link
Contributor

picnixz commented Jul 3, 2024

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

@nineteendo
Copy link
Contributor

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

@picnixz picnixz removed their assignment Jul 3, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Jul 5, 2024
Remove the repr of the searched value (which can be arbitrary large)
from ValueError messages for list.index(), range.index(), deque.index(),
deque.remove() and ShareableList.index().  Make the error messages
consistent with error messages for other index() and remove()
methods.
@serhiy-storchaka
Copy link
Member

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

@nineteendo
Copy link
Contributor

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

gpshead pushed a commit that referenced this issue Jul 5, 2024
Make error message for index() methods consistent

Remove the repr of the searched value (which can be arbitrary large)
from ValueError messages for list.index(), range.index(), deque.index(),
deque.remove() and ShareableList.index().  Make the error messages
consistent with error messages for other index() and remove()
methods.
@vstinner vstinner closed this as not planned Won't fix, can't repro, duplicate, stale Jul 6, 2024
@nineteendo
Copy link
Contributor

Note that the issue was:

tuple.index vs list.index different value error

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

@vstinner vstinner closed this as completed Jul 6, 2024
@vstinner
Copy link
Member

vstinner commented Jul 6, 2024

I didn't see that a PR was merged.

noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
…ythonGH-121395)

Make error message for index() methods consistent

Remove the repr of the searched value (which can be arbitrary large)
from ValueError messages for list.index(), range.index(), deque.index(),
deque.remove() and ShareableList.index().  Make the error messages
consistent with error messages for other index() and remove()
methods.
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
…ythonGH-121395)

Make error message for index() methods consistent

Remove the repr of the searched value (which can be arbitrary large)
from ValueError messages for list.index(), range.index(), deque.index(),
deque.remove() and ShareableList.index().  Make the error messages
consistent with error messages for other index() and remove()
methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

7 participants