python / python/cpython

Improve "not found" error messages in `<sequence>.<method>()` failures

Open
#137,508 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

The docstring of list.remove() says value parameter as shown below:

print(help(list.remove))
# remove(self, value, /) unbound builtins.list method
#     Remove first occurrence of value.
#
#     Raises ValueError if the value is not present.

But the error message says x instead of value as shown below and I don't think list.remove(x): is necessary:

v = ['a', 'b', 'c']

v.remove('B')
# ValueError: list.remove(x): x not in list

So, the error message should just say as shown below:

ValueError: value not in list

In addition, the docstring of bytearray.remove() says valueparameter as shown below:

print(help(bytearray.remove))
# remove(self, value, /) unbound builtins.bytearray method
#     Remove the first occurrence of a value in the bytearray.
#
#     value
#       The value to remove.

And, the error message reasonably says value as shown below but I don't think found is necessary:

v = bytearray(b'abc')

v.remove(ord('B'))
# ValueError: value not found in bytearray

So, the error message should just say as shown below:

ValueError: value not in bytearray

Linked PRs
  • gh-137509
  • gh-139696

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the implementations and tests for list.remove() and bytearray.remove(), using the reported examples as the behavioral baseline. Check linked PRs gh-137509 and gh-139696 before starting; done means the two error messages match the requested wording and the relevant tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.