Skip to content

Commit 9c9fbf3

Browse files
Make BasePersistence Methods Abstract (python-telegram-bot#2624)
Signed-off-by: starry69 <starry369126@outlook.com> Co-authored-by: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com>
1 parent d66eb7a commit 9c9fbf3

File tree

4 files changed

+92
-7
lines changed

4 files changed

+92
-7
lines changed

telegram/ext/basepersistence.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class BasePersistence(Generic[UD, CD, BD], ABC):
7676
store_bot_data (:obj:`bool`, optional): Whether bot_data should be saved by this
7777
persistence class. Default is :obj:`True`.
7878
store_callback_data (:obj:`bool`, optional): Whether callback_data should be saved by this
79-
persistence class. Default is :obj:`False`.
79+
persistence class. Default is :obj:`True`.
8080
8181
.. versionadded:: 13.6
8282
@@ -176,7 +176,7 @@ def __init__(
176176
store_user_data: bool = True,
177177
store_chat_data: bool = True,
178178
store_bot_data: bool = True,
179-
store_callback_data: bool = False,
179+
store_callback_data: bool = True,
180180
):
181181
self.store_user_data = store_user_data
182182
self.store_chat_data = store_chat_data
@@ -439,17 +439,20 @@ def get_bot_data(self) -> BD:
439439
:class:`telegram.ext.utils.types.BD`: The restored bot data.
440440
"""
441441

442+
@abstractmethod
442443
def get_callback_data(self) -> Optional[CDCData]:
443444
"""Will be called by :class:`telegram.ext.Dispatcher` upon creation with a
444445
persistence object. If callback data was stored, it should be returned.
445446
446447
.. versionadded:: 13.6
447448
449+
.. versionchanged:: 14.0
450+
Changed this method into an ``@abstractmethod``.
451+
448452
Returns:
449453
Optional[:class:`telegram.ext.utils.types.CDCData`]: The restored meta data or
450454
:obj:`None`, if no data was stored.
451455
"""
452-
raise NotImplementedError
453456

454457
@abstractmethod
455458
def get_conversations(self, name: str) -> ConversationDict:
@@ -510,56 +513,75 @@ def update_bot_data(self, data: BD) -> None:
510513
:attr:`telegram.ext.Dispatcher.bot_data`.
511514
"""
512515

516+
@abstractmethod
513517
def refresh_user_data(self, user_id: int, user_data: UD) -> None:
514518
"""Will be called by the :class:`telegram.ext.Dispatcher` before passing the
515519
:attr:`user_data` to a callback. Can be used to update data stored in :attr:`user_data`
516520
from an external source.
517521
518522
.. versionadded:: 13.6
519523
524+
.. versionchanged:: 14.0
525+
Changed this method into an ``@abstractmethod``.
526+
520527
Args:
521528
user_id (:obj:`int`): The user ID this :attr:`user_data` is associated with.
522529
user_data (:class:`telegram.ext.utils.types.UD`): The ``user_data`` of a single user.
523530
"""
524531

532+
@abstractmethod
525533
def refresh_chat_data(self, chat_id: int, chat_data: CD) -> None:
526534
"""Will be called by the :class:`telegram.ext.Dispatcher` before passing the
527535
:attr:`chat_data` to a callback. Can be used to update data stored in :attr:`chat_data`
528536
from an external source.
529537
530538
.. versionadded:: 13.6
531539
540+
.. versionchanged:: 14.0
541+
Changed this method into an ``@abstractmethod``.
542+
532543
Args:
533544
chat_id (:obj:`int`): The chat ID this :attr:`chat_data` is associated with.
534545
chat_data (:class:`telegram.ext.utils.types.CD`): The ``chat_data`` of a single chat.
535546
"""
536547

548+
@abstractmethod
537549
def refresh_bot_data(self, bot_data: BD) -> None:
538550
"""Will be called by the :class:`telegram.ext.Dispatcher` before passing the
539551
:attr:`bot_data` to a callback. Can be used to update data stored in :attr:`bot_data`
540552
from an external source.
541553
542554
.. versionadded:: 13.6
543555
556+
.. versionchanged:: 14.0
557+
Changed this method into an ``@abstractmethod``.
558+
544559
Args:
545560
bot_data (:class:`telegram.ext.utils.types.BD`): The ``bot_data``.
546561
"""
547562

563+
@abstractmethod
548564
def update_callback_data(self, data: CDCData) -> None:
549565
"""Will be called by the :class:`telegram.ext.Dispatcher` after a handler has
550566
handled an update.
551567
552568
.. versionadded:: 13.6
553569
570+
.. versionchanged:: 14.0
571+
Changed this method into an ``@abstractmethod``.
572+
554573
Args:
555574
data (:class:`telegram.ext.utils.types.CDCData`): The relevant data to restore
556575
:class:`telegram.ext.CallbackDataCache`.
557576
"""
558-
raise NotImplementedError
559577

578+
@abstractmethod
560579
def flush(self) -> None:
561580
"""Will be called by :class:`telegram.ext.Updater` upon receiving a stop signal. Gives the
562581
persistence a chance to finish up saving or close a database connection gracefully.
582+
583+
.. versionchanged:: 14.0
584+
Changed this method into an ``@abstractmethod``.
563585
"""
564586

565587
REPLACED_BOT: ClassVar[str] = 'bot_instance_replaced_by_ptb_persistence'

telegram/ext/dictpersistence.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,10 @@ def refresh_bot_data(self, bot_data: Dict) -> None:
402402
.. versionadded:: 13.6
403403
.. seealso:: :meth:`telegram.ext.BasePersistence.refresh_bot_data`
404404
"""
405+
406+
def flush(self) -> None:
407+
"""Does nothing.
408+
409+
.. versionadded:: 14.0
410+
.. seealso:: :meth:`telegram.ext.BasePersistence.flush`
411+
"""

tests/test_dispatcher.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,18 @@ def get_conversations(self, name):
632632
def update_conversation(self, name, key, new_state):
633633
pass
634634

635+
def refresh_user_data(self, user_id, user_data):
636+
pass
637+
638+
def refresh_chat_data(self, chat_id, chat_data):
639+
pass
640+
641+
def refresh_bot_data(self, bot_data):
642+
pass
643+
644+
def flush(self):
645+
pass
646+
635647
def start1(b, u):
636648
pass
637649

@@ -776,6 +788,9 @@ def refresh_user_data(self, user_id, user_data):
776788
def refresh_chat_data(self, chat_id, chat_data):
777789
pass
778790

791+
def flush(self):
792+
pass
793+
779794
def callback(update, context):
780795
pass
781796

@@ -845,6 +860,15 @@ def refresh_user_data(self, user_id, user_data):
845860
def refresh_chat_data(self, chat_id, chat_data):
846861
pass
847862

863+
def get_callback_data(self):
864+
pass
865+
866+
def update_callback_data(self, data):
867+
pass
868+
869+
def flush(self):
870+
pass
871+
848872
def callback(update, context):
849873
pass
850874

tests/test_persistence.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ def update_conversation(self, name, key, new_state):
9898
def update_user_data(self, user_id, data):
9999
raise NotImplementedError
100100

101+
def get_callback_data(self):
102+
raise NotImplementedError
103+
104+
def refresh_user_data(self, user_id, user_data):
105+
raise NotImplementedError
106+
107+
def refresh_chat_data(self, chat_id, chat_data):
108+
raise NotImplementedError
109+
110+
def refresh_bot_data(self, bot_data):
111+
raise NotImplementedError
112+
113+
def update_callback_data(self, data):
114+
raise NotImplementedError
115+
116+
def flush(self):
117+
raise NotImplementedError
118+
101119

102120
@pytest.fixture(scope="function")
103121
def base_persistence():
@@ -148,6 +166,18 @@ def update_callback_data(self, data):
148166
def update_conversation(self, name, key, new_state):
149167
raise NotImplementedError
150168

169+
def refresh_user_data(self, user_id, user_data):
170+
pass
171+
172+
def refresh_chat_data(self, chat_id, chat_data):
173+
pass
174+
175+
def refresh_bot_data(self, bot_data):
176+
pass
177+
178+
def flush(self):
179+
pass
180+
151181
return BotPersistence()
152182

153183

@@ -239,9 +269,11 @@ def test_abstract_methods(self, base_persistence):
239269
with pytest.raises(
240270
TypeError,
241271
match=(
242-
'get_bot_data, get_chat_data, get_conversations, '
243-
'get_user_data, update_bot_data, update_chat_data, '
244-
'update_conversation, update_user_data'
272+
'flush, get_bot_data, get_callback_data, '
273+
'get_chat_data, get_conversations, '
274+
'get_user_data, refresh_bot_data, refresh_chat_data, '
275+
'refresh_user_data, update_bot_data, update_callback_data, '
276+
'update_chat_data, update_conversation, update_user_data'
245277
),
246278
):
247279
BasePersistence()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy