airflow icon indicating copy to clipboard operation
airflow copied to clipboard

Fix `@task.kubernetes_cmd` TaskGroup.expand mappings by templating TaskFlow args

Open akkik04 opened this issue 1 month ago • 1 comments


Closes #56459

Problem

@task.kubernetes_cmd breaks when used with TaskFlow-style mapping inside a TaskGroup. The root cause is that we didn't mark TaskFlow arguments (op_args / op_kwargs) as template_fields, thus the validation of the return value of the Python callable occurs before those fields are resolved by the TaskFlow/mapping machinery.

As a result, when group_task.expand(...) is used, the name parameter that reaches the task (echo_cmd is the task being used in the repro linked within the issue) is still a MappedArgument object, and the type check inside _generate_cmds sees a non-str element and raises the TypeError: Expected echo_cmd to return a list of strings, but got ['echo', MappedArgument(...)].

Solution

I aligned @task.kubernetes_cmd with @task.kubernetes with regards to how templated fields are handled, so TaskFlow mappings behave consistently. Concretely, I extended template_fields to include op_args and op_kwargs alongside the existing Kubernetes-specific fields inherited from KubernetesPodOperator.template_fields. This ensures that TaskFlow arguments—including mapping metadata such as MappedArgument coming from TaskGroup.expand(...) will be run through the normal TaskFlow resolution scheme and reach the user function as plain Python values instead of unresolved mapping objects.

Finally, I add a focused unit test, test_kubernetes_cmd_template_fields_include_taskflow_args, which creates a simple @task.kubernetes_cmd TaskFlow function inside self.dag_maker, instantiates it, and asserts that the underlying operator’s template_fields include at least "op_args" and "op_kwargs". This guards against future refactors accidentally dropping TaskFlow arguments from template_fields and reintroducing the original MappedArgument type error for TaskGroup.expand(...) with @task.kubernetes_cmd.

Open to discussion!

akkik04 avatar Dec 11 '25 06:12 akkik04

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst) Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our prek-hooks will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits. Apache Airflow is a community-driven project and together we are making it better 🚀. In case of doubts contact the developers at: Mailing List: [email protected] Slack: https://s.apache.org/airflow-slack

boring-cyborg[bot] avatar Dec 11 '25 06:12 boring-cyborg[bot]