From 8729b1445fea2844bfef3b76dad269629053d83a Mon Sep 17 00:00:00 2001 From: Zaid Date: Thu, 4 Jul 2024 00:01:31 +0300 Subject: [PATCH] update JsonDeserializable --- telebot/types.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/telebot/types.py b/telebot/types.py index 5d69af0ff..cfe322f2e 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -98,11 +98,21 @@ def check_json(json_type, dict_copy = True): raise ValueError("json_type should be a json dict or string.") def __str__(self): - d = { - x: y.__dict__ if hasattr(y, '__dict__') else y - for x, y in self.__dict__.items() - } - return str(d) + return json.dumps( + self, + default=lambda obj: ( + repr(obj) if not isinstance(obj, JsonDeserializable) + else { + attr: getattr(obj, attr) + for attr in filter( + lambda x: not x.startswith("_") and getattr(obj, x) is not None, + obj.__dict__ + ) + } + ), + indent=2, + ensure_ascii=False + ) class Update(JsonDeserializable):