Skip to content

Commit 7bd87e7

Browse files
jeffery9876Taragolisjosh-fell
authored
fix template_fields in the decorator task.docker (#29586)
* fix template_fields in the decorator `task.docker` * add tests * check the `container_name` matched * remove the docker run * Update tests/providers/docker/decorators/test_docker.py Co-authored-by: Andrey Anshin <[email protected]> * fix creating TaskInstance * manually fix code styles and other stuff, like use ' instead of " * fix static check!! * fix static check: No overload variant of "__add__" of "tuple" matches argument type "Sequence[str]" * Update airflow/providers/docker/decorators/docker.py Co-authored-by: Josh Fell <[email protected]> * another static_check fix --------- Co-authored-by: Andrey Anshin <[email protected]> Co-authored-by: Josh Fell <[email protected]>
1 parent 792416d commit 7bd87e7

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

airflow/providers/docker/decorators/docker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class _DockerDecoratedOperator(DecoratedOperator, DockerOperator):
7777

7878
custom_operator_name = "@task.docker"
7979

80-
template_fields: Sequence[str] = ("op_args", "op_kwargs")
80+
template_fields: Sequence[str] = (*DockerOperator.template_fields, "op_args", "op_kwargs")
8181

8282
# since we won't mutate the arguments, we should just do the shallow copy
8383
# there are some cases we can't deepcopy the objects (e.g protobuf).

tests/providers/docker/decorators/test_docker.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from airflow.decorators import task
2222
from airflow.exceptions import AirflowException
23+
from airflow.models import TaskInstance
2324
from airflow.models.dag import DAG
2425
from airflow.utils import timezone
2526
from airflow.utils.state import TaskInstanceState
@@ -60,6 +61,19 @@ def f(num_results):
6061
assert isinstance(result, list)
6162
assert len(result) == 50
6263

64+
def test_basic_docker_operator_with_template_fields(self, dag_maker):
65+
@task.docker(image="python:3.9-slim", container_name="python_{{dag_run.dag_id}}", auto_remove="force")
66+
def f():
67+
raise RuntimeError("Should not executed")
68+
69+
with dag_maker():
70+
ret = f()
71+
72+
dr = dag_maker.create_dagrun()
73+
ti = TaskInstance(task=ret.operator, run_id=dr.run_id)
74+
rendered = ti.render_templates()
75+
assert rendered.container_name == f"python_{dr.dag_id}"
76+
6377
def test_basic_docker_operator_multiple_output(self, dag_maker):
6478
@task.docker(image="python:3.9-slim", multiple_outputs=True, auto_remove="force")
6579
def return_dict(number: int):

0 commit comments

Comments
 (0)