uranusjr commented on a change in pull request #16352:
URL: https://github.com/apache/airflow/pull/16352#discussion_r679619712



##########
File path: airflow/models/taskinstance.py
##########
@@ -1530,21 +1532,25 @@ def get_template_context(self, session=None) -> Context:
         integrate_macros_plugins()
 
         params = {}  # type: Dict[str, Any]
-        run_id = ''
-        dag_run = None
-        if hasattr(task, 'dag'):
-            if task.dag.params:
-                params.update(task.dag.params)
-            from airflow.models.dagrun import DagRun  # Avoid circular import
-
-            dag_run = (
-                session.query(DagRun)
-                .filter_by(dag_id=task.dag.dag_id, 
execution_date=self.execution_date)
-                .first()
+        with contextlib.suppress(AttributeError):
+            params.update(task.dag.params)
+
+        dag_run = self.get_dagrun()
+
+        # FIXME: Many tests don't create a DagRun. We should fix the tests.
+        if dag_run is None:
+            FakeDagRun = namedtuple(
+                "FakeDagRun",
+                # A minimal set of attributes to keep things working.
+                "conf data_interval_start data_interval_end external_trigger 
run_id",
+            )
+            dag_run = FakeDagRun(
+                conf=None,
+                data_interval_start=None,
+                data_interval_end=None,
+                external_trigger=False,
+                run_id="",

Review comment:
       What’s the advantage of None here? IIUC a “real” DagRun cannot have 
`run_id=None` so I’m using consistent type here in case there’s some function 
call expecting `str` (of course `run_id` cannot be empty either, but at least 
the type is correct).




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to