mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-17 04:39:55 +01:00
Only Persist Arbitrary callback_data
if ExtBot.callback_data_cache
is Present (#3384)
This commit is contained in:
parent
1724212458
commit
867f742d08
3 changed files with 19 additions and 17 deletions
|
@ -451,13 +451,16 @@ class Application(Generic[BT, CCT, UD, CD, BD, JQ], AbstractAsyncContextManager)
|
|||
raise ValueError(
|
||||
f"bot_data must be of type {self.context_types.bot_data.__name__}"
|
||||
)
|
||||
if self.persistence.store_data.callback_data:
|
||||
|
||||
# Mypy doesn't know that persistence.set_bot (see above) already checks that
|
||||
# self.bot is an instance of ExtBot if callback_data should be stored ...
|
||||
if self.persistence.store_data.callback_data and (
|
||||
self.bot.callback_data_cache is not None # type: ignore[attr-defined]
|
||||
):
|
||||
persistent_data = await self.persistence.get_callback_data()
|
||||
if persistent_data is not None:
|
||||
if not isinstance(persistent_data, tuple) or len(persistent_data) != 2:
|
||||
raise ValueError("callback_data must be a tuple of length 2")
|
||||
# Mypy doesn't know that persistence.set_bot (see above) already checks that
|
||||
# self.bot is an instance of ExtBot if callback_data should be stored ...
|
||||
self.bot.callback_data_cache.load_persistence_data( # type: ignore[attr-defined]
|
||||
persistent_data
|
||||
)
|
||||
|
@ -1348,9 +1351,11 @@ class Application(Generic[BT, CCT, UD, CD, BD, JQ], AbstractAsyncContextManager)
|
|||
|
||||
coroutines: Set[Coroutine] = set()
|
||||
|
||||
if self.persistence.store_data.callback_data:
|
||||
# Mypy doesn't know that persistence.set_bot (see above) already checks that
|
||||
# self.bot is an instance of ExtBot if callback_data should be stored ...
|
||||
# Mypy doesn't know that persistence.set_bot (see above) already checks that
|
||||
# self.bot is an instance of ExtBot if callback_data should be stored ...
|
||||
if self.persistence.store_data.callback_data and (
|
||||
self.bot.callback_data_cache is not None # type: ignore[attr-defined]
|
||||
):
|
||||
coroutines.add(
|
||||
self.persistence.update_callback_data(
|
||||
deepcopy(
|
||||
|
|
|
@ -178,16 +178,10 @@ class BasePersistence(Generic[UD, CD, BD], ABC):
|
|||
|
||||
Raises:
|
||||
:exc:`TypeError`: If :attr:`PersistenceInput.callback_data` is :obj:`True` and the
|
||||
:paramref:`bot` is not an instance of :class:`telegram.ext.ExtBot` or
|
||||
:attr:`~telegram.ext.ExtBot.callback_data_cache` is :obj:`None`.
|
||||
:paramref:`bot` is not an instance of :class:`telegram.ext.ExtBot`.
|
||||
"""
|
||||
if self.store_data.callback_data and (
|
||||
not isinstance(bot, ExtBot) or bot.callback_data_cache is None
|
||||
):
|
||||
raise TypeError(
|
||||
"callback_data can only be stored when using telegram.ext.ExtBot with arbitrary "
|
||||
"callback_data enabled. "
|
||||
)
|
||||
if self.store_data.callback_data and (not isinstance(bot, ExtBot)):
|
||||
raise TypeError("callback_data can only be stored when using telegram.ext.ExtBot.")
|
||||
|
||||
self.bot = bot
|
||||
|
||||
|
|
|
@ -390,8 +390,11 @@ class TestBasePersistence:
|
|||
with pytest.raises(TypeError, match="when using telegram.ext.ExtBot"):
|
||||
papp.persistence.set_bot(Bot(papp.bot.token))
|
||||
|
||||
with pytest.raises(TypeError, match="when using telegram.ext.ExtBot"):
|
||||
papp.persistence.set_bot(ExtBot(papp.bot.token))
|
||||
# just making sure that setting an ExtBoxt without callback_data_cache doesn't raise an
|
||||
# error even though store_callback_data is True
|
||||
bot = ExtBot(papp.bot.token)
|
||||
assert bot.callback_data_cache is None
|
||||
assert papp.persistence.set_bot(bot) is None
|
||||
|
||||
def test_construction_with_bad_persistence(self, caplog, bot):
|
||||
class MyPersistence:
|
||||
|
|
Loading…
Add table
Reference in a new issue