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

Show value in error of list.remove #96844

Closed
tdegeus opened this issue Sep 15, 2022 · 3 comments
Closed

Show value in error of list.remove #96844

tdegeus opened this issue Sep 15, 2022 · 3 comments
Labels
type-feature A feature request or enhancement

Comments

@tdegeus
Copy link

tdegeus commented Sep 15, 2022

Consider

foo = ["a", "b"]
for i in ["c"]:
    foo.remove(i)

This throws with a useless error message:

Traceback (most recent call last):
  File "/Users/tdegeus/Downloads/t.py", line 3, in <module>
    foo.remove(i)
ValueError: list.remove(x): x not in list

Instead it would be very helpful to show the value of x. Now one needs extra manual debugging, or extra lines of code.

Linked PRs

@pochmann
Copy link
Contributor

pochmann commented Sep 15, 2022

What if x is large, so large that it pushes the previous error-information off-screen or even out of the screen buffer?

What if the conversion of x to string takes a long time or causes another error? (Think of the current large int issue).

Maybe it could be done for known small good types and values.

Then again, for example set.remove already does this:


x = 1; {0}.remove(x) shows:

KeyError: 1

x = 10**1000000; {0}.remove(x) takes a long time and then prints a lot (testing with Python version without the new string conversion limit).


class C:
    def __repr__(self):
        raise Exception('foobar')
{0}.remove(C())

shows:

KeyError: <exception str() failed>

@ericvsmith
Copy link
Member

Wouldn't we already have this problem with KeyError on dict lookups:

>>> {}[1234]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 1234

?
I guess you could argue that the value should be removed from KeyError.

@corona10
Copy link
Member

corona10 commented Jul 5, 2023


Python 3.13.0a0 (heads/main:217f47d6e5, Jul  6 2023, 07:20:36) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> foo = ["a", "b"]
>>> for i in ["c"]:
...     foo.remove(i)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ValueError: 'c' is not in list

Now the error message is updated, here is the rationale why I agree with the updating error message even if for the possibility of large input.

#106455 (comment)

cc @tdegeus @pochmann @ericvsmith

vstinner pushed a commit to vstinner/cpython that referenced this issue Mar 18, 2024
vstinner added a commit to vstinner/cpython that referenced this issue Mar 18, 2024
vstinner added a commit to vstinner/cpython that referenced this issue Mar 18, 2024
vstinner added a commit to vstinner/cpython that referenced this issue Mar 20, 2024
adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
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

4 participants