diff --git a/CHANGES/896.misc.rst b/CHANGES/896.misc.rst new file mode 100644 index 00000000..9dbd6db4 --- /dev/null +++ b/CHANGES/896.misc.rst @@ -0,0 +1 @@ +Restrict including routers with strings diff --git a/aiogram/dispatcher/router.py b/aiogram/dispatcher/router.py index a1a20fba..9f1c0f77 100644 --- a/aiogram/dispatcher/router.py +++ b/aiogram/dispatcher/router.py @@ -4,7 +4,6 @@ import warnings from typing import Any, Dict, Generator, List, Optional, Set, Union from ..types import TelegramObject -from ..utils.imports import import_module from ..utils.warnings import CodeHasNoEffect from .event.bases import REJECTED, UNHANDLED from .event.event import EventObserver @@ -211,8 +210,6 @@ class Router: :param router: :return: """ - if isinstance(router, str): # Resolve import string - router = import_module(router) if not isinstance(router, Router): raise ValueError( f"router should be instance of Router not {type(router).__class__.__name__}" diff --git a/tests/test_dispatcher/test_router.py b/tests/test_dispatcher/test_router.py index 56821f46..7f98e971 100644 --- a/tests/test_dispatcher/test_router.py +++ b/tests/test_dispatcher/test_router.py @@ -5,7 +5,6 @@ from aiogram.dispatcher.router import Router from aiogram.utils.warnings import CodeHasNoEffect pytestmark = pytest.mark.asyncio -importable_router = Router() class TestRouter: @@ -46,14 +45,10 @@ class TestRouter: 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") - def test_include_router_by_string_bad_type(self): router = Router() with pytest.raises(ValueError, match=r"router should be instance of Router"): - router.include_router("tests.test_dispatcher.test_router:TestRouter") + router.include_router(self) def test_set_parent_router_bad_type(self): router = Router() diff --git a/tests/test_utils/test_imports.py b/tests/test_utils/test_imports.py deleted file mode 100644 index e877201c..00000000 --- a/tests/test_utils/test_imports.py +++ /dev/null @@ -1,29 +0,0 @@ -import pytest - -import aiogram -from aiogram.utils.imports import import_module - - -class TestImports: - def test_bad_type(self): - with pytest.raises(ValueError, match=r"Target should be string not"): - import_module(42) - - @pytest.mark.parametrize("value", ["module", "module:", ":attribute"]) - def test_bad_format(self, value): - with pytest.raises(ValueError, match='must be in format ":"'): - import_module(value) - - @pytest.mark.parametrize("value", ["module", "aiogram.KABOOM", "aiogram.KABOOM.TEST"]) - def test_bad_value(self, value): - with pytest.raises(ValueError, match="Could not import module"): - import_module(f"{value}:attribute") - - def test_has_no_attribute(self): - with pytest.raises(ValueError, match="has no attribute"): - import_module("aiogram:KABOOM") - - def test_imported(self): - value = import_module("aiogram:__version__") - isinstance(value, str) - assert value == aiogram.__version__