Override Bot.__deepcopy__ to Raise TypeError (#3446)

This commit is contained in:
Bibo-Joshi 2022-12-30 10:50:49 +01:00 committed by GitHub
parent b8fbb89fae
commit 0a6725852f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 3 deletions

View file

@ -4,3 +4,4 @@ telegram.Bot
.. autoclass:: telegram.Bot
:members:
:show-inheritance:
:special-members: __reduce__, __deepcopy__

View file

@ -141,7 +141,8 @@ class Bot(TelegramObject, AbstractAsyncContextManager):
passing files.
* Bots should not be serialized since if you for e.g. change the bots token, then your
serialized instance will not reflect that change. Trying to pickle a bot instance will
raise :exc:`pickle.PicklingError`.
raise :exc:`pickle.PicklingError`. Trying to deepcopy a bot instance will raise
:exc:`TypeError`.
Examples:
:any:`Raw API Bot <examples.rawapibot>`
@ -167,6 +168,7 @@ class Bot(TelegramObject, AbstractAsyncContextManager):
:class:`telegram.ext.Defaults`, please use the subclass :class:`telegram.ext.ExtBot`
instead.
* Attempting to pickle a bot instance will now raise :exc:`pickle.PicklingError`.
* Attempting to deepcopy a bot instance will now raise :exc:`TypeError`.
* The following are now keyword-only arguments in Bot methods:
``location``, ``filename``, ``venue``, ``contact``,
``{read, write, connect, pool}_timeout``, ``api_kwargs``. Use a named argument for those,
@ -302,9 +304,27 @@ class Bot(TelegramObject, AbstractAsyncContextManager):
return self._private_key
def __reduce__(self) -> NoReturn:
"""Called by pickle.dumps(). Serializing bots is unadvisable, so we forbid pickling."""
"""Customizes how :func:`copy.deepcopy` processes objects of this type. Bots can not
be pickled and this method will always raise an exception.
.. versionadded:: 20.0
Raises:
:exc:`pickle.PicklingError`
"""
raise pickle.PicklingError("Bot objects cannot be pickled!")
def __deepcopy__(self, memodict: dict) -> NoReturn:
"""Customizes how :func:`copy.deepcopy` processes objects of this type. Bots can not
be deepcopied and this method will always raise an exception.
.. versionadded:: 20.0
Raises:
:exc:`TypeError`
"""
raise TypeError("Bot objects cannot be deepcopied!")
# TODO: After https://youtrack.jetbrains.com/issue/PY-50952 is fixed, we can revisit this and
# consider adding Paramspec from typing_extensions to properly fix this. Currently a workaround
def _log(func: Any): # type: ignore[no-untyped-def] # skipcq: PY-D0003

View file

@ -436,6 +436,10 @@ class TestBot:
with pytest.raises(pickle.PicklingError, match="Bot objects cannot be pickled"):
pickle.dumps(bot)
def test_bot_deepcopy_error(self, bot):
with pytest.raises(TypeError, match="Bot objects cannot be deepcopied"):
copy.deepcopy(bot)
@bot_methods(ext_bot=False)
async def test_defaults_handling(
self, bot_class, bot_method_name, bot_method, bot, raw_bot, monkeypatch