From 824b43c4363a325c84e1df9cb8cfbc73abb5351b Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Sat, 14 May 2022 17:27:36 +0300 Subject: [PATCH] #901 Fixed false-positive coercing of Union types in API methods (#912) * #901 Fixed false-positive coercing of Union types in API methods * Added default value for force_reply --- CHANGES/901.bugfix.rst | 1 + aiogram/methods/base.py | 1 + aiogram/types/force_reply.py | 4 +++- tests/test_api/test_methods/test_send_message.py | 7 ++++++- 4 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 CHANGES/901.bugfix.rst diff --git a/CHANGES/901.bugfix.rst b/CHANGES/901.bugfix.rst new file mode 100644 index 00000000..e920c152 --- /dev/null +++ b/CHANGES/901.bugfix.rst @@ -0,0 +1 @@ +Fixed false-positive coercing of Union types in API methods diff --git a/aiogram/methods/base.py b/aiogram/methods/base.py index 4d787a7b..2a42bd09 100644 --- a/aiogram/methods/base.py +++ b/aiogram/methods/base.py @@ -46,6 +46,7 @@ class TelegramMethod(abc.ABC, BaseModel, Generic[TelegramType]): allow_population_by_field_name = True arbitrary_types_allowed = True orm_mode = True + smart_union = True # https://github.com/aiogram/aiogram/issues/901 @root_validator(pre=True) def remove_unset(cls, values: Dict[str, Any]) -> Dict[str, Any]: diff --git a/aiogram/types/force_reply.py b/aiogram/types/force_reply.py index b69ad94c..bd8e8900 100644 --- a/aiogram/types/force_reply.py +++ b/aiogram/types/force_reply.py @@ -2,6 +2,8 @@ from __future__ import annotations from typing import Optional +from pydantic import Field + from .base import MutableTelegramObject @@ -19,7 +21,7 @@ class ForceReply(MutableTelegramObject): Source: https://core.telegram.org/bots/api#forcereply """ - force_reply: bool + force_reply: bool = Field(True, const=True) """Shows reply interface to the user, as if they manually selected the bot's message and tapped 'Reply'""" input_field_placeholder: Optional[str] = None """*Optional*. The placeholder to be shown in the input field when the reply is active; 1-64 characters""" diff --git a/tests/test_api/test_methods/test_send_message.py b/tests/test_api/test_methods/test_send_message.py index c2da672e..35324f34 100644 --- a/tests/test_api/test_methods/test_send_message.py +++ b/tests/test_api/test_methods/test_send_message.py @@ -3,7 +3,7 @@ import datetime import pytest from aiogram.methods import Request, SendMessage -from aiogram.types import Chat, Message +from aiogram.types import Chat, ForceReply, Message from tests.mocked_bot import MockedBot pytestmark = pytest.mark.asyncio @@ -43,3 +43,8 @@ class TestSendMessage: request: Request = bot.get_request() assert request.method == "sendMessage" assert response == prepare_result.result + + async def test_force_reply(self): + # https://github.com/aiogram/aiogram/issues/901 + method = SendMessage(text="test", chat_id=42, reply_markup=ForceReply()) + assert isinstance(method.reply_markup, ForceReply)