From a77b9bf0d6c47d440750545998812d59c03462b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Tshipenchko?= Date: Sat, 8 Apr 2023 20:38:11 +0600 Subject: [PATCH] Resolve #1155: Different signature of startup/shutdown events on polling and webhooks (#1156) * Code refactor - Use 'or' istead of 'A if A else B' - Raise new error from catched error: raise Error from e * Fixed signature of startup/shutdown events - Include the **dispatcher.workflow_data as the handler arguments --- CHANGES/1155.bugfix.rst | 1 + aiogram/dispatcher/dispatcher.py | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 CHANGES/1155.bugfix.rst diff --git a/CHANGES/1155.bugfix.rst b/CHANGES/1155.bugfix.rst new file mode 100644 index 00000000..753ffc96 --- /dev/null +++ b/CHANGES/1155.bugfix.rst @@ -0,0 +1 @@ +Fixed signature of startup/shutdown events to include the **dispatcher.workflow_data as the handler arguments. diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index 71791f72..8485d83c 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -80,9 +80,9 @@ class Dispatcher(Router): # FSM middleware should always be registered after User context middleware # because here is used context from previous step self.fsm = FSMContextMiddleware( - storage=storage if storage else MemoryStorage(), + storage=storage or MemoryStorage(), strategy=fsm_strategy, - events_isolation=events_isolation if events_isolation else DisabledEventIsolation(), + events_isolation=events_isolation or DisabledEventIsolation(), ) if not disable_fsm: # Note that when FSM middleware is disabled, the event isolation is also disabled @@ -251,14 +251,14 @@ class Dispatcher(Router): try: update_type = update.event_type event = update.event - except UpdateTypeLookupError: + except UpdateTypeLookupError as e: warnings.warn( "Detected unknown update type.\n" "Seems like Telegram Bot API was updated and you have " "installed not latest version of aiogram framework", RuntimeWarning, ) - raise SkipHandler() + raise SkipHandler() from e kwargs.update(event_update=update) @@ -373,7 +373,7 @@ class Dispatcher(Router): loop = asyncio.get_running_loop() waiter = loop.create_future() - def release_waiter(*args: Any) -> None: + def release_waiter(*_: Any) -> None: if not waiter.done(): waiter.set_result(None) @@ -488,8 +488,13 @@ class Dispatcher(Router): signal.SIGINT, self._signal_stop_polling, signal.SIGINT ) - workflow_data = {"dispatcher": self, "bots": bots, "bot": bots[-1]} - workflow_data.update(kwargs) + workflow_data = { + "dispatcher": self, + "bots": bots, + "bot": bots[-1], + **kwargs, + **self.workflow_data, + } await self.emit_startup(**workflow_data) loggers.dispatcher.info("Start polling") try: