]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46927: Include the type's name in the error message for subscripting non-generic...
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 5 Mar 2022 13:59:24 +0000 (15:59 +0200)
committerGitHub <noreply@github.com>
Sat, 5 Mar 2022 13:59:24 +0000 (15:59 +0200)
Lib/test/test_exception_group.py
Lib/test/test_genericalias.py
Misc/NEWS.d/next/Core and Builtins/2022-03-05-12-23-58.bpo-46927.URbHBi.rst [new file with mode: 0644]
Objects/abstract.c

index 8a55c826b8328318d7c085059cfd6a1a32804d49..793e8d20de7e33f68d878ebad87ea2a818be1374 100644 (file)
@@ -11,7 +11,7 @@ class TestExceptionGroupTypeHierarchy(unittest.TestCase):
         self.assertTrue(issubclass(BaseExceptionGroup, BaseException))
 
     def test_exception_is_not_generic_type(self):
-        with self.assertRaises(TypeError):
+        with self.assertRaisesRegex(TypeError, 'Exception'):
             Exception[OSError]
 
     def test_exception_group_is_generic_type(self):
index d311281c578a25bf876959a54d7550ca8287782c..1407657c9bb20667149cfdd895cff7be655bd6ec 100644 (file)
@@ -109,7 +109,7 @@ class BaseTest(unittest.TestCase):
         for t in int, str, float, Sized, Hashable:
             tname = t.__name__
             with self.subTest(f"Testing {tname}"):
-                with self.assertRaises(TypeError):
+                with self.assertRaisesRegex(TypeError, tname):
                     t[int]
 
     def test_instantiate(self):
@@ -275,7 +275,7 @@ class BaseTest(unittest.TestCase):
     def test_type_subclass_generic(self):
         class MyType(type):
             pass
-        with self.assertRaises(TypeError):
+        with self.assertRaisesRegex(TypeError, 'MyType'):
             MyType[int]
 
     def test_pickle(self):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-03-05-12-23-58.bpo-46927.URbHBi.rst b/Misc/NEWS.d/next/Core and Builtins/2022-03-05-12-23-58.bpo-46927.URbHBi.rst
new file mode 100644 (file)
index 0000000..cd59fb8
--- /dev/null
@@ -0,0 +1,2 @@
+Include the type's name in the error message for subscripting non-generic
+types.
index 6ad66a88b4619b5e93e467c586ccb0bc40f4bd20..79f5a5f760f8e252cb5d130d9599625f1f8eca8f 100644 (file)
@@ -190,6 +190,9 @@ PyObject_GetItem(PyObject *o, PyObject *key)
             Py_DECREF(meth);
             return result;
         }
+        PyErr_Format(PyExc_TypeError, "type '%.200s' is not subscriptable",
+                     ((PyTypeObject *)o)->tp_name);
+        return NULL;
     }
 
     return type_error("'%.200s' object is not subscriptable", o);