From be2a681889b2ba5eab35410267d8fd151dfbe4c5 Mon Sep 17 00:00:00 2001 From: darksidecat <58224121+darksidecat@users.noreply.github.com> Date: Sat, 21 Aug 2021 20:46:38 +0300 Subject: [PATCH] requested changes --- aiogram/dispatcher/dispatcher.py | 3 +- aiogram/types/update.py | 50 ++++++++++++++++++++------------ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index 9b31d42d..fffe0262 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -188,7 +188,8 @@ class Dispatcher(Router): :return: """ try: - update_type, event = update.content() + update_type = update.event_type + event = update.event except UpdateTypeLookupError: warnings.warn( "Detected unknown update type.\n" diff --git a/aiogram/types/update.py b/aiogram/types/update.py index 013ac5d7..b284f0f7 100644 --- a/aiogram/types/update.py +++ b/aiogram/types/update.py @@ -1,6 +1,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Optional, Tuple +import functools +from typing import TYPE_CHECKING, Callable, Optional, TypeVar, cast from .base import TelegramObject @@ -15,6 +16,12 @@ if TYPE_CHECKING: # pragma: no cover from .pre_checkout_query import PreCheckoutQuery from .shipping_query import ShippingQuery +T = TypeVar("T") + + +def lru_cache(func: Callable[..., T]) -> T: + return functools.lru_cache()(func) # type: ignore + class Update(TelegramObject): """ @@ -54,44 +61,51 @@ class Update(TelegramObject): chat_member: Optional[ChatMemberUpdated] = None """*Optional*. A chat member's status was updated in a chat. The bot must be an administrator in the chat and must explicitly specify 'chat_member' in the list of *allowed_updates* to receive these updates.""" - def content(self) -> Tuple[str, TelegramObject]: - """ - Detect content type + def __hash__(self) -> int: + return hash((type(self), self.update_id)) - Return update type and content + @property # type: ignore + @lru_cache + def event_type(self) -> str: + """ + Detect update type If update type is unknown, raise UpdateTypeLookupError :return: """ if self.message: - return "message", self.message + return "message" if self.edited_message: - return "edited_message", self.edited_message + return "edited_message" if self.channel_post: - return "channel_post", self.channel_post + return "channel_post" if self.edited_channel_post: - return "edited_channel_post", self.edited_channel_post + return "edited_channel_post" if self.inline_query: - return "inline_query", self.inline_query + return "inline_query" if self.chosen_inline_result: - return "chosen_inline_result", self.chosen_inline_result + return "chosen_inline_result" if self.callback_query: - return "callback_query", self.callback_query + return "callback_query" if self.shipping_query: - return "shipping_query", self.shipping_query + return "shipping_query" if self.pre_checkout_query: - return "pre_checkout_query", self.pre_checkout_query + return "pre_checkout_query" if self.poll: - return "poll", self.poll + return "poll" if self.poll_answer: - return "poll_answer", self.poll_answer + return "poll_answer" if self.my_chat_member: - return "my_chat_member", self.my_chat_member + return "my_chat_member" if self.chat_member: - return "chat_member", self.chat_member + return "chat_member" raise UpdateTypeLookupError("Unknown update type") + @property + def event(self) -> TelegramObject: + return cast(TelegramObject, getattr(self, self.event_type)) + class UpdateTypeLookupError(LookupError): pass