ephraimbuddy commented on code in PR #26750:
URL: https://github.com/apache/airflow/pull/26750#discussion_r1011799699


##########
airflow/cli/commands/task_command.py:
##########
@@ -364,14 +363,7 @@ def task_run(args, dag=None):
         print(f'Loading pickle id: {args.pickle}')
         dag = get_dag_by_pickle(args.pickle)
     elif not dag:
-        if args.local:
-            try:
-                dag = get_dag_by_deserialization(args.dag_id)
-            except AirflowException:
-                print(f'DAG {args.dag_id} does not exist in the database, 
trying to parse the dag_file')
-                dag = get_dag(args.subdir, args.dag_id)
-        else:
-            dag = get_dag(args.subdir, args.dag_id)
+        dag = get_dag(args.subdir, args.dag_id, include_examples=False)

Review Comment:
   ```diff
   
   diff --git a/airflow/cli/commands/task_command.py 
b/airflow/cli/commands/task_command.py
   index 982aa31fd5..ab05bb56a9 100644
   --- a/airflow/cli/commands/task_command.py
   +++ b/airflow/cli/commands/task_command.py
   @@ -578,13 +578,13 @@ def task_render(args):
            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)}
            """
                )
            )
   diff --git a/tests/cli/commands/test_task_command.py 
b/tests/cli/commands/test_task_command.py
   index c565f601d7..f3673fb2dd 100644
   --- a/tests/cli/commands/test_task_command.py
   +++ b/tests/cli/commands/test_task_command.py
   @@ -388,6 +388,54 @@ class TestCliTasks:
            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 mapped 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 mapped 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):
   +        """
   +        tasks render should render and displays templated fields for a 
given mapped task
   +        """
   +        with redirect_stdout(io.StringIO()) as stdout:
   +            task_command.task_render(
   +                self.parser.parse_args(
   +                    [
   +                        "tasks",
   +                        "render",
   +                        "test_mapped_task_with_template",
   +                        "some_command",
   +                        "2022-01-01",
   +                        "--map-index",
   +                        "0",
   +                    ]
   +                )
   +            )
   +        # the dag test_mapped_classic has op_args=[[1], [2], [3]], so the 
first mapped task should have op_args=[1]
   +        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
   diff --git a/tests/dags/test_mapped_task_with_templates.py 
b/tests/dags/test_mapped_task_with_templates.py
   new file mode 100644
   index 0000000000..fe0b8d0c49
   --- /dev/null
   +++ b/tests/dags/test_mapped_task_with_templates.py
   @@ -0,0 +1,36 @@
   +# Licensed to the Apache Software Foundation (ASF) under one
   +# or more contributor license agreements.  See the NOTICE file
   +# distributed with this work for additional information
   +# regarding copyright ownership.  The ASF licenses this file
   +# to you under the Apache License, Version 2.0 (the
   +# "License"); you may not use this file except in compliance
   +# with the License.  You may obtain a copy of the License at
   +#
   +#   http://www.apache.org/licenses/LICENSE-2.0
   +#
   +# Unless required by applicable law or agreed to in writing,
   +# software distributed under the License is distributed on an
   +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   +# KIND, either express or implied.  See the License for the
   +# specific language governing permissions and limitations
   +# under the License.
   +from __future__ import annotations
   +
   +import datetime
   +from textwrap import dedent
   +
   +from airflow import DAG
   +from airflow.operators.bash import BashOperator
   +
   +with DAG(dag_id="test_mapped_task_with_template", 
start_date=datetime.datetime(2022, 1, 1)) as dag:
   +    templated_command = dedent(
   +        """
   +    {% 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)
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to