mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-11 01:54:53 +00:00
Move router validation to Router.parent_router
This commit is contained in:
parent
390648aae6
commit
9c2a3d1324
2 changed files with 10 additions and 4 deletions
|
|
@ -87,6 +87,10 @@ class Router:
|
|||
|
||||
:param router:
|
||||
"""
|
||||
if not isinstance(router, Router):
|
||||
raise ValueError(
|
||||
f"router should be instance of Router not {type(router).__class__.__name__}"
|
||||
)
|
||||
if self._parent_router:
|
||||
raise RuntimeError(f"Router is already attached to {self._parent_router!r}")
|
||||
if self == router:
|
||||
|
|
@ -108,6 +112,7 @@ class Router:
|
|||
parent = parent.parent_router
|
||||
|
||||
self._parent_router = router
|
||||
router.sub_routers.append(self)
|
||||
|
||||
def include_router(self, router: Union[Router, str]) -> Router:
|
||||
"""
|
||||
|
|
@ -120,14 +125,11 @@ class Router:
|
|||
"""
|
||||
if isinstance(router, str): # Resolve import string
|
||||
router = import_module(router)
|
||||
|
||||
# TODO: move this to setter of `parent_router` property
|
||||
if not isinstance(router, Router):
|
||||
raise ValueError(
|
||||
f"router should be instance of Router not {type(router).__class__.__name__}"
|
||||
)
|
||||
router.parent_router = self
|
||||
self.sub_routers.append(router)
|
||||
return router
|
||||
|
||||
async def _listen_update(self, update: Update, **kwargs: Any) -> Any:
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ import datetime
|
|||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.api.types import (
|
||||
CallbackQuery,
|
||||
Chat,
|
||||
|
|
@ -71,6 +70,11 @@ class TestRouter:
|
|||
with pytest.raises(ValueError, match=r"router should be instance of Router"):
|
||||
router.include_router("tests.test_dispatcher.test_router:TestRouter")
|
||||
|
||||
def test_set_parent_router_bad_type(self):
|
||||
router = Router()
|
||||
with pytest.raises(ValueError, match=r"router should be instance of Router"):
|
||||
router.parent_router = object()
|
||||
|
||||
def test_observers_config(self):
|
||||
router = Router()
|
||||
assert router.update_handler.handlers
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue