Skip to content

Make basepersistence methods abstractmethod #2624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions telegram/ext/basepersistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -439,17 +439,20 @@ 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.

.. versionadded:: 13.6

.. versionchanged:: 14.0
Changed this method into an ``@abstractmethod``.

Returns:
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:
Expand Down Expand Up @@ -510,56 +513,75 @@ 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`
from an external source.

.. versionadded:: 13.6

.. versionchanged:: 14.0
Changed this method into an ``@abstractmethod``.

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.
"""

@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`
from an external source.

.. versionadded:: 13.6

.. versionchanged:: 14.0
Changed this method into an ``@abstractmethod``.

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.
"""

@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`
from an external source.

.. versionadded:: 13.6

.. versionchanged:: 14.0
Changed this method into an ``@abstractmethod``.

Args:
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.

.. versionadded:: 13.6

.. versionchanged:: 14.0
Changed this method into an ``@abstractmethod``.

Args:
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.

.. versionchanged:: 14.0
Changed this method into an ``@abstractmethod``.
"""

REPLACED_BOT: ClassVar[str] = 'bot_instance_replaced_by_ptb_persistence'
Expand Down
7 changes: 7 additions & 0 deletions telegram/ext/dictpersistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
"""
24 changes: 24 additions & 0 deletions tests/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
38 changes: 35 additions & 3 deletions tests/test_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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()


Expand Down Expand Up @@ -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()
Expand Down