Skip to content

gh-137508: Improve list.remove/index error message #137509

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Doc/library/doctest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,10 @@ Simple example::
>>> [1, 2, 3].remove(42)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
ValueError: 42 not in list

That doctest succeeds if :exc:`ValueError` is raised, with the ``list.remove(x):
x not in list`` detail as shown.
That doctest succeeds if :exc:`ValueError` is raised, with the
``42 not in list`` detail as shown.

The expected output for an exception must start with a traceback header, which
may be either of the following two lines, indented the same as the first line of
Expand Down
2 changes: 1 addition & 1 deletion Lib/multiprocessing/shared_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,6 @@ def index(self, value):
if value == entry:
return position
else:
raise ValueError("ShareableList.index(x): x not in list")
raise ValueError(f"{value} not in list")

__class_getitem__ = classmethod(types.GenericAlias)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`list.remove` and :func:`list.index` now raise with more informative
error messages.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:meth:`multiprocessing.shared_memory.ShareableList` now rasies with a more
informative error message.
4 changes: 2 additions & 2 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3305,7 +3305,7 @@ list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
else if (cmp < 0)
return NULL;
}
PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
PyErr_Format(PyExc_ValueError, "%R not in list", value);
return NULL;
}

Expand Down Expand Up @@ -3375,7 +3375,7 @@ list_remove_impl(PyListObject *self, PyObject *value)
else if (cmp < 0)
return NULL;
}
PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
PyErr_Format(PyExc_ValueError, "%R not in list", value);
return NULL;
}

Expand Down
Loading