From 0bed0875427e72088c08c10e96c9dbc79741c84b Mon Sep 17 00:00:00 2001 From: Eldinnie Date: Mon, 19 Feb 2018 09:31:38 +0100 Subject: [PATCH] remove unnecessary to_dict() (#834) We have some objects that have exactly the same to_dict() method, only specifying that `from_user` should be `from` in the `data`-dict. I refractored this logic to `TelegramObject` and removed those to_dicts() from the code. --- telegram/base.py | 2 ++ telegram/callbackquery.py | 7 ------- telegram/choseninlineresult.py | 8 -------- telegram/inline/inlinequery.py | 8 -------- telegram/message.py | 1 - telegram/payment/precheckoutquery.py | 7 ------- telegram/payment/shippingquery.py | 7 ------- 7 files changed, 2 insertions(+), 38 deletions(-) diff --git a/telegram/base.py b/telegram/base.py index 706ef6622..b8b959eab 100644 --- a/telegram/base.py +++ b/telegram/base.py @@ -70,6 +70,8 @@ class TelegramObject(object): else: data[key] = value + if data.get('from_user'): + data['from'] = data.pop('from_user', None) return data def __eq__(self, other): diff --git a/telegram/callbackquery.py b/telegram/callbackquery.py index 9e2520677..54e5b7576 100644 --- a/telegram/callbackquery.py +++ b/telegram/callbackquery.py @@ -105,13 +105,6 @@ class CallbackQuery(TelegramObject): return cls(bot=bot, **data) - def to_dict(self): - data = super(CallbackQuery, self).to_dict() - - # Required - data['from'] = data.pop('from_user', None) - return data - def answer(self, *args, **kwargs): """Shortcut for:: diff --git a/telegram/choseninlineresult.py b/telegram/choseninlineresult.py index 011cc64ef..f25f91322 100644 --- a/telegram/choseninlineresult.py +++ b/telegram/choseninlineresult.py @@ -79,11 +79,3 @@ class ChosenInlineResult(TelegramObject): data['location'] = Location.de_json(data.get('location'), bot) return cls(**data) - - def to_dict(self): - data = super(ChosenInlineResult, self).to_dict() - - # Required - data['from'] = data.pop('from_user', None) - - return data diff --git a/telegram/inline/inlinequery.py b/telegram/inline/inlinequery.py index 8bcedc900..79b7e127b 100644 --- a/telegram/inline/inlinequery.py +++ b/telegram/inline/inlinequery.py @@ -75,14 +75,6 @@ class InlineQuery(TelegramObject): return cls(bot=bot, **data) - def to_dict(self): - data = super(InlineQuery, self).to_dict() - - # Required - data['from'] = data.pop('from_user', None) - - return data - def answer(self, *args, **kwargs): """Shortcut for:: diff --git a/telegram/message.py b/telegram/message.py index c4686fa07..d36ba32ab 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -387,7 +387,6 @@ class Message(TelegramObject): data = super(Message, self).to_dict() # Required - data['from'] = data.pop('from_user', None) data['date'] = to_timestamp(self.date) # Optionals if self.forward_date: diff --git a/telegram/payment/precheckoutquery.py b/telegram/payment/precheckoutquery.py index 985c51331..36eebf69a 100644 --- a/telegram/payment/precheckoutquery.py +++ b/telegram/payment/precheckoutquery.py @@ -89,13 +89,6 @@ class PreCheckoutQuery(TelegramObject): return cls(bot=bot, **data) - def to_dict(self): - data = super(PreCheckoutQuery, self).to_dict() - - data['from'] = data.pop('from_user', None) - - return data - def answer(self, *args, **kwargs): """Shortcut for:: diff --git a/telegram/payment/shippingquery.py b/telegram/payment/shippingquery.py index da0c15846..a5b819150 100644 --- a/telegram/payment/shippingquery.py +++ b/telegram/payment/shippingquery.py @@ -66,13 +66,6 @@ class ShippingQuery(TelegramObject): return cls(**data) - def to_dict(self): - data = super(ShippingQuery, self).to_dict() - - data['from'] = data.pop('from_user', None) - - return data - def answer(self, *args, **kwargs): """Shortcut for::