[Python-checkins] gh-92114: Improve error message for types with __class_getitem__ = None (GH-92115)
serhiy-storchaka
webhook-mailer at python.org
Mon May 2 01:29:58 EDT 2022
https://github.com/python/cpython/commit/4d10f703d79b72a9c7f88862c0b4a9abbfb04ee2
commit: 4d10f703d79b72a9c7f88862c0b4a9abbfb04ee2
branch: main
author: Serhiy Storchaka <storchaka at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2022-05-02T08:29:49+03:00
summary:
gh-92114: Improve error message for types with __class_getitem__ = None (GH-92115)
files:
A Misc/NEWS.d/next/Core and Builtins/2022-05-01-16-40-07.gh-issue-92114.5xTlLt.rst
M Lib/test/test_genericclass.py
M Objects/abstract.c
diff --git a/Lib/test/test_genericclass.py b/Lib/test/test_genericclass.py
index 27420d4f2bad5..d8bb37f69e18a 100644
--- a/Lib/test/test_genericclass.py
+++ b/Lib/test/test_genericclass.py
@@ -220,6 +220,7 @@ def __class_getitem__(cls):
return None
with self.assertRaises(TypeError):
C_too_few[int]
+
class C_too_many:
def __class_getitem__(cls, one, two):
return None
@@ -232,16 +233,23 @@ def __class_getitem__(cls, item):
return None
with self.assertRaises(TypeError):
C()[int]
+
class E: ...
e = E()
e.__class_getitem__ = lambda cls, item: 'This will not work'
with self.assertRaises(TypeError):
e[int]
+
class C_not_callable:
__class_getitem__ = "Surprise!"
with self.assertRaises(TypeError):
C_not_callable[int]
+ class C_is_none(tuple):
+ __class_getitem__ = None
+ with self.assertRaisesRegex(TypeError, "C_is_none"):
+ C_is_none[int]
+
def test_class_getitem_metaclass(self):
class Meta(type):
def __class_getitem__(cls, item):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-05-01-16-40-07.gh-issue-92114.5xTlLt.rst b/Misc/NEWS.d/next/Core and Builtins/2022-05-01-16-40-07.gh-issue-92114.5xTlLt.rst
new file mode 100644
index 0000000000000..a999245953022
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-05-01-16-40-07.gh-issue-92114.5xTlLt.rst
@@ -0,0 +1,2 @@
+Improve error message when subscript a type with ``__class_getitem__`` set
+to ``None``.
diff --git a/Objects/abstract.c b/Objects/abstract.c
index cfb0edcab0e5d..9034737235681 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -185,11 +185,12 @@ PyObject_GetItem(PyObject *o, PyObject *key)
if (_PyObject_LookupAttr(o, &_Py_ID(__class_getitem__), &meth) < 0) {
return NULL;
}
- if (meth) {
+ if (meth && meth != Py_None) {
result = PyObject_CallOneArg(meth, key);
Py_DECREF(meth);
return result;
}
+ Py_XDECREF(meth);
PyErr_Format(PyExc_TypeError, "type '%.200s' is not subscriptable",
((PyTypeObject *)o)->tp_name);
return NULL;
More information about the Python-checkins
mailing list