diff --git a/aiogram/exceptions.py b/aiogram/exceptions.py index 514d7382..4ef5baa9 100644 --- a/aiogram/exceptions.py +++ b/aiogram/exceptions.py @@ -1,7 +1,4 @@ -from textwrap import indent -from typing import List, Optional, Set - -from pydantic import ValidationError +from typing import Optional from aiogram.methods import TelegramMethod from aiogram.methods.base import TelegramType @@ -104,17 +101,3 @@ class RestartingTelegram(TelegramServerError): class TelegramEntityTooLarge(TelegramNetworkError): url = "https://core.telegram.org/bots/api#sending-files" - - -class FiltersResolveError(DetailedAiogramError): - def __init__(self, unresolved_fields: Set[str], possible_cases: List[ValidationError]) -> None: - possible_cases_str = "\n".join( - " - " + indent(str(e), " " * 4).lstrip() for e in possible_cases - ) - message = f"Unknown keyword filters: {unresolved_fields}" - if possible_cases_str: - message += f"\n Possible cases:\n{possible_cases_str}" - - super().__init__(message=message) - self.unresolved_fields = unresolved_fields - self.possible_cases = possible_cases diff --git a/aiogram/filters/command.py b/aiogram/filters/command.py index 4d8a0143..01993efb 100644 --- a/aiogram/filters/command.py +++ b/aiogram/filters/command.py @@ -65,7 +65,7 @@ class Command(Filter): commands = [commands] if not isinstance(commands, Iterable): - ValueError( + raise ValueError( "Command filter only supports str, re.Pattern, BotCommand object" " or their Iterable" ) @@ -244,8 +244,6 @@ class CommandStart(Command): def __str__(self) -> str: return self._signature_to_string( - *self.commands, - prefix=self.prefix, ignore_case=self.ignore_case, ignore_mention=self.ignore_mention, magic=self.magic, diff --git a/tests/test_dispatcher/test_dispatcher.py b/tests/test_dispatcher/test_dispatcher.py index 50a4ef10..c1dd05fc 100644 --- a/tests/test_dispatcher/test_dispatcher.py +++ b/tests/test_dispatcher/test_dispatcher.py @@ -77,6 +77,12 @@ class TestDispatcher: assert dp.update.handlers[0].callback == dp._listen_update assert dp.update.outer_middleware + def test_init_args(self, bot: MockedBot): + with pytest.raises(TypeError): + Dispatcher(bot) + with pytest.raises(TypeError): + Dispatcher(storage=bot) + def test_data_bind(self): dp = Dispatcher() assert dp.get("foo") is None diff --git a/tests/test_dispatcher/test_event/test_handler.py b/tests/test_dispatcher/test_event/test_handler.py index 6fd59947..1c787b3c 100644 --- a/tests/test_dispatcher/test_event/test_handler.py +++ b/tests/test_dispatcher/test_event/test_handler.py @@ -2,12 +2,14 @@ import functools from typing import Any, Dict, Union import pytest +from magic_filter import F as A from aiogram import F from aiogram.dispatcher.event.handler import CallableMixin, FilterObject, HandlerObject from aiogram.filters import Filter from aiogram.handlers import BaseHandler from aiogram.types import Update +from aiogram.utils.warnings import Recommendation pytestmark = pytest.mark.asyncio @@ -209,3 +211,7 @@ class TestHandlerObject: assert len(handler.filters) == 1 result = await handler.call(Update(update_id=42)) assert result == 42 + + def test_warn_another_magic(self): + with pytest.warns(Recommendation): + FilterObject(callback=A.test.is_(True)) diff --git a/tests/test_filters/test_command.py b/tests/test_filters/test_command.py index 1cdd0b70..84c2350a 100644 --- a/tests/test_filters/test_command.py +++ b/tests/test_filters/test_command.py @@ -6,13 +6,30 @@ import pytest from aiogram import F from aiogram.filters import Command, CommandObject from aiogram.filters.command import CommandStart -from aiogram.types import Chat, Message, User +from aiogram.types import BotCommand, Chat, Message, User from tests.mocked_bot import MockedBot pytestmark = pytest.mark.asyncio class TestCommandFilter: + def test_commands_not_iterable(self): + with pytest.raises(ValueError): + Command(commands=1) + + def test_bad_type(self): + with pytest.raises(ValueError): + Command(1) + + def test_without_args(self): + with pytest.raises(ValueError): + Command() + + def test_resolve_bot_command(self): + command = Command(BotCommand(command="test", description="Test")) + assert isinstance(command.commands[0], str) + assert command.commands[0] == "test" + def test_convert_to_list(self): cmd = Command(commands="start") assert cmd.commands @@ -86,6 +103,7 @@ class TestCommandFilter: ), True, ], + [None, False], ], ) async def test_call(self, message: Message, result: bool, bot: MockedBot): @@ -121,6 +139,19 @@ class TestCommandFilter: command_obj: CommandObject = result["command"] assert command_obj.mention is None + def test_str(self): + cmd = Command(commands=["start"]) + assert str(cmd) == "Command('start', prefix='/', ignore_case=False, ignore_mention=False)" + + +class TestCommandStart: + def test_str(self): + cmd = CommandStart() + assert ( + str(cmd) + == "CommandStart(ignore_case=False, ignore_mention=False, deep_link=False, deep_link_encoded=False)" + ) + class TestCommandObject: @pytest.mark.parametrize( @@ -168,7 +199,3 @@ class TestCommandObject: cmd.update_handler_flags(flags) assert len(flags["commands"]) == 2 - - def test_str(self): - cmd = Command(commands=["start"]) - assert str(cmd) == "Command('start', prefix='/', ignore_case=False, ignore_mention=False)" diff --git a/tests/test_filters/test_exception.py b/tests/test_filters/test_exception.py index aa3cbfed..2480e76c 100644 --- a/tests/test_filters/test_exception.py +++ b/tests/test_filters/test_exception.py @@ -57,6 +57,10 @@ class TestExceptionTypeFilter: assert result == value + def test_without_arguments(self): + with pytest.raises(ValueError): + ExceptionTypeFilter() + class TestDispatchException: async def test_handle_exception(self, bot):