From b0386c925091c6858547a20f9d557138c1eeaee6 Mon Sep 17 00:00:00 2001 From: latan Date: Tue, 23 May 2023 13:09:33 +0300 Subject: [PATCH 1/9] Add `auto_resolve_update_types` argument to `start_polling` method. --- aiogram/dispatcher/dispatcher.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index 9c61a447..3f0ec2b4 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -451,6 +451,7 @@ class Dispatcher(Router): handle_as_tasks: bool = True, backoff_config: BackoffConfig = DEFAULT_BACKOFF_CONFIG, allowed_updates: Optional[List[str]] = None, + auto_resolve_update_types: Optional[bool] = False, handle_signals: bool = True, close_bot_session: bool = True, **kwargs: Any, @@ -463,6 +464,7 @@ class Dispatcher(Router): :param handle_as_tasks: Run task for each event and no wait result :param backoff_config: backoff-retry config :param allowed_updates: List of the update types you want your bot to receive + :param auto_resolve_update_types: automatically resolve used update types in handlers :param handle_signals: handle signals (SIGINT/SIGTERM) :param close_bot_session: close bot sessions on shutdown :param kwargs: contextual data @@ -497,6 +499,15 @@ class Dispatcher(Router): signal.SIGINT, self._signal_stop_polling, signal.SIGINT ) + if auto_resolve_update_types: + if allowed_updates: + loggers.dispatcher.warning( + "auto_resolve_update_types and allowed_updates " + "arguments are mutually exclusive, allowed_updates will be used instead" + ) + else: + allowed_updates = self.resolve_used_update_types() + workflow_data = { "dispatcher": self, "bots": bots, @@ -547,6 +558,7 @@ class Dispatcher(Router): handle_as_tasks: bool = True, backoff_config: BackoffConfig = DEFAULT_BACKOFF_CONFIG, allowed_updates: Optional[List[str]] = None, + auto_resolve_update_types: Optional[bool] = False, handle_signals: bool = True, close_bot_session: bool = True, **kwargs: Any, @@ -559,6 +571,7 @@ class Dispatcher(Router): :param handle_as_tasks: Run task for each event and no wait result :param backoff_config: backoff-retry config :param allowed_updates: List of the update types you want your bot to receive + :param auto_resolve_update_types: auto resolve update types from handlers :param handle_signals: handle signals (SIGINT/SIGTERM) :param close_bot_session: close bot sessions on shutdown :param kwargs: contextual data @@ -573,6 +586,7 @@ class Dispatcher(Router): handle_as_tasks=handle_as_tasks, backoff_config=backoff_config, allowed_updates=allowed_updates, + auto_resolve_update_types=auto_resolve_update_types, handle_signals=handle_signals, close_bot_session=close_bot_session, ) From e955d7cb3b85313114a49f3832e4336d3da2669b Mon Sep 17 00:00:00 2001 From: latan Date: Tue, 23 May 2023 13:24:29 +0300 Subject: [PATCH 2/9] add towncrier file for a feature --- CHANGES/1178.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 CHANGES/1178.feature diff --git a/CHANGES/1178.feature b/CHANGES/1178.feature new file mode 100644 index 00000000..2a600fd8 --- /dev/null +++ b/CHANGES/1178.feature @@ -0,0 +1 @@ +Add auto_resolve_update_types argument to start_polling method to collect the used update types automatically. From a5ffa37b696918afbfbbb7c18fcb08f0bccb564b Mon Sep 17 00:00:00 2001 From: latan Date: Thu, 1 Jun 2023 22:49:04 +0300 Subject: [PATCH 3/9] make resolving update types by default in dispatcher.py --- aiogram/dispatcher/dispatcher.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index 3f0ec2b4..32129b80 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -451,7 +451,6 @@ class Dispatcher(Router): handle_as_tasks: bool = True, backoff_config: BackoffConfig = DEFAULT_BACKOFF_CONFIG, allowed_updates: Optional[List[str]] = None, - auto_resolve_update_types: Optional[bool] = False, handle_signals: bool = True, close_bot_session: bool = True, **kwargs: Any, @@ -464,7 +463,6 @@ class Dispatcher(Router): :param handle_as_tasks: Run task for each event and no wait result :param backoff_config: backoff-retry config :param allowed_updates: List of the update types you want your bot to receive - :param auto_resolve_update_types: automatically resolve used update types in handlers :param handle_signals: handle signals (SIGINT/SIGTERM) :param close_bot_session: close bot sessions on shutdown :param kwargs: contextual data @@ -499,14 +497,8 @@ class Dispatcher(Router): signal.SIGINT, self._signal_stop_polling, signal.SIGINT ) - if auto_resolve_update_types: - if allowed_updates: - loggers.dispatcher.warning( - "auto_resolve_update_types and allowed_updates " - "arguments are mutually exclusive, allowed_updates will be used instead" - ) - else: - allowed_updates = self.resolve_used_update_types() + if allowed_updates is None: + allowed_updates = self.resolve_used_update_types() workflow_data = { "dispatcher": self, From ce444a90440a101b5724ddba8667074f76bc2fba Mon Sep 17 00:00:00 2001 From: latan Date: Thu, 1 Jun 2023 22:51:55 +0300 Subject: [PATCH 4/9] change the default value for allowed updates to UNSET TYPE --- aiogram/dispatcher/dispatcher.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index 32129b80..6457aac6 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -18,6 +18,7 @@ from ..fsm.strategy import FSMStrategy from ..methods import GetUpdates, TelegramMethod from ..methods.base import TelegramType from ..types import Update, User +from ..types.base import UNSET_TYPE from ..types.update import UpdateTypeLookupError from ..utils.backoff import Backoff, BackoffConfig from .event.bases import UNHANDLED, SkipHandler @@ -450,7 +451,7 @@ class Dispatcher(Router): polling_timeout: int = 10, handle_as_tasks: bool = True, backoff_config: BackoffConfig = DEFAULT_BACKOFF_CONFIG, - allowed_updates: Optional[List[str]] = None, + allowed_updates: Optional[List[str]] = UNSET_TYPE, handle_signals: bool = True, close_bot_session: bool = True, **kwargs: Any, @@ -497,7 +498,7 @@ class Dispatcher(Router): signal.SIGINT, self._signal_stop_polling, signal.SIGINT ) - if allowed_updates is None: + if allowed_updates is UNSET_TYPE: allowed_updates = self.resolve_used_update_types() workflow_data = { From 6a3df4d6f33fb439e04460ad59b1ba7398b06ffc Mon Sep 17 00:00:00 2001 From: latan Date: Thu, 1 Jun 2023 22:53:16 +0300 Subject: [PATCH 5/9] removed auto_resolve_update_types from run_polling --- aiogram/dispatcher/dispatcher.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index 6457aac6..b348a7da 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -551,7 +551,6 @@ class Dispatcher(Router): handle_as_tasks: bool = True, backoff_config: BackoffConfig = DEFAULT_BACKOFF_CONFIG, allowed_updates: Optional[List[str]] = None, - auto_resolve_update_types: Optional[bool] = False, handle_signals: bool = True, close_bot_session: bool = True, **kwargs: Any, @@ -564,7 +563,6 @@ class Dispatcher(Router): :param handle_as_tasks: Run task for each event and no wait result :param backoff_config: backoff-retry config :param allowed_updates: List of the update types you want your bot to receive - :param auto_resolve_update_types: auto resolve update types from handlers :param handle_signals: handle signals (SIGINT/SIGTERM) :param close_bot_session: close bot sessions on shutdown :param kwargs: contextual data @@ -579,7 +577,6 @@ class Dispatcher(Router): handle_as_tasks=handle_as_tasks, backoff_config=backoff_config, allowed_updates=allowed_updates, - auto_resolve_update_types=auto_resolve_update_types, handle_signals=handle_signals, close_bot_session=close_bot_session, ) From 1abbf9a218989faac36da2f407b74c4720076690 Mon Sep 17 00:00:00 2001 From: latand Date: Fri, 2 Jun 2023 10:15:20 +0300 Subject: [PATCH 6/9] fix typehint --- aiogram/dispatcher/dispatcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index b348a7da..41821519 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -451,7 +451,7 @@ class Dispatcher(Router): polling_timeout: int = 10, handle_as_tasks: bool = True, backoff_config: BackoffConfig = DEFAULT_BACKOFF_CONFIG, - allowed_updates: Optional[List[str]] = UNSET_TYPE, + allowed_updates: Optional[Union[List[str], Literal[UNSET_TYPE]]] = UNSET_TYPE, handle_signals: bool = True, close_bot_session: bool = True, **kwargs: Any, From caba07a529030ca7515ced619b7400c137708aca Mon Sep 17 00:00:00 2001 From: latand Date: Fri, 2 Jun 2023 10:17:39 +0300 Subject: [PATCH 7/9] update changelog --- CHANGES/1178.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES/1178.feature b/CHANGES/1178.feature index 2a600fd8..654e78ae 100644 --- a/CHANGES/1178.feature +++ b/CHANGES/1178.feature @@ -1 +1 @@ -Add auto_resolve_update_types argument to start_polling method to collect the used update types automatically. +Made allowed_updates list to revolve automatically in start_polling method if not set explicitly. From af4b9cc415ef728d1f6815f5ff37446c24132b03 Mon Sep 17 00:00:00 2001 From: latand Date: Fri, 2 Jun 2023 11:12:34 +0300 Subject: [PATCH 8/9] fix typing --- aiogram/dispatcher/dispatcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index 41821519..763a9e37 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -451,7 +451,7 @@ class Dispatcher(Router): polling_timeout: int = 10, handle_as_tasks: bool = True, backoff_config: BackoffConfig = DEFAULT_BACKOFF_CONFIG, - allowed_updates: Optional[Union[List[str], Literal[UNSET_TYPE]]] = UNSET_TYPE, + allowed_updates: Optional[Union[List[str], UNSET_TYPE]] = UNSET_TYPE, handle_signals: bool = True, close_bot_session: bool = True, **kwargs: Any, From 5236329521fedc246e04cc283b47c74a4f8e8dab Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Mon, 17 Jul 2023 03:09:36 +0300 Subject: [PATCH 9/9] Update hints --- aiogram/dispatcher/dispatcher.py | 11 ++++++----- aiogram/types/base.py | 7 ++++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index 763a9e37..7de7960c 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -18,7 +18,7 @@ from ..fsm.strategy import FSMStrategy from ..methods import GetUpdates, TelegramMethod from ..methods.base import TelegramType from ..types import Update, User -from ..types.base import UNSET_TYPE +from ..types.base import UNSET_TYPE, UNSET from ..types.update import UpdateTypeLookupError from ..utils.backoff import Backoff, BackoffConfig from .event.bases import UNHANDLED, SkipHandler @@ -451,7 +451,7 @@ class Dispatcher(Router): polling_timeout: int = 10, handle_as_tasks: bool = True, backoff_config: BackoffConfig = DEFAULT_BACKOFF_CONFIG, - allowed_updates: Optional[Union[List[str], UNSET_TYPE]] = UNSET_TYPE, + allowed_updates: Optional[Union[List[str], UNSET_TYPE]] = UNSET, handle_signals: bool = True, close_bot_session: bool = True, **kwargs: Any, @@ -464,6 +464,7 @@ class Dispatcher(Router): :param handle_as_tasks: Run task for each event and no wait result :param backoff_config: backoff-retry config :param allowed_updates: List of the update types you want your bot to receive + By default, all used update types are enabled (resolved from handlers) :param handle_signals: handle signals (SIGINT/SIGTERM) :param close_bot_session: close bot sessions on shutdown :param kwargs: contextual data @@ -483,6 +484,9 @@ class Dispatcher(Router): if self._stopped_signal is None: self._stopped_signal = Event() + if allowed_updates is UNSET: + allowed_updates = self.resolve_used_update_types() + self._stop_signal.clear() self._stopped_signal.clear() @@ -498,9 +502,6 @@ class Dispatcher(Router): signal.SIGINT, self._signal_stop_polling, signal.SIGINT ) - if allowed_updates is UNSET_TYPE: - allowed_updates = self.resolve_used_update_types() - workflow_data = { "dispatcher": self, "bots": bots, diff --git a/aiogram/types/base.py b/aiogram/types/base.py index 707e328c..21c5bceb 100644 --- a/aiogram/types/base.py +++ b/aiogram/types/base.py @@ -24,7 +24,8 @@ class MutableTelegramObject(TelegramObject): # special sentinel object which used in situation when None might be a useful value +UNSET: Any = sentinel.UNSET UNSET_PARSE_MODE: Any = sentinel.UNSET_PARSE_MODE -UNSET_DISABLE_WEB_PAGE_PREVIEW = sentinel.UNSET_DISABLE_WEB_PAGE_PREVIEW -UNSET_PROTECT_CONTENT = sentinel.UNSET_PROTECT_CONTENT -UNSET_TYPE = type(sentinel.DEFAULT) +UNSET_DISABLE_WEB_PAGE_PREVIEW: Any = sentinel.UNSET_DISABLE_WEB_PAGE_PREVIEW +UNSET_PROTECT_CONTENT: Any = sentinel.UNSET_PROTECT_CONTENT +UNSET_TYPE: Any = type(UNSET)