[Python-checkins] gh-93021: Fix __text_signature__ for __get__ (GH-93023) (GH-94086)

ambv webhook-mailer at python.org
Tue Jun 21 16:32:29 EDT 2022


https://github.com/python/cpython/commit/1b8aa7aafd2ac07836777707fd9ba5ffb353eb0c
commit: 1b8aa7aafd2ac07836777707fd9ba5ffb353eb0c
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: ambv <lukasz at langa.pl>
date: 2022-06-21T22:32:24+02:00
summary:

gh-93021: Fix __text_signature__ for __get__ (GH-93023) (GH-94086)

Because of the way wrap_descr_get is written, the second argument
to __get__ methods implemented through the wrapper is always
optional.
(cherry picked from commit 4e08fbcfdfa57ea94091aabdd09413708e3fb2bf)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra at gmail.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2022-05-20-09-25-34.gh-issue-93021.k3Aji2.rst
M Lib/test/test_types.py
M Objects/typeobject.c

diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index 725d80d9ffdcf..d8e7204dc06c1 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -599,6 +599,12 @@ def test_slot_wrapper_types(self):
         self.assertIsInstance(object.__lt__, types.WrapperDescriptorType)
         self.assertIsInstance(int.__lt__, types.WrapperDescriptorType)
 
+    def test_dunder_get_signature(self):
+        sig = inspect.signature(object.__init__.__get__)
+        self.assertEqual(list(sig.parameters), ["instance", "owner"])
+        # gh-93021: Second parameter is optional
+        self.assertIs(sig.parameters["owner"].default, None)
+
     def test_method_wrapper_types(self):
         self.assertIsInstance(object().__init__, types.MethodWrapperType)
         self.assertIsInstance(object().__str__, types.MethodWrapperType)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-05-20-09-25-34.gh-issue-93021.k3Aji2.rst b/Misc/NEWS.d/next/Core and Builtins/2022-05-20-09-25-34.gh-issue-93021.k3Aji2.rst
new file mode 100644
index 0000000000000..8fdd8dfb4229a
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-05-20-09-25-34.gh-issue-93021.k3Aji2.rst	
@@ -0,0 +1,2 @@
+Fix the :attr:`__text_signature__` for :meth:`__get__` methods implemented
+in C. Patch by Jelle Zijlstra.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 65a9475ab7d7f..d3a0bba20a711 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -6907,7 +6907,7 @@ wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
         obj = NULL;
     if (type == Py_None)
         type = NULL;
-    if (type == NULL &&obj == NULL) {
+    if (type == NULL && obj == NULL) {
         PyErr_SetString(PyExc_TypeError,
                         "__get__(None, None) is invalid");
         return NULL;
@@ -7950,7 +7950,7 @@ static slotdef slotdefs[] = {
     TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next,
            "__next__($self, /)\n--\n\nImplement next(self)."),
     TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
-           "__get__($self, instance, owner, /)\n--\n\nReturn an attribute of instance, which is of type owner."),
+           "__get__($self, instance, owner=None, /)\n--\n\nReturn an attribute of instance, which is of type owner."),
     TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
            "__set__($self, instance, value, /)\n--\n\nSet an attribute of instance to value."),
     TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,



More information about the Python-checkins mailing list