Fix @task.kubernetes_cmd TaskGroup.expand mappings by templating TaskFlow args
#59292
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #56459
Problem
@task.kubernetes_cmdbreaks 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_cmdis the task being used in the repro linked within the issue) is still aMappedArgumentobject, and the type check inside_generate_cmdssees a non-strelement and raises theTypeError: Expected echo_cmd to return a list of strings, but got ['echo', MappedArgument(...)].Solution
I aligned
@task.kubernetes_cmdwith@task.kuberneteswith regards to how templated fields are handled, so TaskFlow mappings behave consistently. Concretely, I extendedtemplate_fieldsto includeop_argsandop_kwargsalongside the existing Kubernetes-specific fields inherited fromKubernetesPodOperator.template_fields. This ensures that TaskFlow arguments—including mapping metadata such asMappedArgumentcoming fromTaskGroup.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_cmdTaskFlow function insideself.dag_maker, instantiates it, and asserts that the underlying operator’stemplate_fieldsinclude at least"op_args"and"op_kwargs". This guards against future refactors accidentally dropping TaskFlow arguments fromtemplate_fieldsand reintroducing the originalMappedArgumenttype error forTaskGroup.expand(...)with@task.kubernetes_cmd.Open to discussion!