Skip to content

Commit 7806f47

Browse files
committed
pythongh-93021: Fix __text_signature__ for __get__
Because of the way wrap_descr_get is written, the second argument to __get__ methods implemented through the wrapper is always optional.
1 parent 87b9b4e commit 7806f47

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

Lib/test/test_types.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,12 @@ def test_slot_wrapper_types(self):
597597
self.assertIsInstance(object.__lt__, types.WrapperDescriptorType)
598598
self.assertIsInstance(int.__lt__, types.WrapperDescriptorType)
599599

600+
def test_dunder_get_signature(self):
601+
sig = inspect.signature(object.__init__.__get__)
602+
self.assertEqual(list(sig.parameters), ["instance", "owner"])
603+
# gh-93021: Second parameter is optional
604+
self.assertIs(sig.parameters["owner"].default, None)
605+
600606
def test_method_wrapper_types(self):
601607
self.assertIsInstance(object().__init__, types.MethodWrapperType)
602608
self.assertIsInstance(object().__str__, types.MethodWrapperType)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the :attr:`__text_signature__` for :meth:`__get__` methods implemented
2+
in C. Patch by Jelle Zijlstra.

Objects/typeobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7029,7 +7029,7 @@ wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
70297029
obj = NULL;
70307030
if (type == Py_None)
70317031
type = NULL;
7032-
if (type == NULL &&obj == NULL) {
7032+
if (type == NULL && obj == NULL) {
70337033
PyErr_SetString(PyExc_TypeError,
70347034
"__get__(None, None) is invalid");
70357035
return NULL;
@@ -8047,7 +8047,7 @@ static slotdef slotdefs[] = {
80478047
TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next,
80488048
"__next__($self, /)\n--\n\nImplement next(self)."),
80498049
TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
8050-
"__get__($self, instance, owner, /)\n--\n\nReturn an attribute of instance, which is of type owner."),
8050+
"__get__($self, instance, owner=None, /)\n--\n\nReturn an attribute of instance, which is of type owner."),
80518051
TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
80528052
"__set__($self, instance, value, /)\n--\n\nSet an attribute of instance to value."),
80538053
TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,

0 commit comments

Comments
 (0)