Remove filters from class based handlers

This commit is contained in:
jrootjunior 2019-12-11 16:45:10 +02:00
parent acbe1f81b6
commit 647d03b2d4
5 changed files with 1 additions and 16 deletions

View file

@ -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:

View file

@ -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

View file

@ -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]`

View file

@ -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")

View file

@ -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