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