mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Removed deprecated arguments from Bot class (#1494)
* Remove deprecated attributes from Bot class The deprecated attributes `parse_mode`, `disable_web_page_preview`, and `protect_content` have been removed from the Bot class. Additionally, the associated warnings and test cases have been deleted. These attributes should now be passed using the `default=DefaultBotProperties(...)` syntax instead. * Added docs and changelog
This commit is contained in:
parent
895f4f8dce
commit
b5d94f17b5
5 changed files with 117 additions and 82 deletions
2
CHANGES/1494.removal.rst
Normal file
2
CHANGES/1494.removal.rst
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
Removed deprecated arguments from Bot class
|
||||||
|
:code:`parse_mode`, :code:`disable_web_page_preview`, :code:`protect_content` as previously announced in v3.4.0.
|
||||||
|
|
@ -3,7 +3,6 @@ from __future__ import annotations
|
||||||
import datetime
|
import datetime
|
||||||
import io
|
import io
|
||||||
import pathlib
|
import pathlib
|
||||||
import warnings
|
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from types import TracebackType
|
from types import TracebackType
|
||||||
from typing import (
|
from typing import (
|
||||||
|
|
@ -251,10 +250,8 @@ class Bot:
|
||||||
self,
|
self,
|
||||||
token: str,
|
token: str,
|
||||||
session: Optional[BaseSession] = None,
|
session: Optional[BaseSession] = None,
|
||||||
parse_mode: Optional[str] = None,
|
|
||||||
disable_web_page_preview: Optional[bool] = None,
|
|
||||||
protect_content: Optional[bool] = None,
|
|
||||||
default: Optional[DefaultBotProperties] = None,
|
default: Optional[DefaultBotProperties] = None,
|
||||||
|
**kwargs: Any,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Bot class
|
Bot class
|
||||||
|
|
@ -262,12 +259,6 @@ class Bot:
|
||||||
:param token: Telegram Bot token `Obtained from @BotFather <https://t.me/BotFather>`_
|
:param token: Telegram Bot token `Obtained from @BotFather <https://t.me/BotFather>`_
|
||||||
:param session: HTTP Client session (For example AiohttpSession).
|
:param session: HTTP Client session (For example AiohttpSession).
|
||||||
If not specified it will be automatically created.
|
If not specified it will be automatically created.
|
||||||
:param parse_mode: Default parse mode.
|
|
||||||
If specified it will be propagated into the API methods at runtime.
|
|
||||||
:param disable_web_page_preview: Default disable_web_page_preview mode.
|
|
||||||
If specified it will be propagated into the API methods at runtime.
|
|
||||||
:param protect_content: Default protect_content mode.
|
|
||||||
If specified it will be propagated into the API methods at runtime.
|
|
||||||
:param default: Default bot properties.
|
:param default: Default bot properties.
|
||||||
If specified it will be propagated into the API methods at runtime.
|
If specified it will be propagated into the API methods at runtime.
|
||||||
:raise TokenValidationError: When token has invalid format this exception will be raised
|
:raise TokenValidationError: When token has invalid format this exception will be raised
|
||||||
|
|
@ -278,24 +269,33 @@ class Bot:
|
||||||
if session is None:
|
if session is None:
|
||||||
session = AiohttpSession()
|
session = AiohttpSession()
|
||||||
if default is None:
|
if default is None:
|
||||||
default = DefaultBotProperties(
|
default = DefaultBotProperties()
|
||||||
parse_mode=parse_mode,
|
|
||||||
link_preview_is_disabled=disable_web_page_preview,
|
|
||||||
protect_content=protect_content,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.session = session
|
self.session = session
|
||||||
|
|
||||||
|
# Few arguments are completely removed in 3.7.0 version
|
||||||
|
# Temporary solution to raise an error if user passed these arguments
|
||||||
|
# with explanation how to fix it
|
||||||
|
parse_mode = kwargs.get("parse_mode", None)
|
||||||
|
link_preview_is_disabled = kwargs.get("disable_web_page_preview", None)
|
||||||
|
protect_content = kwargs.get("protect_content", None)
|
||||||
if (
|
if (
|
||||||
parse_mode is not None
|
parse_mode is not None
|
||||||
or disable_web_page_preview is not None
|
or link_preview_is_disabled is not None
|
||||||
or protect_content is not None
|
or protect_content is not None
|
||||||
):
|
):
|
||||||
warnings.warn(
|
example_kwargs = {
|
||||||
|
"parse_mode": parse_mode,
|
||||||
|
"link_preview_is_disabled": link_preview_is_disabled,
|
||||||
|
"protect_content": protect_content,
|
||||||
|
}
|
||||||
|
replacement_spec = ", ".join(
|
||||||
|
f"{k}={v!r}" for k, v in example_kwargs.items() if v is not None
|
||||||
|
)
|
||||||
|
raise TypeError(
|
||||||
"Passing `parse_mode`, `disable_web_page_preview` or `protect_content` "
|
"Passing `parse_mode`, `disable_web_page_preview` or `protect_content` "
|
||||||
"to Bot initializer is deprecated. This arguments will be removed in 3.7.0 version\n"
|
"to Bot initializer is not supported anymore. These arguments have been removed "
|
||||||
"Use `default=DefaultBotProperties(...)` instead.",
|
f"in 3.7.0 version. Use `default=DefaultBotProperties({replacement_spec})` argument instead."
|
||||||
category=DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
self.default = default
|
self.default = default
|
||||||
|
|
@ -314,36 +314,6 @@ class Bot:
|
||||||
) -> None:
|
) -> None:
|
||||||
await self.session.close()
|
await self.session.close()
|
||||||
|
|
||||||
@property
|
|
||||||
def parse_mode(self) -> Optional[str]:
|
|
||||||
warnings.warn(
|
|
||||||
"Accessing `parse_mode` from Bot instance is deprecated. This attribute will be removed in 3.5.0 version\n"
|
|
||||||
"Use `bot.default.parse_mode` instead.",
|
|
||||||
category=DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
return self.default.parse_mode
|
|
||||||
|
|
||||||
@property
|
|
||||||
def disable_web_page_preview(self) -> Optional[bool]:
|
|
||||||
warnings.warn(
|
|
||||||
"Accessing `disable_web_page_preview` from Bot instance is deprecated. This attribute will be removed in 3.5.0 version\n"
|
|
||||||
"Use `bot.default.link_preview_is_disabled` instead.",
|
|
||||||
category=DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
return self.default.link_preview_is_disabled
|
|
||||||
|
|
||||||
@property
|
|
||||||
def protect_content(self) -> Optional[bool]:
|
|
||||||
warnings.warn(
|
|
||||||
"Accessing `protect_content` from Bot instance is deprecated. This attribute will be removed in 3.5.0 version\n"
|
|
||||||
"Use `bot.default.protect_content` instead.",
|
|
||||||
category=DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
return self.default.protect_content
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def token(self) -> str:
|
def token(self) -> str:
|
||||||
return self.__token
|
return self.__token
|
||||||
|
|
|
||||||
81
docs/api/defaults.rst
Normal file
81
docs/api/defaults.rst
Normal file
|
|
@ -0,0 +1,81 @@
|
||||||
|
===============
|
||||||
|
Global defaults
|
||||||
|
===============
|
||||||
|
|
||||||
|
aiogram provides mechanism to set some global defaults for all requests to Telegram Bot API
|
||||||
|
in your application using :class:`aiogram.client.default.DefaultBotProperties` class.
|
||||||
|
|
||||||
|
There are some properties that can be set:
|
||||||
|
|
||||||
|
.. autoclass:: aiogram.client.default.DefaultBotProperties
|
||||||
|
:members:
|
||||||
|
:member-order: bysource
|
||||||
|
:undoc-members: True
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
If you need to override default properties for some requests, you should use `aiogram.client.default.DefaultBotProperties`
|
||||||
|
only for properties that you want to set as defaults and pass explicit values for other properties.
|
||||||
|
|
||||||
|
.. danger::
|
||||||
|
|
||||||
|
If you upgrading from aiogram 3.0-3.6 to 3.7,
|
||||||
|
you should update your code to use `aiogram.client.default.DefaultBotProperties`.
|
||||||
|
|
||||||
|
Example
|
||||||
|
=======
|
||||||
|
|
||||||
|
Here is an example of setting default parse mode for all requests to Telegram Bot API:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
bot = Bot(
|
||||||
|
token=...,
|
||||||
|
defaults=DefaultBotProperties(
|
||||||
|
parse_mode=ParseMode.HTML,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
In this case all messages sent by this bot will be parsed as HTML, so you don't need to specify `parse_mode`
|
||||||
|
in every message you send.
|
||||||
|
|
||||||
|
Instead of
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
await bot.send_message(chat_id, text, parse_mode=ParseMode.HTML)
|
||||||
|
|
||||||
|
you can use
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
await bot.send_message(chat_id, text)
|
||||||
|
|
||||||
|
and the message will be sent with HTML parse mode.
|
||||||
|
|
||||||
|
In some cases you may want to override default properties for some requests. You can do it by passing
|
||||||
|
explicit values to the method:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
await bot.send_message(chat_id, text, parse_mode=ParseMode.MARKDOWN_V2)
|
||||||
|
|
||||||
|
In this case the message will be sent with Markdown parse mode instead of default HTML.
|
||||||
|
|
||||||
|
Another example of overriding default properties:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
await bot.send_message(chat_id, text, parse_mode=None)
|
||||||
|
|
||||||
|
In this case the message will be send withoout parse mode, even if default parse mode is set it may be useful
|
||||||
|
if you want to send message with plain text or :ref:`aiogram.types.message_entity.MessageEntity`.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
await bot.send_message(
|
||||||
|
chat_id=chat_id,
|
||||||
|
text=text,
|
||||||
|
entities=[MessageEntity(type='bold', offset=0, length=4)],
|
||||||
|
parse_mode=None
|
||||||
|
)
|
||||||
|
|
@ -14,3 +14,4 @@ All methods and types is fully autogenerated from Telegram Bot API docs by parse
|
||||||
enums/index
|
enums/index
|
||||||
download_file
|
download_file
|
||||||
upload_file
|
upload_file
|
||||||
|
defaults
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ from aiogram.client.session.aiohttp import AiohttpSession
|
||||||
from aiogram.client.telegram import TelegramAPIServer
|
from aiogram.client.telegram import TelegramAPIServer
|
||||||
from aiogram.methods import GetFile, GetMe
|
from aiogram.methods import GetFile, GetMe
|
||||||
from aiogram.types import File, PhotoSize
|
from aiogram.types import File, PhotoSize
|
||||||
from tests.deprecated import check_deprecated
|
|
||||||
from tests.mocked_bot import MockedBot
|
from tests.mocked_bot import MockedBot
|
||||||
from tests.test_api.test_client.test_session.test_base_session import CustomSession
|
from tests.test_api.test_client.test_session.test_base_session import CustomSession
|
||||||
|
|
||||||
|
|
@ -55,36 +54,18 @@ class TestBot:
|
||||||
|
|
||||||
mocked_close.assert_awaited_once()
|
mocked_close.assert_awaited_once()
|
||||||
|
|
||||||
def test_init_default(self):
|
@pytest.mark.parametrize(
|
||||||
with check_deprecated(
|
"kwargs",
|
||||||
max_version="3.7.0",
|
[
|
||||||
exception=TypeError,
|
{"parse_mode": "HTML"},
|
||||||
):
|
{"disable_web_page_preview": True},
|
||||||
bot = Bot(token="42:Test", parse_mode="HTML")
|
{"protect_content": True},
|
||||||
|
{"parse_mode": True, "disable_web_page_preview": True},
|
||||||
def test_deprecated_parse_mode(self):
|
],
|
||||||
with check_deprecated(
|
)
|
||||||
max_version="3.7.0",
|
def test_init_default(self, kwargs):
|
||||||
exception=AttributeError,
|
with pytest.raises(TypeError):
|
||||||
):
|
Bot(token="42:Test", **kwargs)
|
||||||
bot = Bot(token="42:Test", parse_mode="HTML")
|
|
||||||
assert bot.parse_mode == "HTML"
|
|
||||||
|
|
||||||
def test_disable_web_page_preview(self):
|
|
||||||
with check_deprecated(
|
|
||||||
max_version="3.7.0",
|
|
||||||
exception=TypeError,
|
|
||||||
):
|
|
||||||
bot = Bot(token="42:Test", disable_web_page_preview=True)
|
|
||||||
assert bot.disable_web_page_preview is True
|
|
||||||
|
|
||||||
def test_deprecated_protect_content(self):
|
|
||||||
with check_deprecated(
|
|
||||||
max_version="3.7.0",
|
|
||||||
exception=AttributeError,
|
|
||||||
):
|
|
||||||
bot = Bot(token="42:Test", protect_content=True)
|
|
||||||
assert bot.protect_content is True
|
|
||||||
|
|
||||||
def test_hashable(self):
|
def test_hashable(self):
|
||||||
bot = Bot("42:TEST")
|
bot = Bot("42:TEST")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue