diff --git a/aiogram/types/base.py b/aiogram/types/base.py index 20290a98..f59b9d74 100644 --- a/aiogram/types/base.py +++ b/aiogram/types/base.py @@ -13,29 +13,45 @@ from .fields import BaseField if typing.TYPE_CHECKING: from ..bot.bot import Bot -__all__ = ('MetaTelegramObject', 'TelegramObject', 'InputFile', 'String', 'Integer', 'Float', 'Boolean') +__all__ = ( + "MetaTelegramObject", + "TelegramObject", + "InputFile", + "String", + "Integer", + "Float", + "Boolean", +) -PROPS_ATTR_NAME = '_props' -VALUES_ATTR_NAME = '_values' -ALIASES_ATTR_NAME = '_aliases' +PROPS_ATTR_NAME = "_props" +VALUES_ATTR_NAME = "_values" +ALIASES_ATTR_NAME = "_aliases" # Binding of builtin types -InputFile = TypeVar('InputFile', 'InputFile', io.BytesIO, io.FileIO, str) -String = TypeVar('String', bound=str) -Integer = TypeVar('Integer', bound=int) -Float = TypeVar('Float', bound=float) -Boolean = TypeVar('Boolean', bound=bool) -T = TypeVar('T') +InputFile = TypeVar("InputFile", "InputFile", io.BytesIO, io.FileIO, str) +String = TypeVar("String", bound=str) +Integer = TypeVar("Integer", bound=int) +Float = TypeVar("Float", bound=float) +Boolean = TypeVar("Boolean", bound=bool) +T = TypeVar("T") class MetaTelegramObject(type): """ Metaclass for telegram objects """ + _objects = {} - def __new__(mcs: typing.Type[T], name: str, bases: typing.Tuple[typing.Type], namespace: typing.Dict[str, typing.Any], **kwargs: typing.Any) -> T: - cls = super(MetaTelegramObject, mcs).__new__(mcs, name, bases, namespace) + def __new__( + mcs: typing.Type[T], + name: str, + bases: typing.Tuple[typing.Type], + namespace: typing.Dict[str, typing.Any], + **kwargs: typing.Any + ) -> T: + cls = super(MetaTelegramObject, mcs).__new__( + mcs, name, bases, namespace) props = {} values = {} @@ -50,7 +66,11 @@ class MetaTelegramObject(type): aliases.update(getattr(base, ALIASES_ATTR_NAME)) # Scan current object for props - for name, prop in ((name, prop) for name, prop in namespace.items() if isinstance(prop, BaseField)): + for name, prop in ( + (name, prop) + for name, prop in namespace.items() + if isinstance(prop, BaseField) + ): props[prop.alias] = prop if prop.default is not None: values[prop.alias] = prop.default @@ -75,7 +95,9 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject): Abstract class for telegram objects """ - def __init__(self, conf: typing.Dict[str, typing.Any]=None, **kwargs: typing.Any) -> None: + def __init__( + self, conf: typing.Dict[str, typing.Any] = None, **kwargs: typing.Any + ) -> None: """ Deserialize object @@ -151,9 +173,11 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject): bot = Bot.get_current() if bot is None: - raise RuntimeError("Can't get bot instance from context. " - "You can fix it with setting current instance: " - "'Bot.set_current(bot_instance)'") + raise RuntimeError( + "Can't get bot instance from context. " + "You can fix it with setting current instance: " + "'Bot.set_current(bot_instance)'" + ) return bot def to_python(self) -> typing.Dict[str, typing.Any]: @@ -224,7 +248,7 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject): :return: """ if key in self.props: - return self.props[key].set_value(self, value, self.conf.get('parent', None)) + return self.props[key].set_value(self, value, self.conf.get("parent", None)) raise KeyError(key) def __contains__(self, item: str) -> bool: