1818from __future__ import annotations
1919
2020from collections .abc import Generator , Sequence
21- from typing import TYPE_CHECKING , Any
21+ from typing import TYPE_CHECKING
2222
2323from airflow .sdk .definitions ._internal .templater import Templater
2424from airflow .utils .context import context_merge
@@ -35,25 +35,31 @@ class BaseNotifier(LoggingMixin, Templater):
3535 """
3636 BaseNotifier class for sending notifications.
3737
38- This class can be used both synchronously and asynchronously.
39- Subclasses should implement the `notify_async` method for optimal performance .
38+ It can be used asynchronously (preferred) if `async_notify`is implemented and/or
39+ synchronously if `notify` is implemented .
4040
41- Usage:
42- # Asynchronous usage (preferred)
43- await BaseNotifier(context=context)
41+ Currently, the DAG/Task state change callbacks run on the DAG Processor and only support sync usage.
4442
45- # Synchronous usage (fallback)
46- BaseNotifier(context=context)()
43+ Usage:
44+ # Asynchronous usage
45+ notifier = Notifier()
46+ notifier.context = context
47+ await notifier
48+
49+ # Synchronous usage
50+ notifier = Notifier()
51+ notifier(context)
4752 """
4853
4954 template_fields : Sequence [str ] = ()
5055 template_ext : Sequence [str ] = ()
5156
57+ # Context stored as attribute here because parameters can't be passed to __await__
58+ context : Context
59+
5260 def __init__ (self ):
5361 super ().__init__ ()
5462 self .resolve_template_files ()
55- # Context stored as attribute here because parameters can't be passed to __await__
56- self .context = {}
5763
5864 def _update_context (self , context : Context ) -> Context :
5965 """
@@ -135,11 +141,11 @@ def __call__(self, *args) -> None:
135141 self .log .error ("Failed to send notification (sync): %s" , e )
136142 raise
137143
138- def __await__ (self ) -> Generator [ Any , Any , None ] :
144+ def __await__ (self ) -> Generator :
139145 """
140146 Make the notifier awaitable.
141147
142- Context provided in the constructor is used .
148+ Context must be provided as an attribute .
143149 """
144150 self ._update_context (self .context )
145151 self .render_template_fields (self .context )
0 commit comments