diff --git a/telethon/events/common.py b/telethon/events/common.py index e8978f31..3986a426 100644 --- a/telethon/events/common.py +++ b/telethon/events/common.py @@ -1,8 +1,9 @@ import abc import asyncio import warnings +from typing import Optional, Sequence, Callable -from .. import utils +from .. import utils, TelegramClient, hints from ..tl import TLObject, types from ..tl.custom.chatgetter import ChatGetter @@ -65,7 +66,8 @@ class EventBuilder(abc.ABC): async def handler(event): pass # code here """ - def __init__(self, chats=None, *, blacklist_chats=False, func=None): + def __init__(self, chats: Optional[Sequence[hints.Entity]] = None, *, + blacklist_chats: bool = False, func: Optional[Callable[['EventCommon'], None]] = None): self.chats = chats self.blacklist_chats = bool(blacklist_chats) self.resolved = False @@ -102,7 +104,7 @@ class EventBuilder(abc.ABC): async def _resolve(self, client): self.chats = await _into_id_set(client, self.chats) - def filter(self, event): + def filter(self, event: 'EventCommon'): """ Returns a truthy value if the event passed the filter and should be used, or falsy otherwise. The return value may need to be awaited. @@ -145,9 +147,9 @@ class EventCommon(ChatGetter, abc.ABC): self._entities = {} self._client = None self._message_id = msg_id - self.original_update = None + self.original_update = None # type: Optional[types.TypeUpdate] - def _set_client(self, client): + def _set_client(self, client: TelegramClient): """ Setter so subclasses can act accordingly when the client is set. """ @@ -159,7 +161,7 @@ class EventCommon(ChatGetter, abc.ABC): self._chat = self._input_chat = None @property - def client(self): + def client(self) -> TelegramClient: """ The `telethon.TelegramClient` that created this event. """ diff --git a/telethon/events/inlinequery.py b/telethon/events/inlinequery.py index ad3dbcf6..688f42ef 100644 --- a/telethon/events/inlinequery.py +++ b/telethon/events/inlinequery.py @@ -2,9 +2,10 @@ import inspect import re import asyncio +from typing import Optional, Sequence, Callable, Union, Pattern, Match from .common import EventBuilder, EventCommon, name_inner_event -from .. import utils +from .. import utils, hints from ..tl import types, functions, custom from ..tl.custom.sendergetter import SenderGetter @@ -48,7 +49,9 @@ class InlineQuery(EventBuilder): ]) """ def __init__( - self, users=None, *, blacklist_users=False, func=None, pattern=None): + self, users: Optional[Sequence[hints.EntityLike]] = None, *, + blacklist_users: bool = False, func: Optional[Callable[['InlineQuery.Event'], None]] = None, + pattern: Union[str, Callable, Pattern, Optional] = None): super().__init__(users, blacklist_chats=blacklist_users, func=func) if isinstance(pattern, str): @@ -65,7 +68,7 @@ class InlineQuery(EventBuilder): if isinstance(update, types.UpdateBotInlineQuery): return cls.Event(update) - def filter(self, event): + def filter(self, event: 'InlineQuery.Event'): if self.pattern: match = self.pattern(event.text) if not match: @@ -89,11 +92,11 @@ class InlineQuery(EventBuilder): The resulting object from calling the passed ``pattern`` function, which is ``re.compile(...).match`` by default. """ - def __init__(self, query): + def __init__(self, query: types.UpdateBotInlineQuery): super().__init__(chat_peer=types.PeerUser(query.user_id)) SenderGetter.__init__(self, query.user_id) self.query = query - self.pattern_match = None + self.pattern_match = None # type: Optional[Match] self._answered = False def _set_client(self, client): @@ -141,9 +144,9 @@ class InlineQuery(EventBuilder): return custom.InlineBuilder(self._client) async def answer( - self, results=None, cache_time=0, *, - gallery=False, next_offset=None, private=False, - switch_pm=None, switch_pm_param=''): + self, results: Optional[Sequence[types.InputBotInlineResult]] = None, cache_time: Optional[int] = 0, *, + gallery: bool = False, next_offset: Optional[str] = None, private: bool = False, + switch_pm: Optional[str] = None, switch_pm_param: str = ''): """ Answers the inline query with the given results. diff --git a/telethon/events/newmessage.py b/telethon/events/newmessage.py index dc40ca11..7ccbfd62 100644 --- a/telethon/events/newmessage.py +++ b/telethon/events/newmessage.py @@ -1,7 +1,8 @@ import re +from typing import Optional, Callable, Union, Sequence, Match, Pattern from .common import EventBuilder, EventCommon, name_inner_event, _into_id_set -from .. import utils +from .. import utils, hints from ..tl import types @@ -56,9 +57,11 @@ class NewMessage(EventBuilder): await asyncio.sleep(5) await client.delete_messages(event.chat_id, [event.id, m.id]) """ - def __init__(self, chats=None, *, blacklist_chats=False, func=None, - incoming=None, outgoing=None, - from_users=None, forwards=None, pattern=None): + def __init__(self, chats: Optional[Sequence[hints.Entity]] = None, *, blacklist_chats: bool = False, + func: Optional[Callable[['NewMessage.Event'], None]] = None, + incoming: Optional[bool] = None, outgoing: Optional[bool] = None, + from_users: Optional[hints.Entity] = None, forwards: Optional[bool] = None, + pattern: Union[str, Callable, Pattern, Optional] = None): if incoming and outgoing: incoming = outgoing = None # Same as no filter elif incoming is not None and outgoing is None: @@ -143,7 +146,7 @@ class NewMessage(EventBuilder): return event - def filter(self, event): + def filter(self, event: 'NewMessage.Event'): if self._no_check: return event @@ -167,7 +170,7 @@ class NewMessage(EventBuilder): return super().filter(event) - class Event(EventCommon): + class Event(EventCommon, types.TypeMessage): """ Represents the event of a new message. This event can be treated to all effects as a `Message `, @@ -200,12 +203,12 @@ class NewMessage(EventBuilder): ... >>> """ - def __init__(self, message): + def __init__(self, message: types.TypeMessage): self.__dict__['_init'] = False super().__init__(chat_peer=message.peer_id, msg_id=message.id, broadcast=bool(message.post)) - self.pattern_match = None + self.pattern_match = None # type: Optional[Match] self.message = message def _set_client(self, client):