@@ -225,9 +225,8 @@ def get_template_context(self) -> Context:
225225
226226 # Cache the context object, which ensures that all calls to get_template_context
227227 # are operating on the same context object.
228- context = self ._cached_template_context
229- if context is None :
230- context = {
228+ if self ._cached_template_context is None :
229+ self ._cached_template_context = {
231230 # From the Task Execution interface
232231 "dag" : self .task .dag ,
233232 "inlets" : self .task .inlets ,
@@ -249,7 +248,8 @@ def get_template_context(self) -> Context:
249248 },
250249 "conn" : ConnectionAccessor (),
251250 }
252- self ._cached_template_context = context
251+ if TYPE_CHECKING :
252+ assert self ._cached_template_context is not None
253253 if from_server :
254254 dag_run = from_server .dag_run
255255 context_from_server : Context = {
@@ -268,7 +268,7 @@ def get_template_context(self) -> Context:
268268 lambda : coerce_datetime (get_previous_dagrun_success (self .id ).end_date )
269269 ),
270270 }
271- context .update (context_from_server )
271+ self . _cached_template_context .update (context_from_server )
272272
273273 if logical_date := coerce_datetime (dag_run .logical_date ):
274274 if TYPE_CHECKING :
@@ -279,7 +279,7 @@ def get_template_context(self) -> Context:
279279 ts_nodash = logical_date .strftime ("%Y%m%dT%H%M%S" )
280280 ts_nodash_with_tz = ts .replace ("-" , "" ).replace (":" , "" )
281281 # logical_date and data_interval either coexist or be None together
282- context .update (
282+ self . _cached_template_context .update (
283283 {
284284 # keys that depend on logical_date
285285 "logical_date" : logical_date ,
@@ -306,7 +306,7 @@ def get_template_context(self) -> Context:
306306 if upstream_map_indexes is not None :
307307 setattr (self , "_upstream_map_indexes" , upstream_map_indexes )
308308
309- return context
309+ return self . _cached_template_context
310310
311311 def render_templates (
312312 self , context : Context | None = None , jinja_env : jinja2 .Environment | None = None
0 commit comments