From ae55b6bbeb6223aab0b20d5bd99d21c69d96605a Mon Sep 17 00:00:00 2001 From: starry69 Date: Tue, 10 Aug 2021 09:31:28 +0530 Subject: [PATCH 1/5] Make basepersistence methods abstractmethod Signed-off-by: starry69 --- telegram/ext/basepersistence.py | 8 +++++-- telegram/ext/dictpersistence.py | 7 ++++++ tests/test_dispatcher.py | 24 +++++++++++++++++++++ tests/test_persistence.py | 38 ++++++++++++++++++++++++++++++--- 4 files changed, 72 insertions(+), 5 deletions(-) diff --git a/telegram/ext/basepersistence.py b/telegram/ext/basepersistence.py index 974b97f8f8c..e45548f9825 100644 --- a/telegram/ext/basepersistence.py +++ b/telegram/ext/basepersistence.py @@ -439,6 +439,7 @@ def get_bot_data(self) -> BD: :class:`telegram.ext.utils.types.BD`: The restored bot data. """ + @abstractmethod def get_callback_data(self) -> Optional[CDCData]: """Will be called by :class:`telegram.ext.Dispatcher` upon creation with a persistence object. If callback data was stored, it should be returned. @@ -449,7 +450,6 @@ def get_callback_data(self) -> Optional[CDCData]: Optional[:class:`telegram.ext.utils.types.CDCData`]: The restored meta data or :obj:`None`, if no data was stored. """ - raise NotImplementedError @abstractmethod def get_conversations(self, name: str) -> ConversationDict: @@ -510,6 +510,7 @@ def update_bot_data(self, data: BD) -> None: :attr:`telegram.ext.Dispatcher.bot_data`. """ + @abstractmethod def refresh_user_data(self, user_id: int, user_data: UD) -> None: """Will be called by the :class:`telegram.ext.Dispatcher` before passing the :attr:`user_data` to a callback. Can be used to update data stored in :attr:`user_data` @@ -522,6 +523,7 @@ def refresh_user_data(self, user_id: int, user_data: UD) -> None: user_data (:class:`telegram.ext.utils.types.UD`): The ``user_data`` of a single user. """ + @abstractmethod def refresh_chat_data(self, chat_id: int, chat_data: CD) -> None: """Will be called by the :class:`telegram.ext.Dispatcher` before passing the :attr:`chat_data` to a callback. Can be used to update data stored in :attr:`chat_data` @@ -534,6 +536,7 @@ def refresh_chat_data(self, chat_id: int, chat_data: CD) -> None: chat_data (:class:`telegram.ext.utils.types.CD`): The ``chat_data`` of a single chat. """ + @abstractmethod def refresh_bot_data(self, bot_data: BD) -> None: """Will be called by the :class:`telegram.ext.Dispatcher` before passing the :attr:`bot_data` to a callback. Can be used to update data stored in :attr:`bot_data` @@ -545,6 +548,7 @@ def refresh_bot_data(self, bot_data: BD) -> None: bot_data (:class:`telegram.ext.utils.types.BD`): The ``bot_data``. """ + @abstractmethod def update_callback_data(self, data: CDCData) -> None: """Will be called by the :class:`telegram.ext.Dispatcher` after a handler has handled an update. @@ -555,8 +559,8 @@ def update_callback_data(self, data: CDCData) -> None: data (:class:`telegram.ext.utils.types.CDCData`): The relevant data to restore :class:`telegram.ext.CallbackDataCache`. """ - raise NotImplementedError + @abstractmethod def flush(self) -> None: """Will be called by :class:`telegram.ext.Updater` upon receiving a stop signal. Gives the persistence a chance to finish up saving or close a database connection gracefully. diff --git a/telegram/ext/dictpersistence.py b/telegram/ext/dictpersistence.py index 72c767d74fa..0b9390a50a6 100644 --- a/telegram/ext/dictpersistence.py +++ b/telegram/ext/dictpersistence.py @@ -402,3 +402,10 @@ def refresh_bot_data(self, bot_data: Dict) -> None: .. versionadded:: 13.6 .. seealso:: :meth:`telegram.ext.BasePersistence.refresh_bot_data` """ + + def flush(self) -> None: + """Does nothing. + + .. versionadded:: 14.0 + .. seealso:: :meth:`telegram.ext.BasePersistence.flush` + """ diff --git a/tests/test_dispatcher.py b/tests/test_dispatcher.py index 4c25f8a3ab1..c69ae515cb8 100644 --- a/tests/test_dispatcher.py +++ b/tests/test_dispatcher.py @@ -632,6 +632,18 @@ def get_conversations(self, name): def update_conversation(self, name, key, new_state): pass + def refresh_user_data(self, user_id, user_data): + pass + + def refresh_chat_data(self, chat_id, chat_data): + pass + + def refresh_bot_data(self, bot_data): + pass + + def flush(self): + pass + def start1(b, u): pass @@ -776,6 +788,9 @@ def refresh_user_data(self, user_id, user_data): def refresh_chat_data(self, chat_id, chat_data): pass + def flush(self): + pass + def callback(update, context): pass @@ -845,6 +860,15 @@ def refresh_user_data(self, user_id, user_data): def refresh_chat_data(self, chat_id, chat_data): pass + def get_callback_data(self): + pass + + def update_callback_data(self, data): + pass + + def flush(self): + pass + def callback(update, context): pass diff --git a/tests/test_persistence.py b/tests/test_persistence.py index 56e797219df..d03bf835b98 100644 --- a/tests/test_persistence.py +++ b/tests/test_persistence.py @@ -98,6 +98,24 @@ def update_conversation(self, name, key, new_state): def update_user_data(self, user_id, data): raise NotImplementedError + def get_callback_data(self): + raise NotImplementedError + + def refresh_user_data(self, user_id, user_data): + raise NotImplementedError + + def refresh_chat_data(self, chat_id, chat_data): + raise NotImplementedError + + def refresh_bot_data(self, bot_data): + raise NotImplementedError + + def update_callback_data(self, data): + raise NotImplementedError + + def flush(self): + raise NotImplementedError + @pytest.fixture(scope="function") def base_persistence(): @@ -148,6 +166,18 @@ def update_callback_data(self, data): def update_conversation(self, name, key, new_state): raise NotImplementedError + def refresh_user_data(self, user_id, user_data): + pass + + def refresh_chat_data(self, chat_id, chat_data): + pass + + def refresh_bot_data(self, bot_data): + pass + + def flush(self): + pass + return BotPersistence() @@ -239,9 +269,11 @@ def test_abstract_methods(self, base_persistence): with pytest.raises( TypeError, match=( - 'get_bot_data, get_chat_data, get_conversations, ' - 'get_user_data, update_bot_data, update_chat_data, ' - 'update_conversation, update_user_data' + 'flush, get_bot_data, get_callback_data, ' + 'get_chat_data, get_conversations, ' + 'get_user_data, refresh_bot_data, refresh_chat_data, ' + 'refresh_user_data, update_bot_data, update_callback_data, ' + 'update_chat_data, update_conversation, update_user_data' ), ): BasePersistence() From 79145617a2e94c73b6265a455426511bb7faaa57 Mon Sep 17 00:00:00 2001 From: starry69 Date: Tue, 10 Aug 2021 12:57:40 +0530 Subject: [PATCH 2/5] set store_callback_data to True by default Signed-off-by: starry69 --- telegram/ext/basepersistence.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/telegram/ext/basepersistence.py b/telegram/ext/basepersistence.py index e45548f9825..c53fbd2b340 100644 --- a/telegram/ext/basepersistence.py +++ b/telegram/ext/basepersistence.py @@ -76,7 +76,7 @@ class BasePersistence(Generic[UD, CD, BD], ABC): store_bot_data (:obj:`bool`, optional): Whether bot_data should be saved by this persistence class. Default is :obj:`True`. store_callback_data (:obj:`bool`, optional): Whether callback_data should be saved by this - persistence class. Default is :obj:`False`. + persistence class. Default is :obj:`True`. .. versionadded:: 13.6 @@ -176,7 +176,7 @@ def __init__( store_user_data: bool = True, store_chat_data: bool = True, store_bot_data: bool = True, - store_callback_data: bool = False, + store_callback_data: bool = True, ): self.store_user_data = store_user_data self.store_chat_data = store_chat_data From 1d64bfcbb5e37e59c546b6f3ac599304287c1587 Mon Sep 17 00:00:00 2001 From: starry69 Date: Wed, 11 Aug 2021 18:11:01 +0530 Subject: [PATCH 3/5] added versionchanged directives Signed-off-by: starry69 --- telegram/ext/basepersistence.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/telegram/ext/basepersistence.py b/telegram/ext/basepersistence.py index c53fbd2b340..fd106892f59 100644 --- a/telegram/ext/basepersistence.py +++ b/telegram/ext/basepersistence.py @@ -446,6 +446,8 @@ def get_callback_data(self) -> Optional[CDCData]: .. versionadded:: 13.6 + .. versionchanged:: 14.0 + Returns: Optional[:class:`telegram.ext.utils.types.CDCData`]: The restored meta data or :obj:`None`, if no data was stored. @@ -518,6 +520,8 @@ def refresh_user_data(self, user_id: int, user_data: UD) -> None: .. versionadded:: 13.6 + .. versionchanged:: 14.0 + Args: user_id (:obj:`int`): The user ID this :attr:`user_data` is associated with. user_data (:class:`telegram.ext.utils.types.UD`): The ``user_data`` of a single user. @@ -531,6 +535,8 @@ def refresh_chat_data(self, chat_id: int, chat_data: CD) -> None: .. versionadded:: 13.6 + .. versionchanged:: 14.0 + Args: chat_id (:obj:`int`): The chat ID this :attr:`chat_data` is associated with. chat_data (:class:`telegram.ext.utils.types.CD`): The ``chat_data`` of a single chat. @@ -544,6 +550,8 @@ def refresh_bot_data(self, bot_data: BD) -> None: .. versionadded:: 13.6 + .. versionchanged:: 14.0 + Args: bot_data (:class:`telegram.ext.utils.types.BD`): The ``bot_data``. """ @@ -555,6 +563,8 @@ def update_callback_data(self, data: CDCData) -> None: .. versionadded:: 13.6 + .. versionchanged:: 14.0 + Args: data (:class:`telegram.ext.utils.types.CDCData`): The relevant data to restore :class:`telegram.ext.CallbackDataCache`. @@ -564,6 +574,8 @@ def update_callback_data(self, data: CDCData) -> None: def flush(self) -> None: """Will be called by :class:`telegram.ext.Updater` upon receiving a stop signal. Gives the persistence a chance to finish up saving or close a database connection gracefully. + + .. versionchanged:: 14.0 """ REPLACED_BOT: ClassVar[str] = 'bot_instance_replaced_by_ptb_persistence' From 7e556e7259db9656a811a773d29073a109979399 Mon Sep 17 00:00:00 2001 From: starry69 Date: Thu, 12 Aug 2021 10:05:56 +0530 Subject: [PATCH 4/5] Specify changes in versionchanged directive Signed-off-by: starry69 --- telegram/ext/basepersistence.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/telegram/ext/basepersistence.py b/telegram/ext/basepersistence.py index fd106892f59..d693ae921f6 100644 --- a/telegram/ext/basepersistence.py +++ b/telegram/ext/basepersistence.py @@ -447,6 +447,7 @@ def get_callback_data(self) -> Optional[CDCData]: .. versionadded:: 13.6 .. versionchanged:: 14.0 + Changed this method into `@abstractmethod`. Returns: Optional[:class:`telegram.ext.utils.types.CDCData`]: The restored meta data or @@ -521,6 +522,7 @@ def refresh_user_data(self, user_id: int, user_data: UD) -> None: .. versionadded:: 13.6 .. versionchanged:: 14.0 + Changed this method into `@abstractmethod`. Args: user_id (:obj:`int`): The user ID this :attr:`user_data` is associated with. @@ -536,6 +538,7 @@ def refresh_chat_data(self, chat_id: int, chat_data: CD) -> None: .. versionadded:: 13.6 .. versionchanged:: 14.0 + Changed this method into `@abstractmethod`. Args: chat_id (:obj:`int`): The chat ID this :attr:`chat_data` is associated with. @@ -551,6 +554,7 @@ def refresh_bot_data(self, bot_data: BD) -> None: .. versionadded:: 13.6 .. versionchanged:: 14.0 + Changed this method into `@abstractmethod`. Args: bot_data (:class:`telegram.ext.utils.types.BD`): The ``bot_data``. @@ -564,6 +568,7 @@ def update_callback_data(self, data: CDCData) -> None: .. versionadded:: 13.6 .. versionchanged:: 14.0 + Changed this method into `@abstractmethod`. Args: data (:class:`telegram.ext.utils.types.CDCData`): The relevant data to restore @@ -576,6 +581,7 @@ def flush(self) -> None: persistence a chance to finish up saving or close a database connection gracefully. .. versionchanged:: 14.0 + Changed this method into `@abstractmethod`. """ REPLACED_BOT: ClassVar[str] = 'bot_instance_replaced_by_ptb_persistence' From 778bafe651f4fbdbe01f8861d8df53bb1c94718e Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Thu, 12 Aug 2021 08:44:03 +0200 Subject: [PATCH 5/5] Minor nitpicking --- telegram/ext/basepersistence.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/telegram/ext/basepersistence.py b/telegram/ext/basepersistence.py index d693ae921f6..3e03249240d 100644 --- a/telegram/ext/basepersistence.py +++ b/telegram/ext/basepersistence.py @@ -447,7 +447,7 @@ def get_callback_data(self) -> Optional[CDCData]: .. versionadded:: 13.6 .. versionchanged:: 14.0 - Changed this method into `@abstractmethod`. + Changed this method into an ``@abstractmethod``. Returns: Optional[:class:`telegram.ext.utils.types.CDCData`]: The restored meta data or @@ -522,7 +522,7 @@ def refresh_user_data(self, user_id: int, user_data: UD) -> None: .. versionadded:: 13.6 .. versionchanged:: 14.0 - Changed this method into `@abstractmethod`. + Changed this method into an ``@abstractmethod``. Args: user_id (:obj:`int`): The user ID this :attr:`user_data` is associated with. @@ -538,7 +538,7 @@ def refresh_chat_data(self, chat_id: int, chat_data: CD) -> None: .. versionadded:: 13.6 .. versionchanged:: 14.0 - Changed this method into `@abstractmethod`. + Changed this method into an ``@abstractmethod``. Args: chat_id (:obj:`int`): The chat ID this :attr:`chat_data` is associated with. @@ -554,7 +554,7 @@ def refresh_bot_data(self, bot_data: BD) -> None: .. versionadded:: 13.6 .. versionchanged:: 14.0 - Changed this method into `@abstractmethod`. + Changed this method into an ``@abstractmethod``. Args: bot_data (:class:`telegram.ext.utils.types.BD`): The ``bot_data``. @@ -568,7 +568,7 @@ def update_callback_data(self, data: CDCData) -> None: .. versionadded:: 13.6 .. versionchanged:: 14.0 - Changed this method into `@abstractmethod`. + Changed this method into an ``@abstractmethod``. Args: data (:class:`telegram.ext.utils.types.CDCData`): The relevant data to restore @@ -581,7 +581,7 @@ def flush(self) -> None: persistence a chance to finish up saving or close a database connection gracefully. .. versionchanged:: 14.0 - Changed this method into `@abstractmethod`. + Changed this method into an ``@abstractmethod``. """ REPLACED_BOT: ClassVar[str] = 'bot_instance_replaced_by_ptb_persistence'