You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "ivan-afonichkin (via GitHub)" <gi...@apache.org> on 2023/08/24 10:02:23 UTC

[GitHub] [airflow] ivan-afonichkin commented on a diff in pull request #33645: Set context inside templates

ivan-afonichkin commented on code in PR #33645:
URL: https://github.com/apache/airflow/pull/33645#discussion_r1304097850


##########
tests/models/test_taskinstance.py:
##########
@@ -2890,6 +2890,33 @@ def test_echo_env_variables(self, dag_maker):
         ti.refresh_from_db()
         assert ti.state == State.SUCCESS
 
+    def test_context_inside_template(self, dag_maker):
+        def user_defined_macro():
+            from airflow.operators.python import get_current_context
+
+            get_current_context()
+
+        with dag_maker(
+            "test_context_inside_template",
+            start_date=DEFAULT_DATE,
+            end_date=DEFAULT_DATE + datetime.timedelta(days=10),
+            user_defined_macros={"user_defined_macro": user_defined_macro},
+        ):
+
+            def foo(arg):
+                print(arg)
+
+            PythonOperator(
+                task_id="context_inside_template",
+                python_callable=foo,
+                op_kwargs={"arg": "{{ user_defined_macro() }}"},
+            ),
+        dagrun = dag_maker.create_dagrun()
+        tis = dagrun.get_task_instances()
+        ti: TaskInstance = next(x for x in tis if x.task_id == "context_inside_template")
+        ti._run_raw_task()
+        assert ti.state == State.SUCCESS

Review Comment:
   It's implemented as setting a context for rendering templates inside `_execute_task_with_callbacks`. I'm not aware perhaps, but is there a way to run `_execute_task_with_callbacks` without actually running a task? Or do you think there is a better place to set up this context?



-- 
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-unsubscribe@airflow.apache.org

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