From c82a0808d18cc3ed62b30398cfb33b2b92520ae8 Mon Sep 17 00:00:00 2001 From: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sun, 22 Oct 2023 12:43:23 +0200 Subject: [PATCH] Improve `BaseHandler.__repr__` for Callbacks without `__qualname__` (#3934) --- telegram/ext/_basehandler.py | 6 +++++- tests/ext/test_basehandler.py | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/telegram/ext/_basehandler.py b/telegram/ext/_basehandler.py index d4a75113c..5fa1e3730 100644 --- a/telegram/ext/_basehandler.py +++ b/telegram/ext/_basehandler.py @@ -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]]: diff --git a/tests/ext/test_basehandler.py b/tests/ext/test_basehandler.py index 825db31db..453cf219e 100644 --- a/tests/ext/test_basehandler.py +++ b/tests/ext/test_basehandler.py @@ -52,3 +52,23 @@ class TestHandler: sh = SubclassHandler() assert repr(sh) == "SubclassHandler[callback=TestHandler.test_repr..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]"