Skip to content

gh-95144: Improve error message of ... in None #119888

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

Merged
merged 2 commits into from
Jul 12, 2024
Merged
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
7 changes: 5 additions & 2 deletions Lib/test/test_contains.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ def test_common_tests(self):
self.assertNotIn(0, b)
self.assertIn(1, c)
self.assertNotIn(0, c)
self.assertRaises(TypeError, lambda: 1 in a)
self.assertRaises(TypeError, lambda: 1 not in a)
msg = "argument of type 'base_set' is not a container or iterable"
with self.assertRaisesRegex(TypeError, msg):
1 in a
with self.assertRaisesRegex(TypeError, msg):
1 not in a

# test char in string
self.assertIn('c', 'abc')
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,7 @@ def test_blob_sequence_not_supported(self):
self.blob + self.blob
with self.assertRaisesRegex(TypeError, "unsupported operand"):
self.blob * 5
with self.assertRaisesRegex(TypeError, "is not iterable"):
with self.assertRaisesRegex(TypeError, "is not.+iterable"):
b"a" in self.blob

def test_blob_context_manager(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve the error message from ``a in b`` when ``b`` is not a container
to mention the term "container".
12 changes: 10 additions & 2 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -2141,7 +2141,7 @@ PySequence_Fast(PyObject *v, const char *m)
PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq;
set ValueError and return -1 if none found; also return -1 on error.
Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
*/
Py_ssize_t
_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
Expand All @@ -2158,7 +2158,15 @@ _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
it = PyObject_GetIter(seq);
if (it == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
type_error("argument of type '%.200s' is not iterable", seq);
if (operation == PY_ITERSEARCH_CONTAINS) {
type_error(
"argument of type '%.200s' is not a container or iterable",
seq
);
}
else {
type_error("argument of type '%.200s' is not iterable", seq);
}
}
return -1;
}
Expand Down
Loading