Improve BaseHandler.__repr__ for Callbacks without __qualname__ (#3934)

This commit is contained in:
Bibo-Joshi 2023-10-22 12:43:23 +02:00 committed by GitHub
parent ea7e5a69aa
commit c82a0808d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View file

@ -105,7 +105,11 @@ class BaseHandler(Generic[UT, CCT], ABC):
Returns:
:obj:`str`
"""
return build_repr_with_selected_attrs(self, callback=self.callback.__qualname__)
try:
callback_name = self.callback.__qualname__
except AttributeError:
callback_name = repr(self.callback)
return build_repr_with_selected_attrs(self, callback=callback_name)
@abstractmethod
def check_update(self, update: object) -> Optional[Union[bool, object]]:

View file

@ -52,3 +52,23 @@ class TestHandler:
sh = SubclassHandler()
assert repr(sh) == "SubclassHandler[callback=TestHandler.test_repr.<locals>.some_func]"
def test_repr_no_qualname(self):
class ClassBasedCallback:
async def __call__(self, *args, **kwargs):
pass
def __repr__(self):
return "Repr of ClassBasedCallback"
class SubclassHandler(BaseHandler):
__slots__ = ()
def __init__(self):
super().__init__(callback=ClassBasedCallback())
def check_update(self, update: object):
pass
sh = SubclassHandler()
assert repr(sh) == "SubclassHandler[callback=Repr of ClassBasedCallback]"