mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 10:11:52 +00:00
Remove filters from class based handlers
This commit is contained in:
parent
acbe1f81b6
commit
647d03b2d4
5 changed files with 1 additions and 16 deletions
|
|
@ -54,10 +54,6 @@ class HandlerObject(CallableMixin):
|
||||||
|
|
||||||
if inspect.isclass(self.callback) and issubclass(self.callback, BaseHandler):
|
if inspect.isclass(self.callback) and issubclass(self.callback, BaseHandler):
|
||||||
self.awaitable = True
|
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]]:
|
async def check(self, *args: Any, **kwargs: Any) -> Tuple[bool, Dict[str, Any]]:
|
||||||
for event_filter in self.filters:
|
for event_filter in self.filters:
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ class _HandlerBotMixin(BaseHandlerMixin):
|
||||||
|
|
||||||
class BaseHandler(_HandlerBotMixin, ABC):
|
class BaseHandler(_HandlerBotMixin, ABC):
|
||||||
event: TelegramObject
|
event: TelegramObject
|
||||||
filters: Union[List["FilterType"], Tuple["FilterType"]]
|
|
||||||
|
|
||||||
def __init__(self, event: TelegramObject, **kwargs: Any) -> None:
|
def __init__(self, event: TelegramObject, **kwargs: Any) -> None:
|
||||||
self.event = event
|
self.event = event
|
||||||
|
|
|
||||||
|
|
@ -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: ...`
|
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.
|
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]`
|
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]`
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,6 @@ from aiogram.handlers import MessageHandler
|
||||||
|
|
||||||
@router.message_handler()
|
@router.message_handler()
|
||||||
class MyTestMessageHandler(MessageHandler):
|
class MyTestMessageHandler(MessageHandler):
|
||||||
filters = [Text(text="test")]
|
|
||||||
|
|
||||||
async def handle() -> Any:
|
async def handle() -> Any:
|
||||||
return SendMessage(chat_id=self.chat.id, text="PASS")
|
return SendMessage(chat_id=self.chat.id, text="PASS")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -180,13 +180,8 @@ class TestHandlerObject:
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_class_based_handler(self):
|
async def test_class_based_handler(self):
|
||||||
class MyFilter(BaseFilter):
|
|
||||||
async def __call__(self, event):
|
|
||||||
return True
|
|
||||||
|
|
||||||
class MyHandler(BaseHandler):
|
class MyHandler(BaseHandler):
|
||||||
event: Update
|
event: Update
|
||||||
filters = [MyFilter()]
|
|
||||||
|
|
||||||
async def handle(self) -> Any:
|
async def handle(self) -> Any:
|
||||||
return self.event.update_id
|
return self.event.update_id
|
||||||
|
|
@ -195,7 +190,6 @@ class TestHandlerObject:
|
||||||
|
|
||||||
assert handler.awaitable
|
assert handler.awaitable
|
||||||
assert handler.callback == MyHandler
|
assert handler.callback == MyHandler
|
||||||
assert len(handler.filters) == 2
|
assert len(handler.filters) == 1
|
||||||
assert handler.filters[1].callback == MyFilter()
|
|
||||||
result = await handler.call(Update(update_id=42))
|
result = await handler.call(Update(update_id=42))
|
||||||
assert result == 42
|
assert result == 42
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue