diff --git a/aiogram/utils/chat_member.py b/aiogram/utils/chat_member.py new file mode 100644 index 00000000..9d86823d --- /dev/null +++ b/aiogram/utils/chat_member.py @@ -0,0 +1,37 @@ +from typing import Tuple, Type, Union + +from pydantic import Field, TypeAdapter +from typing_extensions import Annotated + +from aiogram.types import ( + ChatMember, + ChatMemberAdministrator, + ChatMemberBanned, + ChatMemberLeft, + ChatMemberMember, + ChatMemberOwner, + ChatMemberRestricted, +) + +ChatMemberUnion = Union[ + ChatMemberOwner, + ChatMemberAdministrator, + ChatMemberMember, + ChatMemberRestricted, + ChatMemberLeft, + ChatMemberBanned, +] + +ChatMemberCollection = Tuple[Type[ChatMember], ...] + +ChatMemberAdapter = TypeAdapter( + Annotated[ + ChatMemberUnion, + Field(discriminator="status"), + ] +) + +ADMINS: ChatMemberCollection = (ChatMemberOwner, ChatMemberAdministrator) +USERS: ChatMemberCollection = (ChatMemberMember, ChatMemberRestricted) +MEMBERS: ChatMemberCollection = ADMINS + USERS +NOT_MEMBERS: ChatMemberCollection = (ChatMemberLeft, ChatMemberBanned) diff --git a/aiogram/utils/chat_member_adapter.py b/aiogram/utils/chat_member_adapter.py deleted file mode 100644 index e97fa975..00000000 --- a/aiogram/utils/chat_member_adapter.py +++ /dev/null @@ -1,22 +0,0 @@ -from typing import Union - -from pydantic import Field, TypeAdapter -from typing_extensions import Annotated - -from aiogram import types - -ChatMemberUnion = Union[ - types.ChatMemberOwner, - types.ChatMemberAdministrator, - types.ChatMemberMember, - types.ChatMemberRestricted, - types.ChatMemberLeft, - types.ChatMemberBanned, -] - -ChatMemberAdapter = TypeAdapter( - Annotated[ - ChatMemberUnion, - Field(discriminator="status"), - ] -) diff --git a/docs/migration_2_to_3.rst b/docs/migration_2_to_3.rst index cd74973a..b3df34df 100644 --- a/docs/migration_2_to_3.rst +++ b/docs/migration_2_to_3.rst @@ -253,15 +253,15 @@ ChatMember tools .. code-block:: # Version 3.x - from aiogram.utils.chat_member_adapter import ChatMemberAdapter + from aiogram.utils.chat_member import ChatMemberAdapter chat_member = ChatMemberAdapter.validate_python(dict_data) - Now :class:`aiogram.types.chat_member.ChatMember` and all its child classes no longer contain methods for checking for membership in certain logical groups. - As a substitute, you can create such groups yourself and check their entry using - the :func:`isinstance` function + As a substitute, you can use pre-defined groups or create such groups yourself + and check their entry using the :func:`isinstance` function .. code-block:: @@ -277,13 +277,7 @@ ChatMember tools # Version 3.x - ADMINS = (ChatMemberOwner, ChatMemberAdministrator) - MEMBERS = ( - ChatMemberOwner, - ChatMemberAdministrator, - ChatMemberMember, - ChatMemberRestricted, - ) + from aiogram.utils.chat_member import ADMINS, MEMBERS if isinstance(chat_member, ADMINS): print("ChatMember is chat admin") @@ -292,6 +286,6 @@ ChatMember tools print("ChatMember is in the chat") .. note:: - This way you can independently create any group that fits the logic of your application. + You also can independently create group similar to ADMINS that fits the logic of your application. - E.g., you can create a PUNISHED group and include banned and restricted members there + E.g., you can create a PUNISHED group and include banned and restricted members there! diff --git a/tests/test_utils/test_chat_member_adapter.py b/tests/test_utils/test_chat_member.py similarity index 97% rename from tests/test_utils/test_chat_member_adapter.py rename to tests/test_utils/test_chat_member.py index 554084a4..2f0f9eef 100644 --- a/tests/test_utils/test_chat_member_adapter.py +++ b/tests/test_utils/test_chat_member.py @@ -13,7 +13,7 @@ from aiogram.types import ( ChatMemberRestricted, User, ) -from aiogram.utils.chat_member_adapter import ChatMemberAdapter +from aiogram.utils.chat_member import ChatMemberAdapter USER = User( id=42,