From ac7cc7fe5e376e26ce1b38b587b9d4c14a515bee Mon Sep 17 00:00:00 2001 From: n5y <41209360+n5y@users.noreply.github.com> Date: Wed, 10 Jun 2020 23:21:25 +0300 Subject: [PATCH] Ignore private attributes in TelegramObject.to_dict() (#1989) --- telegram/base.py | 7 +------ tests/test_telegramobject.py | 9 +++++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/telegram/base.py b/telegram/base.py index 3431396c9..65c43678e 100644 --- a/telegram/base.py +++ b/telegram/base.py @@ -57,12 +57,7 @@ class TelegramObject(object): data = dict() for key in iter(self.__dict__): - if key in ('bot', - '_id_attrs', - '_credentials', - '_decrypted_credentials', - '_decrypted_data', - '_decrypted_secret'): + if key == 'bot' or key.startswith('_'): continue value = self.__dict__[key] diff --git a/tests/test_telegramobject.py b/tests/test_telegramobject.py index 66f52dbfb..156c43b65 100644 --- a/tests/test_telegramobject.py +++ b/tests/test_telegramobject.py @@ -74,3 +74,12 @@ class TestTelegramObject(object): monkeypatch.setattr('telegram.TelegramObject.to_dict', lambda _: d) telegram_object.to_json() + + def test_to_dict_private_attribute(self): + class TelegramObjectSubclass(TelegramObject): + def __init__(self): + self.a = 1 + self._b = 2 + + subclass_instance = TelegramObjectSubclass() + assert subclass_instance.to_dict() == {'a': 1}