diff --git a/aiogram/dispatcher/router.py b/aiogram/dispatcher/router.py index b39a1f58..df48df4e 100644 --- a/aiogram/dispatcher/router.py +++ b/aiogram/dispatcher/router.py @@ -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: diff --git a/tests/test_dispatcher/test_router.py b/tests/test_dispatcher/test_router.py index cbadf5ed..5cf04dcc 100644 --- a/tests/test_dispatcher/test_router.py +++ b/tests/test_dispatcher/test_router.py @@ -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