Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "airflow tasks render" cli command for mapped task instances #28698

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions airflow/cli/commands/task_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,21 +594,22 @@ def task_test(args, dag=None):

@cli_utils.action_cli(check_db=False)
@suppress_logs_and_warning
def task_render(args):
def task_render(args, dag=None):
"""Renders and displays templated fields for a given task."""
dag = get_dag(args.subdir, args.dag_id)
if not dag:
dag = get_dag(args.subdir, args.dag_id)
task = dag.get_task(task_id=args.task_id)
ti, _ = _get_ti(
task, args.map_index, exec_date_or_run_id=args.execution_date_or_run_id, create_if_necessary="memory"
)
ti.render_templates()
for attr in task.__class__.template_fields:
for attr in task.template_fields:
print(
textwrap.dedent(
f""" # ----------------------------------------------------------
# property: {attr}
# ----------------------------------------------------------
{getattr(task, attr)}
{getattr(ti.task, attr)}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uranusjr , is it a bug that DecoratedMappedOperator does not have the template_dict attribute? That's the error I get if 'task' is used above instead of ti.task

"""
)
)
Expand Down
62 changes: 62 additions & 0 deletions tests/cli/commands/test_task_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from airflow.exceptions import AirflowException, DagRunNotFound
from airflow.models import DagBag, DagRun, Pool, TaskInstance
from airflow.models.serialized_dag import SerializedDagModel
from airflow.operators.bash import BashOperator
from airflow.utils import timezone
from airflow.utils.session import create_session
from airflow.utils.state import State
Expand Down Expand Up @@ -390,6 +391,67 @@ def test_task_render(self):
assert 'echo "2016-01-01"' in output
assert 'echo "2016-01-08"' in output

def test_mapped_task_render(self):
"""
tasks render should render and displays templated fields for a given mapping task
"""
with redirect_stdout(io.StringIO()) as stdout:
task_command.task_render(
self.parser.parse_args(
[
"tasks",
"render",
"test_mapped_classic",
"consumer_literal",
"2022-01-01",
"--map-index",
"0",
]
)
)
# the dag test_mapped_classic has op_args=[[1], [2], [3]], so the first mapping task should have
# op_args=[1]
output = stdout.getvalue()
assert "[1]" in output
assert "[2]" not in output
assert "[3]" not in output
assert "property: op_args" in output

def test_mapped_task_render_with_template(self, dag_maker):
"""
tasks render should render and displays templated fields for a given mapping task
"""
with dag_maker() as dag:
templated_command = """
{% for i in range(5) %}
echo "{{ ds }}"
echo "{{ macros.ds_add(ds, 7)}}"
{% endfor %}
"""
commands = [templated_command, "echo 1"]

BashOperator.partial(task_id="some_command").expand(bash_command=commands)

with redirect_stdout(io.StringIO()) as stdout:
task_command.task_render(
self.parser.parse_args(
[
"tasks",
"render",
"test_dag",
"some_command",
"2022-01-01",
"--map-index",
"0",
]
),
dag=dag,
)

output = stdout.getvalue()
assert 'echo "2022-01-01"' in output
assert 'echo "2022-01-08"' in output

def test_cli_run_when_pickle_and_dag_cli_method_selected(self):
"""
tasks run should return an AirflowException when invalid pickle_id is passed
Expand Down