Skip to content

Commit

Permalink
Improve BaseHandler.__repr__ for Callbacks without __qualname__ (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Bibo-Joshi committed Oct 22, 2023
1 parent ea7e5a6 commit c82a080
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion telegram/ext/_basehandler.py
Expand Up @@ -105,7 +105,11 @@ def __repr__(self) -> str:
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]]:
Expand Down
20 changes: 20 additions & 0 deletions tests/ext/test_basehandler.py
Expand Up @@ -52,3 +52,23 @@ def check_update(self, update: object):

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]"

0 comments on commit c82a080

Please sign in to comment.