use __qualname__ to compute bound method repr (closes #21389) · pythoncapi/cpython@48ad7c0 · GitHub
Skip to content

Commit 48ad7c0

Browse files
committed
use __qualname__ to compute bound method repr (closes python#21389)
Patch from Steven Barker.
1 parent 2b7ccbd commit 48ad7c0

4 files changed

Lines changed: 74 additions & 32 deletions

File tree

Lib/test/test_defaultdict.py

Lines changed: 3 additions & 2 deletions

Lib/test/test_descr.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4423,6 +4423,61 @@ class Sub(Base):
44234423
self.assertIn("__dict__", Base.__dict__)
44244424
self.assertNotIn("__dict__", Sub.__dict__)
44254425

4426+
def test_bound_method_repr(self):
4427+
class Foo:
4428+
def method(self):
4429+
pass
4430+
self.assertRegex(repr(Foo().method),
4431+
r"<bound method .*Foo\.method of <.*Foo object at .*>>")
4432+
4433+
4434+
class Base:
4435+
def method(self):
4436+
pass
4437+
class Derived1(Base):
4438+
pass
4439+
class Derived2(Base):
4440+
def method(self):
4441+
pass
4442+
base = Base()
4443+
derived1 = Derived1()
4444+
derived2 = Derived2()
4445+
super_d2 = super(Derived2, derived2)
4446+
self.assertRegex(repr(base.method),
4447+
r"<bound method .*Base\.method of <.*Base object at .*>>")
4448+
self.assertRegex(repr(derived1.method),
4449+
r"<bound method .*Base\.method of <.*Derived1 object at .*>>")
4450+
self.assertRegex(repr(derived2.method),
4451+
r"<bound method .*Derived2\.method of <.*Derived2 object at .*>>")
4452+
self.assertRegex(repr(super_d2.method),
4453+
r"<bound method .*Base\.method of <.*Derived2 object at .*>>")
4454+
4455+
class Foo:
4456+
@classmethod
4457+
def method(cls):
4458+
pass
4459+
foo = Foo()
4460+
self.assertRegex(repr(foo.method), # access via instance
4461+
r"<bound method .*Foo\.method of <class '.*Foo'>>")
4462+
self.assertRegex(repr(Foo.method), # access via the class
4463+
r"<bound method .*Foo\.method of <class '.*Foo'>>")
4464+
4465+
4466+
class MyCallable:
4467+
def __call__(self, arg):
4468+
pass
4469+
func = MyCallable() # func has no __name__ or __qualname__ attributes
4470+
instance = object()
4471+
method = types.MethodType(func, instance)
4472+
self.assertRegex(repr(method),
4473+
r"<bound method \? of <object object at .*>>")
4474+
func.__name__ = "name"
4475+
self.assertRegex(repr(method),
4476+
r"<bound method name of <object object at .*>>")
4477+
func.__qualname__ = "qualname"
4478+
self.assertRegex(repr(method),
4479+
r"<bound method qualname of <object object at .*>>")
4480+
44264481

44274482
class DictProxyTests(unittest.TestCase):
44284483
def setUp(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #21389: Displaying the __qualname__ of the underlying function in the
14+
repr of a bound method.
15+
1316
- Issue #22206: Using pthread, PyThread_create_key() now sets errno to ENOMEM
1417
and returns -1 (error) on integer overflow.
1518

Objects/classobject.c

Lines changed: 13 additions & 30 deletions

0 commit comments

Comments
 (0)