From 71d3d87bccf70a01e2d54f081c8c131d89d90130 Mon Sep 17 00:00:00 2001 From: Zhiwei Date: Thu, 15 Dec 2022 04:32:07 -0600 Subject: [PATCH] Deprecate Conditions and Triggers Saved in `handler` Callable; Save Condition in `signal.extra` Instead (#2608) --- sanic/signals.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/sanic/signals.py b/sanic/signals.py index 3b662684..5710f1ed 100644 --- a/sanic/signals.py +++ b/sanic/signals.py @@ -154,9 +154,7 @@ class SignalRouter(BaseRouter): try: for signal in signals: params.pop("__trigger__", None) - requirements = getattr( - signal.handler, "__requirements__", None - ) + requirements = signal.extra.requirements if ( (condition is None and signal.ctx.exclusive is False) or (condition is None and not requirements) @@ -219,8 +217,13 @@ class SignalRouter(BaseRouter): if not trigger: event = ".".join([*parts[:2], "<__trigger__>"]) - handler.__requirements__ = condition # type: ignore - handler.__trigger__ = trigger # type: ignore + try: + # Attaching __requirements__ and __trigger__ to the handler + # is deprecated and will be removed in v23.6. + handler.__requirements__ = condition # type: ignore + handler.__trigger__ = trigger # type: ignore + except AttributeError: + pass signal = super().add( event, @@ -232,6 +235,7 @@ class SignalRouter(BaseRouter): signal.ctx.exclusive = exclusive signal.ctx.trigger = trigger signal.ctx.definition = event_definition + signal.extra.requirements = condition return cast(Signal, signal)