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

serhiy-storchaka webhook-mailer at python.org
Sat Mar 5 08:59:51 EST 2022


https://github.com/python/cpython/commit/ab9301a28fa431d7a32163126fc96de3b2ce6107
commit: ab9301a28fa431d7a32163126fc96de3b2ce6107
branch: main
author: Serhiy Storchaka <storchaka at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2022年03月05日T15:59:24+02:00
summary:
bpo-46927: Include the type's name in the error message for subscripting non-generic types (GH-31694)
files:
A Misc/NEWS.d/next/Core and Builtins/2022-03-05-12-23-58.bpo-46927.URbHBi.rst
M Lib/test/test_exception_group.py
M Lib/test/test_genericalias.py
M Objects/abstract.c
diff --git a/Lib/test/test_exception_group.py b/Lib/test/test_exception_group.py
index 8a55c826b8328..793e8d20de7e3 100644
--- a/Lib/test/test_exception_group.py
+++ b/Lib/test/test_exception_group.py
@@ -11,7 +11,7 @@ def test_exception_group_types(self):
 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):
diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py
index d311281c578a2..1407657c9bb20 100644
--- a/Lib/test/test_genericalias.py
+++ b/Lib/test/test_genericalias.py
@@ -109,7 +109,7 @@ def test_unsubscriptable(self):
 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 @@ def test_type_generic(self):
 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
index 0000000000000..cd59fb89c3654
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-03-05-12-23-58.bpo-46927.URbHBi.rst	
@@ -0,0 +1,2 @@
+Include the type's name in the error message for subscripting non-generic
+types.
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 6ad66a88b4619..79f5a5f760f8e 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -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);


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /