Skip to content

Commit ab9301a

Browse files
bpo-46927: Include the type's name in the error message for subscripting non-generic types (GH-31694)
1 parent 2031149 commit ab9301a

File tree

4 files changed

+8
-3
lines changed

4 files changed

+8
-3
lines changed

Lib/test/test_exception_group.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def test_exception_group_types(self):
1111
self.assertTrue(issubclass(BaseExceptionGroup, BaseException))
1212

1313
def test_exception_is_not_generic_type(self):
14-
with self.assertRaises(TypeError):
14+
with self.assertRaisesRegex(TypeError, 'Exception'):
1515
Exception[OSError]
1616

1717
def test_exception_group_is_generic_type(self):

Lib/test/test_genericalias.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def test_unsubscriptable(self):
109109
for t in int, str, float, Sized, Hashable:
110110
tname = t.__name__
111111
with self.subTest(f"Testing {tname}"):
112-
with self.assertRaises(TypeError):
112+
with self.assertRaisesRegex(TypeError, tname):
113113
t[int]
114114

115115
def test_instantiate(self):
@@ -275,7 +275,7 @@ def test_type_generic(self):
275275
def test_type_subclass_generic(self):
276276
class MyType(type):
277277
pass
278-
with self.assertRaises(TypeError):
278+
with self.assertRaisesRegex(TypeError, 'MyType'):
279279
MyType[int]
280280

281281
def test_pickle(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Include the type's name in the error message for subscripting non-generic
2+
types.

Objects/abstract.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ PyObject_GetItem(PyObject *o, PyObject *key)
190190
Py_DECREF(meth);
191191
return result;
192192
}
193+
PyErr_Format(PyExc_TypeError, "type '%.200s' is not subscriptable",
194+
((PyTypeObject *)o)->tp_name);
195+
return NULL;
193196
}
194197

195198
return type_error("'%.200s' object is not subscriptable", o);

0 commit comments

Comments
 (0)