This is an automated email from the ASF dual-hosted git repository.
vatsrahul1001 pushed a commit to branch v3-4-test
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/v3-4-test by this push:
new 5a72d44a059 [v3-4-test] Fix `map_index_template` failing for
numeric-looking labels with `render_template_as_native_obj` (#71592) (#74978)
5a72d44a059 is described below
commit 5a72d44a059d0c3e356e9deab72853916f2f7d7b
Author: github-actions[bot]
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Sun Oct 18 15:32:48 2026 +0530
[v3-4-test] Fix `map_index_template` failing for numeric-looking labels
with `render_template_as_native_obj` (#71592) (#74978)
* Explicitly convert map_index to str
* Address comments
(cherry picked from commit 47d929c702be22d289b13316b6173e6a863a4c54)
Co-authored-by: Prajwal Agarwal
<[email protected]>
---
.../src/airflow/sdk/execution_time/task_runner.py | 5 +--
.../task_sdk/execution_time/test_task_runner.py | 39 ++++++++++++++++++++++
2 files changed, 42 insertions(+), 2 deletions(-)
diff --git a/task-sdk/src/airflow/sdk/execution_time/task_runner.py
b/task-sdk/src/airflow/sdk/execution_time/task_runner.py
index 77dbe0b1bc8..88bf9ca01c7 100644
--- a/task-sdk/src/airflow/sdk/execution_time/task_runner.py
+++ b/task-sdk/src/airflow/sdk/execution_time/task_runner.py
@@ -73,6 +73,7 @@ from airflow.sdk.definitions.asset import (
AssetUniqueKey,
AssetUriRef,
)
+from airflow.sdk.definitions.context import render_template_to_string
from airflow.sdk.definitions.mappedoperator import MappedOperator
from airflow.sdk.definitions.param import process_params
from airflow.sdk.definitions.xcom_arg import PlainXComArg
@@ -2339,8 +2340,8 @@ def _render_map_index(context: Context, ti:
RuntimeTaskInstance, log: Logger) ->
if (template := context.get("map_index_template")) is None:
return None
log.debug("Rendering map_index_template", template_length=len(template))
- jinja_env = ti.task.dag.get_template_env()
- rendered_map_index = jinja_env.from_string(template).render(context)
+ jinja_env = ti.task.dag.get_template_env(force_sandboxed=True)
+ rendered_map_index =
render_template_to_string(jinja_env.from_string(template), context)
log.debug("Map index rendered", length=len(rendered_map_index))
return rendered_map_index
diff --git a/task-sdk/tests/task_sdk/execution_time/test_task_runner.py
b/task-sdk/tests/task_sdk/execution_time/test_task_runner.py
index af2a15dca34..cbce1bb32d1 100644
--- a/task-sdk/tests/task_sdk/execution_time/test_task_runner.py
+++ b/task-sdk/tests/task_sdk/execution_time/test_task_runner.py
@@ -2543,6 +2543,45 @@ def
test_execute_success_task_with_rendered_map_index(create_runtime_ti, mock_su
assert ti.rendered_map_index == "Hello! test_run"
[email protected](
+ ("map_index_template", "expected"),
+ [
+ ("{{ ti.try_number }}", "1"),
+ ("index-{{ ti.try_number }}", "index-1"),
+ ("{{ '3.10' }}", "3.10"),
+ ],
+)
[email protected]("render_template_as_native_obj", [False, True])
+def test_execute_success_task_with_numeric_rendered_map_index(
+ create_runtime_ti,
+ mock_supervisor_comms,
+ map_index_template,
+ expected,
+ render_template_as_native_obj,
+):
+ """Test that numeric-looking map indices remain strings in both rendering
modes."""
+
+ def test_function():
+ return "test function"
+
+ with DAG(
+ dag_id="dag_with_numeric_map_index_template",
+ schedule=None,
+ render_template_as_native_obj=render_template_as_native_obj,
+ ):
+ task = PythonOperator(
+ task_id="test_task",
+ python_callable=test_function,
+ map_index_template=map_index_template,
+ )
+
+ ti = create_runtime_ti(task)
+
+ run(ti, ti.get_template_context(), log=mock.MagicMock())
+
+ assert ti.rendered_map_index == expected
+
+
def test_execute_failed_task_with_rendered_map_index(create_runtime_ti,
mock_supervisor_comms):
"""Test that the map index is rendered in the task context."""