mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-09 01:15:31 +00:00
Merge remote-tracking branch 'origin/dev-2.x' into dev-2.x
This commit is contained in:
commit
f03e2169c3
1 changed files with 22 additions and 19 deletions
|
|
@ -9,6 +9,8 @@ from babel.support import LazyProxy
|
||||||
from .fields import BaseField
|
from .fields import BaseField
|
||||||
from ..utils import json
|
from ..utils import json
|
||||||
from ..utils.mixins import ContextInstanceMixin
|
from ..utils.mixins import ContextInstanceMixin
|
||||||
|
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')
|
||||||
|
|
||||||
|
|
@ -22,6 +24,7 @@ String = TypeVar('String', bound=str)
|
||||||
Integer = TypeVar('Integer', bound=int)
|
Integer = TypeVar('Integer', bound=int)
|
||||||
Float = TypeVar('Float', bound=float)
|
Float = TypeVar('Float', bound=float)
|
||||||
Boolean = TypeVar('Boolean', bound=bool)
|
Boolean = TypeVar('Boolean', bound=bool)
|
||||||
|
T = TypeVar('T')
|
||||||
|
|
||||||
|
|
||||||
class MetaTelegramObject(type):
|
class MetaTelegramObject(type):
|
||||||
|
|
@ -30,7 +33,7 @@ class MetaTelegramObject(type):
|
||||||
"""
|
"""
|
||||||
_objects = {}
|
_objects = {}
|
||||||
|
|
||||||
def __new__(mcs, name, bases, namespace, **kwargs):
|
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)
|
cls = super(MetaTelegramObject, mcs).__new__(mcs, name, bases, namespace)
|
||||||
|
|
||||||
props = {}
|
props = {}
|
||||||
|
|
@ -71,7 +74,7 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
||||||
Abstract class for telegram objects
|
Abstract class for telegram objects
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, conf=None, **kwargs):
|
def __init__(self, conf: typing.Dict[str, typing.Any]=None, **kwargs: typing.Any) -> None:
|
||||||
"""
|
"""
|
||||||
Deserialize object
|
Deserialize object
|
||||||
|
|
||||||
|
|
@ -117,7 +120,7 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
||||||
return getattr(self, ALIASES_ATTR_NAME, {})
|
return getattr(self, ALIASES_ATTR_NAME, {})
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def values(self):
|
def values(self) -> typing.Tuple[str]:
|
||||||
"""
|
"""
|
||||||
Get values
|
Get values
|
||||||
|
|
||||||
|
|
@ -128,11 +131,11 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
||||||
return getattr(self, VALUES_ATTR_NAME)
|
return getattr(self, VALUES_ATTR_NAME)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def telegram_types(self):
|
def telegram_types(self) -> typing.List[TelegramObject]:
|
||||||
return type(self).telegram_types
|
return type(self).telegram_types
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def to_object(cls, data):
|
def to_object(cls: typing.Type[T], data: typing.Dict[str, typing.Any]) -> T:
|
||||||
"""
|
"""
|
||||||
Deserialize object
|
Deserialize object
|
||||||
|
|
||||||
|
|
@ -142,7 +145,7 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
||||||
return cls(**data)
|
return cls(**data)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def bot(self):
|
def bot(self) -> Bot:
|
||||||
from ..bot.bot import Bot
|
from ..bot.bot import Bot
|
||||||
|
|
||||||
bot = Bot.get_current()
|
bot = Bot.get_current()
|
||||||
|
|
@ -152,7 +155,7 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
||||||
"'Bot.set_current(bot_instance)'")
|
"'Bot.set_current(bot_instance)'")
|
||||||
return bot
|
return bot
|
||||||
|
|
||||||
def to_python(self) -> typing.Dict:
|
def to_python(self) -> typing.Dict[str, typing.Any]:
|
||||||
"""
|
"""
|
||||||
Get object as JSON serializable
|
Get object as JSON serializable
|
||||||
|
|
||||||
|
|
@ -170,7 +173,7 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
||||||
result[self.props_aliases.get(name, name)] = value
|
result[self.props_aliases.get(name, name)] = value
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def clean(self):
|
def clean(self) -> None:
|
||||||
"""
|
"""
|
||||||
Remove empty values
|
Remove empty values
|
||||||
"""
|
"""
|
||||||
|
|
@ -188,7 +191,7 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
||||||
return json.dumps(self.to_python())
|
return json.dumps(self.to_python())
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(cls, *args, **kwargs):
|
def create(cls: Type[T], *args: typing.Any, **kwargs: typing.Any) -> T:
|
||||||
raise NotImplemented
|
raise NotImplemented
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
|
|
@ -199,7 +202,7 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
||||||
"""
|
"""
|
||||||
return self.as_json()
|
return self.as_json()
|
||||||
|
|
||||||
def __getitem__(self, item):
|
def __getitem__(self, item: typing.Union[str, int]) -> typing.Any:
|
||||||
"""
|
"""
|
||||||
Item getter (by key)
|
Item getter (by key)
|
||||||
|
|
||||||
|
|
@ -210,7 +213,7 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
||||||
return self.props[item].get_value(self)
|
return self.props[item].get_value(self)
|
||||||
raise KeyError(item)
|
raise KeyError(item)
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key: str, value: typing.Any) -> None:
|
||||||
"""
|
"""
|
||||||
Item setter (by key)
|
Item setter (by key)
|
||||||
|
|
||||||
|
|
@ -222,7 +225,7 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
||||||
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)
|
raise KeyError(key)
|
||||||
|
|
||||||
def __contains__(self, item):
|
def __contains__(self, item: typing.Dict[str, typing.Any]) -> bool:
|
||||||
"""
|
"""
|
||||||
Check key contains in that object
|
Check key contains in that object
|
||||||
|
|
||||||
|
|
@ -232,7 +235,7 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
||||||
self.clean()
|
self.clean()
|
||||||
return item in self.values
|
return item in self.values
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self) -> typing.Iterator[str]:
|
||||||
"""
|
"""
|
||||||
Iterate over items
|
Iterate over items
|
||||||
|
|
||||||
|
|
@ -241,7 +244,7 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
||||||
for item in self.to_python().items():
|
for item in self.to_python().items():
|
||||||
yield item
|
yield item
|
||||||
|
|
||||||
def iter_keys(self):
|
def iter_keys(self) -> typing.Generator[typing.Any, None, None]:
|
||||||
"""
|
"""
|
||||||
Iterate over keys
|
Iterate over keys
|
||||||
|
|
||||||
|
|
@ -250,7 +253,7 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
||||||
for key, _ in self:
|
for key, _ in self:
|
||||||
yield key
|
yield key
|
||||||
|
|
||||||
def iter_values(self):
|
def iter_values(self) -> typing.Generator[typing.Any, None, None]:
|
||||||
"""
|
"""
|
||||||
Iterate over values
|
Iterate over values
|
||||||
|
|
||||||
|
|
@ -259,9 +262,9 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
||||||
for _, value in self:
|
for _, value in self:
|
||||||
yield value
|
yield value
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self) -> int:
|
||||||
def _hash(obj):
|
def _hash(obj)-> int:
|
||||||
buf = 0
|
buf: int = 0
|
||||||
if isinstance(obj, list):
|
if isinstance(obj, list):
|
||||||
for item in obj:
|
for item in obj:
|
||||||
buf += _hash(item)
|
buf += _hash(item)
|
||||||
|
|
@ -281,5 +284,5 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other: TelegramObject) -> bool:
|
||||||
return isinstance(other, self.__class__) and hash(other) == hash(self)
|
return isinstance(other, self.__class__) and hash(other) == hash(self)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue