bpo-46927: Include the type's name in the error message for subscripting non-generic types (GH-31694)

This commit is contained in:
Serhiy Storchaka 2022-03-05 15:59:24 +02:00 committed by GitHub
parent 2031149b9a
commit ab9301a28f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 3 deletions

View File

@ -11,7 +11,7 @@ class TestExceptionGroupTypeHierarchy(unittest.TestCase):
self.assertTrue(issubclass(BaseExceptionGroup, BaseException)) self.assertTrue(issubclass(BaseExceptionGroup, BaseException))
def test_exception_is_not_generic_type(self): def test_exception_is_not_generic_type(self):
with self.assertRaises(TypeError): with self.assertRaisesRegex(TypeError, 'Exception'):
Exception[OSError] Exception[OSError]
def test_exception_group_is_generic_type(self): def test_exception_group_is_generic_type(self):

View File

@ -109,7 +109,7 @@ class BaseTest(unittest.TestCase):
for t in int, str, float, Sized, Hashable: for t in int, str, float, Sized, Hashable:
tname = t.__name__ tname = t.__name__
with self.subTest(f"Testing {tname}"): with self.subTest(f"Testing {tname}"):
with self.assertRaises(TypeError): with self.assertRaisesRegex(TypeError, tname):
t[int] t[int]
def test_instantiate(self): def test_instantiate(self):
@ -275,7 +275,7 @@ class BaseTest(unittest.TestCase):
def test_type_subclass_generic(self): def test_type_subclass_generic(self):
class MyType(type): class MyType(type):
pass pass
with self.assertRaises(TypeError): with self.assertRaisesRegex(TypeError, 'MyType'):
MyType[int] MyType[int]
def test_pickle(self): def test_pickle(self):

View File

@ -0,0 +1,2 @@
Include the type's name in the error message for subscripting non-generic
types.

View File

@ -190,6 +190,9 @@ PyObject_GetItem(PyObject *o, PyObject *key)
Py_DECREF(meth); Py_DECREF(meth);
return result; 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); return type_error("'%.200s' object is not subscriptable", o);