mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 10:11:52 +00:00
Allow to disable builtin filters for router
This commit is contained in:
parent
ce7b5f70e0
commit
97acf956e9
4 changed files with 37 additions and 6 deletions
|
|
@ -11,8 +11,8 @@ from .router import Router
|
|||
|
||||
|
||||
class Dispatcher(Router):
|
||||
def __init__(self):
|
||||
super(Dispatcher, self).__init__()
|
||||
def __init__(self, **kwargs):
|
||||
super(Dispatcher, self).__init__(**kwargs)
|
||||
self._running_lock = Lock()
|
||||
|
||||
@property
|
||||
|
|
|
|||
|
|
@ -1,15 +1,19 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import warnings
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
|
||||
from ..api.types import Chat, Update, User
|
||||
from ..utils.imports import import_module
|
||||
from ..utils.warnings import CodeHasNoEffect
|
||||
from .event.observer import EventObserver, SkipHandler, TelegramEventObserver
|
||||
from .filters import BUILTIN_FILTERS
|
||||
|
||||
|
||||
class Router:
|
||||
def __init__(self):
|
||||
def __init__(self, use_builtin_filters: bool = True):
|
||||
self.use_builtin_filters = use_builtin_filters
|
||||
|
||||
self._parent_router: Optional[Router] = None
|
||||
self.sub_routers: List[Router] = []
|
||||
|
||||
|
|
@ -55,9 +59,11 @@ class Router:
|
|||
}
|
||||
|
||||
self.update_handler.register(self._listen_update)
|
||||
for name, observer in self.observers.items():
|
||||
for builtin_filter in BUILTIN_FILTERS.get(name, ()):
|
||||
observer.bind_filter(builtin_filter)
|
||||
|
||||
if use_builtin_filters:
|
||||
for name, observer in self.observers.items():
|
||||
for builtin_filter in BUILTIN_FILTERS.get(name, ()):
|
||||
observer.bind_filter(builtin_filter)
|
||||
|
||||
@property
|
||||
def parent_router(self) -> Optional[Router]:
|
||||
|
|
@ -73,6 +79,15 @@ class Router:
|
|||
while parent is not None:
|
||||
if parent == self:
|
||||
raise RuntimeError("Circular referencing of Router is not allowed")
|
||||
|
||||
if not self.use_builtin_filters and parent.use_builtin_filters:
|
||||
warnings.warn(
|
||||
f"Router(use_builtin_filters=False) has no effect for router {self} "
|
||||
f"in due to builtin filters is already registered in parent router",
|
||||
CodeHasNoEffect,
|
||||
stacklevel=3,
|
||||
)
|
||||
|
||||
parent = parent.parent_router
|
||||
|
||||
self._parent_router = router
|
||||
|
|
|
|||
6
aiogram/utils/warnings.py
Normal file
6
aiogram/utils/warnings.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
class AiogramWarning(Warning):
|
||||
pass
|
||||
|
||||
|
||||
class CodeHasNoEffect(AiogramWarning):
|
||||
pass
|
||||
|
|
@ -19,6 +19,7 @@ from aiogram.api.types import (
|
|||
)
|
||||
from aiogram.dispatcher.event.observer import SkipHandler
|
||||
from aiogram.dispatcher.router import Router
|
||||
from aiogram.utils.warnings import CodeHasNoEffect
|
||||
|
||||
importable_router = Router()
|
||||
|
||||
|
|
@ -52,6 +53,15 @@ class TestRouter:
|
|||
assert router3.parent_router is router2
|
||||
assert router3.sub_routers == []
|
||||
|
||||
def test_include_router_code_has_no_effect(self):
|
||||
router1 = Router()
|
||||
router2 = Router(use_builtin_filters=False)
|
||||
|
||||
assert router1.use_builtin_filters
|
||||
assert not router2.use_builtin_filters
|
||||
with pytest.warns(CodeHasNoEffect):
|
||||
assert router1.include_router(router2)
|
||||
|
||||
def test_include_router_by_string(self):
|
||||
router = Router()
|
||||
router.include_router("tests.test_dispatcher.test_router:importable_router")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue