From 21351de335f13bd27174e49b40c6a8156690e708 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Mon, 17 Jul 2023 00:10:47 +0300 Subject: [PATCH] Fixed #1217: Fixed union subtypes generation inside arrays of elements --- aiogram/client/bot.py | 52 ++++++++++++++++--- aiogram/methods/answer_inline_query.py | 51 ++++++++++++++++-- aiogram/methods/set_passport_data_errors.py | 28 ++++++++-- aiogram/types/inline_query.py | 48 +++++++++++++++-- pyproject.toml | 2 +- .../test_methods/test_answer_inline_query.py | 10 +++- .../test_set_passport_data_errors.py | 11 +++- 7 files changed, 183 insertions(+), 19 deletions(-) diff --git a/aiogram/client/bot.py b/aiogram/client/bot.py index b7604390..c6721c6f 100644 --- a/aiogram/client/bot.py +++ b/aiogram/client/bot.py @@ -140,7 +140,6 @@ from ..methods import ( from ..types import ( UNSET_PARSE_MODE, BotCommand, - BotCommandScope, BotCommandScopeAllChatAdministrators, BotCommandScopeAllGroupChats, BotCommandScopeAllPrivateChats, @@ -167,7 +166,6 @@ from ..types import ( ForumTopic, GameHighScore, InlineKeyboardMarkup, - InlineQueryResult, InlineQueryResultArticle, InlineQueryResultAudio, InlineQueryResultCachedAudio, @@ -190,7 +188,6 @@ from ..types import ( InlineQueryResultVideo, InlineQueryResultVoice, InputFile, - InputMedia, InputMediaAnimation, InputMediaAudio, InputMediaDocument, @@ -205,7 +202,15 @@ from ..types import ( Message, MessageEntity, MessageId, - PassportElementError, + PassportElementErrorDataField, + PassportElementErrorFile, + PassportElementErrorFiles, + PassportElementErrorFrontSide, + PassportElementErrorReverseSide, + PassportElementErrorSelfie, + PassportElementErrorTranslationFile, + PassportElementErrorTranslationFiles, + PassportElementErrorUnspecified, Poll, ReplyKeyboardMarkup, ReplyKeyboardRemove, @@ -509,7 +514,30 @@ class Bot(ContextInstanceMixin["Bot"]): async def answer_inline_query( self, inline_query_id: str, - results: List[InlineQueryResult], + results: List[ + Union[ + InlineQueryResultCachedAudio, + InlineQueryResultCachedDocument, + InlineQueryResultCachedGif, + InlineQueryResultCachedMpeg4Gif, + InlineQueryResultCachedPhoto, + InlineQueryResultCachedSticker, + InlineQueryResultCachedVideo, + InlineQueryResultCachedVoice, + InlineQueryResultArticle, + InlineQueryResultAudio, + InlineQueryResultContact, + InlineQueryResultGame, + InlineQueryResultDocument, + InlineQueryResultGif, + InlineQueryResultLocation, + InlineQueryResultMpeg4Gif, + InlineQueryResultPhoto, + InlineQueryResultVenue, + InlineQueryResultVideo, + InlineQueryResultVoice, + ] + ], cache_time: Optional[int] = None, is_personal: Optional[bool] = None, next_offset: Optional[str] = None, @@ -3368,7 +3396,19 @@ class Bot(ContextInstanceMixin["Bot"]): async def set_passport_data_errors( self, user_id: int, - errors: List[PassportElementError], + errors: List[ + Union[ + PassportElementErrorDataField, + PassportElementErrorFrontSide, + PassportElementErrorReverseSide, + PassportElementErrorSelfie, + PassportElementErrorFile, + PassportElementErrorFiles, + PassportElementErrorTranslationFile, + PassportElementErrorTranslationFiles, + PassportElementErrorUnspecified, + ] + ], request_timeout: Optional[int] = None, ) -> bool: """ diff --git a/aiogram/methods/answer_inline_query.py b/aiogram/methods/answer_inline_query.py index 53eaf5be..70dc1169 100644 --- a/aiogram/methods/answer_inline_query.py +++ b/aiogram/methods/answer_inline_query.py @@ -1,10 +1,32 @@ from __future__ import annotations -from typing import List, Optional +from typing import List, Optional, Union from pydantic import Field -from ..types import InlineQueryResult, InlineQueryResultsButton +from ..types import ( + InlineQueryResultArticle, + InlineQueryResultAudio, + InlineQueryResultCachedAudio, + InlineQueryResultCachedDocument, + InlineQueryResultCachedGif, + InlineQueryResultCachedMpeg4Gif, + InlineQueryResultCachedPhoto, + InlineQueryResultCachedSticker, + InlineQueryResultCachedVideo, + InlineQueryResultCachedVoice, + InlineQueryResultContact, + InlineQueryResultDocument, + InlineQueryResultGame, + InlineQueryResultGif, + InlineQueryResultLocation, + InlineQueryResultMpeg4Gif, + InlineQueryResultPhoto, + InlineQueryResultsButton, + InlineQueryResultVenue, + InlineQueryResultVideo, + InlineQueryResultVoice, +) from .base import TelegramMethod @@ -22,7 +44,30 @@ class AnswerInlineQuery(TelegramMethod[bool]): inline_query_id: str """Unique identifier for the answered query""" - results: List[InlineQueryResult] + results: List[ + Union[ + InlineQueryResultCachedAudio, + InlineQueryResultCachedDocument, + InlineQueryResultCachedGif, + InlineQueryResultCachedMpeg4Gif, + InlineQueryResultCachedPhoto, + InlineQueryResultCachedSticker, + InlineQueryResultCachedVideo, + InlineQueryResultCachedVoice, + InlineQueryResultArticle, + InlineQueryResultAudio, + InlineQueryResultContact, + InlineQueryResultGame, + InlineQueryResultDocument, + InlineQueryResultGif, + InlineQueryResultLocation, + InlineQueryResultMpeg4Gif, + InlineQueryResultPhoto, + InlineQueryResultVenue, + InlineQueryResultVideo, + InlineQueryResultVoice, + ] + ] """A JSON-serialized array of results for the inline query""" cache_time: Optional[int] = None """The maximum amount of time in seconds that the result of the inline query may be cached on the server. Defaults to 300.""" diff --git a/aiogram/methods/set_passport_data_errors.py b/aiogram/methods/set_passport_data_errors.py index f829fa47..8dbd52b2 100644 --- a/aiogram/methods/set_passport_data_errors.py +++ b/aiogram/methods/set_passport_data_errors.py @@ -1,8 +1,18 @@ from __future__ import annotations -from typing import List +from typing import List, Union -from ..types import PassportElementError +from ..types import ( + PassportElementErrorDataField, + PassportElementErrorFile, + PassportElementErrorFiles, + PassportElementErrorFrontSide, + PassportElementErrorReverseSide, + PassportElementErrorSelfie, + PassportElementErrorTranslationFile, + PassportElementErrorTranslationFiles, + PassportElementErrorUnspecified, +) from .base import TelegramMethod @@ -19,5 +29,17 @@ class SetPassportDataErrors(TelegramMethod[bool]): user_id: int """User identifier""" - errors: List[PassportElementError] + errors: List[ + Union[ + PassportElementErrorDataField, + PassportElementErrorFrontSide, + PassportElementErrorReverseSide, + PassportElementErrorSelfie, + PassportElementErrorFile, + PassportElementErrorFiles, + PassportElementErrorTranslationFile, + PassportElementErrorTranslationFiles, + PassportElementErrorUnspecified, + ] + ] """A JSON-serialized array describing the errors""" diff --git a/aiogram/types/inline_query.py b/aiogram/types/inline_query.py index afb76a8b..e9fb644e 100644 --- a/aiogram/types/inline_query.py +++ b/aiogram/types/inline_query.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, List, Optional +from typing import TYPE_CHECKING, Any, List, Optional, Union from pydantic import Field @@ -8,7 +8,26 @@ from .base import TelegramObject if TYPE_CHECKING: from ..methods import AnswerInlineQuery - from .inline_query_result import InlineQueryResult + from .inline_query_result_article import InlineQueryResultArticle + from .inline_query_result_audio import InlineQueryResultAudio + from .inline_query_result_cached_audio import InlineQueryResultCachedAudio + from .inline_query_result_cached_document import InlineQueryResultCachedDocument + from .inline_query_result_cached_gif import InlineQueryResultCachedGif + from .inline_query_result_cached_mpeg4_gif import InlineQueryResultCachedMpeg4Gif + from .inline_query_result_cached_photo import InlineQueryResultCachedPhoto + from .inline_query_result_cached_sticker import InlineQueryResultCachedSticker + from .inline_query_result_cached_video import InlineQueryResultCachedVideo + from .inline_query_result_cached_voice import InlineQueryResultCachedVoice + from .inline_query_result_contact import InlineQueryResultContact + from .inline_query_result_document import InlineQueryResultDocument + from .inline_query_result_game import InlineQueryResultGame + from .inline_query_result_gif import InlineQueryResultGif + from .inline_query_result_location import InlineQueryResultLocation + from .inline_query_result_mpeg4_gif import InlineQueryResultMpeg4Gif + from .inline_query_result_photo import InlineQueryResultPhoto + from .inline_query_result_venue import InlineQueryResultVenue + from .inline_query_result_video import InlineQueryResultVideo + from .inline_query_result_voice import InlineQueryResultVoice from .inline_query_results_button import InlineQueryResultsButton from .location import Location from .user import User @@ -36,7 +55,30 @@ class InlineQuery(TelegramObject): def answer( self, - results: List[InlineQueryResult], + results: List[ + Union[ + InlineQueryResultCachedAudio, + InlineQueryResultCachedDocument, + InlineQueryResultCachedGif, + InlineQueryResultCachedMpeg4Gif, + InlineQueryResultCachedPhoto, + InlineQueryResultCachedSticker, + InlineQueryResultCachedVideo, + InlineQueryResultCachedVoice, + InlineQueryResultArticle, + InlineQueryResultAudio, + InlineQueryResultContact, + InlineQueryResultGame, + InlineQueryResultDocument, + InlineQueryResultGif, + InlineQueryResultLocation, + InlineQueryResultMpeg4Gif, + InlineQueryResultPhoto, + InlineQueryResultVenue, + InlineQueryResultVideo, + InlineQueryResultVoice, + ] + ], cache_time: Optional[int] = None, is_personal: Optional[bool] = None, next_offset: Optional[str] = None, diff --git a/pyproject.toml b/pyproject.toml index 31e4e968..cffd123b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -149,7 +149,7 @@ features = [ "test", ] extra-dependencies = [ - "butcher @ git+https://github.com/aiogram/butcher.git@v0.1.16" + "butcher @ git+https://github.com/aiogram/butcher.git@v0.1.17" ] [tool.hatch.envs.dev.scripts] diff --git a/tests/test_api/test_methods/test_answer_inline_query.py b/tests/test_api/test_methods/test_answer_inline_query.py index a0cec362..cd8ba266 100644 --- a/tests/test_api/test_methods/test_answer_inline_query.py +++ b/tests/test_api/test_methods/test_answer_inline_query.py @@ -2,6 +2,7 @@ from aiogram import Bot from aiogram.methods import AnswerInlineQuery, Request from aiogram.types import ( InlineQueryResult, + InlineQueryResultArticle, InlineQueryResultPhoto, InputTextMessageContent, ) @@ -13,7 +14,14 @@ class TestAnswerInlineQuery: prepare_result = bot.add_result_for(AnswerInlineQuery, ok=True, result=True) response: bool = await bot.answer_inline_query( - inline_query_id="query id", results=[InlineQueryResult()] + inline_query_id="query id", + results=[ + InlineQueryResultArticle( + id="1", + title="title", + input_message_content=InputTextMessageContent(message_text="text"), + ) + ], ) request = bot.get_request() assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_set_passport_data_errors.py b/tests/test_api/test_methods/test_set_passport_data_errors.py index ecc2425b..0e5647fb 100644 --- a/tests/test_api/test_methods/test_set_passport_data_errors.py +++ b/tests/test_api/test_methods/test_set_passport_data_errors.py @@ -1,5 +1,5 @@ from aiogram.methods import Request, SetPassportDataErrors -from aiogram.types import PassportElementError +from aiogram.types import PassportElementError, PassportElementErrorFile from tests.mocked_bot import MockedBot @@ -8,7 +8,14 @@ class TestSetPassportDataErrors: prepare_result = bot.add_result_for(SetPassportDataErrors, ok=True, result=True) response: bool = await bot.set_passport_data_errors( - user_id=42, errors=[PassportElementError()] + user_id=42, + errors=[ + PassportElementErrorFile( + type="type", + file_hash="hash", + message="message", + ) + ], ) request = bot.get_request() assert response == prepare_result.result