From 647d03b2d4b074937b47076ca190060b175b4a48 Mon Sep 17 00:00:00 2001 From: jrootjunior Date: Wed, 11 Dec 2019 16:45:10 +0200 Subject: [PATCH] Remove filters from class based handlers --- aiogram/dispatcher/event/handler.py | 4 ---- aiogram/dispatcher/handler/base.py | 1 - docs/dispatcher/class_based_handlers/basics.md | 2 -- docs/dispatcher/class_based_handlers/message.md | 2 -- tests/test_dispatcher/test_event/test_handler.py | 8 +------- 5 files changed, 1 insertion(+), 16 deletions(-) diff --git a/aiogram/dispatcher/event/handler.py b/aiogram/dispatcher/event/handler.py index cf2248ce..42c6202d 100644 --- a/aiogram/dispatcher/event/handler.py +++ b/aiogram/dispatcher/event/handler.py @@ -54,10 +54,6 @@ class HandlerObject(CallableMixin): if inspect.isclass(self.callback) and issubclass(self.callback, BaseHandler): self.awaitable = True - if hasattr(self.callback, "filters"): - self.filters.extend( - FilterObject(event_filter) for event_filter in self.callback.filters - ) async def check(self, *args: Any, **kwargs: Any) -> Tuple[bool, Dict[str, Any]]: for event_filter in self.filters: diff --git a/aiogram/dispatcher/handler/base.py b/aiogram/dispatcher/handler/base.py index a576b0b6..47abcbb5 100644 --- a/aiogram/dispatcher/handler/base.py +++ b/aiogram/dispatcher/handler/base.py @@ -23,7 +23,6 @@ class _HandlerBotMixin(BaseHandlerMixin): class BaseHandler(_HandlerBotMixin, ABC): event: TelegramObject - filters: Union[List["FilterType"], Tuple["FilterType"]] def __init__(self, event: TelegramObject, **kwargs: Any) -> None: self.event = event diff --git a/docs/dispatcher/class_based_handlers/basics.md b/docs/dispatcher/class_based_handlers/basics.md index 529def4d..a6e4c1f8 100644 --- a/docs/dispatcher/class_based_handlers/basics.md +++ b/docs/dispatcher/class_based_handlers/basics.md @@ -17,8 +17,6 @@ Import: `#!python3 from aiogram.hanler import BaseHandler` By default you will need to override only method `#!python3 async def handle(self) -> Any: ...` -The list of filters can be specified in attribute `#!python3 filters: Union[List["FilterType"], Tuple["FilterType"]]` or you can combine that attribute with filters specified in event registerer (via decorator or observer method) - This class is also have an default initializer and you don't need to change it. Initializer accepts current event and all contextual data and which can be accessed from the handler through attributes: `event: TelegramEvent` and `data: Dict[Any, str]` diff --git a/docs/dispatcher/class_based_handlers/message.md b/docs/dispatcher/class_based_handlers/message.md index 90866506..978cafab 100644 --- a/docs/dispatcher/class_based_handlers/message.md +++ b/docs/dispatcher/class_based_handlers/message.md @@ -10,8 +10,6 @@ from aiogram.handlers import MessageHandler @router.message_handler() class MyTestMessageHandler(MessageHandler): - filters = [Text(text="test")] - async def handle() -> Any: return SendMessage(chat_id=self.chat.id, text="PASS") diff --git a/tests/test_dispatcher/test_event/test_handler.py b/tests/test_dispatcher/test_event/test_handler.py index bde90ed9..3db676db 100644 --- a/tests/test_dispatcher/test_event/test_handler.py +++ b/tests/test_dispatcher/test_event/test_handler.py @@ -180,13 +180,8 @@ class TestHandlerObject: @pytest.mark.asyncio async def test_class_based_handler(self): - class MyFilter(BaseFilter): - async def __call__(self, event): - return True - class MyHandler(BaseHandler): event: Update - filters = [MyFilter()] async def handle(self) -> Any: return self.event.update_id @@ -195,7 +190,6 @@ class TestHandlerObject: assert handler.awaitable assert handler.callback == MyHandler - assert len(handler.filters) == 2 - assert handler.filters[1].callback == MyFilter() + assert len(handler.filters) == 1 result = await handler.call(Update(update_id=42)) assert result == 42