@@ -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
44274482class DictProxyTests (unittest .TestCase ):
44284483 def setUp (self ):
0 commit comments