#896 Restrict including routers with strings (#897)

* #896 Restrict including routers with strings
* Remove imports util, bump dependencies
This commit is contained in:
Alex Root Junior 2022-04-25 21:24:58 +03:00 committed by GitHub
parent 4fb77a3a2a
commit f2e02e2a7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 11 additions and 70 deletions

View file

@ -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__}"

View file

@ -1,23 +0,0 @@
import importlib
from typing import Any
def import_module(target: str) -> Any:
if not isinstance(target, str):
raise ValueError(f"Target should be string not {type(target).__class__.__name__}")
module_name, _, attr_name = target.partition(":")
if not module_name or not attr_name:
raise ValueError(f'Import string "{target}" must be in format "<module>:<attribute>"')
try:
module = importlib.import_module(module_name)
except ImportError:
raise ValueError(f'Could not import module "{module_name}".')
try:
attribute = getattr(module, attr_name)
except AttributeError:
raise ValueError(f'Module "{module_name}" has no attribute "{attr_name}"')
return attribute