https://github.com/python/cpython/commit/dc03ce797ae8786a9711e6ee5dcaadde02c55864
commit: dc03ce797ae8786a9711e6ee5dcaadde02c55864
branch: main
author: Zachary Ware <[email protected]>
committer: zware <[email protected]>
date: 2024-07-12T16:34:17Z
summary:

gh-95144: Improve error message of `... in None` (GH-119888)

files:
A Misc/NEWS.d/next/Core and 
Builtins/2022-07-22-15-56-35.gh-issue-95144.FZYWX-.rst
M Lib/test/test_contains.py
M Lib/test/test_sqlite3/test_dbapi.py
M Objects/abstract.c

diff --git a/Lib/test/test_contains.py b/Lib/test/test_contains.py
index 471d04a76ca4e4..259c954aa1d616 100644
--- a/Lib/test/test_contains.py
+++ b/Lib/test/test_contains.py
@@ -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')
diff --git a/Lib/test/test_sqlite3/test_dbapi.py 
b/Lib/test/test_sqlite3/test_dbapi.py
index 293baccaf1831d..488b401fb0054d 100644
--- a/Lib/test/test_sqlite3/test_dbapi.py
+++ b/Lib/test/test_sqlite3/test_dbapi.py
@@ -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):
diff --git a/Misc/NEWS.d/next/Core and 
Builtins/2022-07-22-15-56-35.gh-issue-95144.FZYWX-.rst b/Misc/NEWS.d/next/Core 
and Builtins/2022-07-22-15-56-35.gh-issue-95144.FZYWX-.rst
new file mode 100644
index 00000000000000..83b1126a8a455f
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2022-07-22-15-56-35.gh-issue-95144.FZYWX-.rst  
@@ -0,0 +1,2 @@
+Improve the error message from ``a in b`` when ``b`` is not a container
+to mention the term "container".
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 200817064e3cda..afb068718bb010 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -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)
@@ -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;
     }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to