Skip to content

[AIRFLOW-3948] Operator list as templated field issue - #4848

Closed
ryanyuan wants to merge 4 commits into
apache:masterfrom
ryanyuan:jinjia-get-templated-list
Closed

ryanyuan wants to merge 4 commits into
apache:masterfrom
ryanyuan:jinjia-get-templated-list

Conversation

@ryanyuan

@ryanyuan ryanyuan commented Mar 6, 2019 •

Copy link
Copy Markdown
Contributor

Operator list as templated field

This is a global fix for #4770

Make sure you have checked all steps below.

Jira

Description

  • Here are some details about my PR, including screenshots of any UI changes:

Tests

  • My PR adds the following unit tests OR does not need testing for this extremely good reason:

Commits

  • My commits all reference Jira issues in their subject lines, and I have squashed multiple commits if they address the same issue. In addition, my commits follow the guidelines from "How to write a good git commit message":
    1. Subject is separated from body by a blank line
    2. Subject is limited to 50 characters (not including Jira issue reference)
    3. Subject does not end with a period
    4. Subject uses the imperative mood ("add", not "adding")
    5. Body wraps at 72 characters
    6. Body explains "what" and "why", not "how"

Documentation

  • In case of new functionality, my PR adds documentation that describes how to use it.
    • When adding new operators/hooks/sensors, the autoclass documentation generation needs to be added.
    • All the public functions and the classes in the PR contain docstrings that explain what it does
    • If you implement backwards incompatible changes, please leave a note in the Updating.md so we can assign it to a appropriate release

Code Quality

  • Passes flake8

ryanyuan added 4 commits March 6, 2019 14:13
Add get_templated_list to utils.helper
Fix operator list as templated field issue
Fix operators' list as templated field issue
Fix operators' list as templated field issue
@ashb

ashb commented Mar 6, 2019 •

Copy link
Copy Markdown
Member

I don't think this sort of special case handling should be needed after #4292 (which is in master but not released, will make it in to 1.10.3).

For example https://github.com/VilledeMontreal/incubator-airflow/blob/d8841ea88797f908d988c4121cae59d3fcd82518/tests/models.py#L421-L424

Maybe all the change we need to do here is add more fields to be templated in these operators.

@ryanyuan

ryanyuan commented Mar 7, 2019

Copy link
Copy Markdown
Contributor Author

@ashb Thank you for looking at my PR.

I looked at the commit you mentioned above. It looks like the issue it's fixed is not for the issue I've raised.

If I pass a Jinja template to a templated field, the type of the content would be string. In that case, it will render the actual value using Jinja template as string.

For example:

def push_value(**kwargs):
    kwargs['ti'].xcom_push(key='push_key', value=[1, 2, 3])

def do_nothing(**kwargs):
    return

push_task = PythonOperator(
    task_id='push_task',
    dag=dag,
    python_callable=push_value,
)

pull_task = PythonOperator(
    templates_list='{{ ti.xcom_pull(key="push_key") }}',
    task_id='pull_task',
    dag=dag,
    python_callable=do_nothing,
)

push_task >> pull_task

Inside pull_task, the type of templates_list is string instead of list.

My fix is specifcally for passing Jinja templated list to a templated field. And the rendered value should be converted from string to list.

@ashb

ashb commented Mar 7, 2019

Copy link
Copy Markdown
Member

Ah okay, similar but not the same issue.

In the specific case of PythonOperator you can do this:

def push_value(**kwargs):
    kwargs['ti'].xcom_push(key='push_key', value=[1, 2, 3])

def do_nothing(ti, **kwargs):
    mylist = ti.xcom_pull(key='push_key')
    print(repr(mylist))
    return

push_task = PythonOperator(
    task_id='push_task',
    provider_context=True,
    dag=dag,
    python_callable=push_value,
)

pull_task = PythonOperator(
    task_id='pull_task',
    provide_context=True,
    dag=dag,
    python_callable=do_nothing,
)

push_task >> pull_task

(I know that only works for python operator, and not a general solution.)

I think https://issues.apache.org/jira/browse/AIRFLOW-3362 is the way to fix this more generally - that one: works with types other than list, and doesn't need each operator to handle it specifically.

@ryanyuan

ryanyuan commented Mar 7, 2019

Copy link
Copy Markdown
Contributor Author

@ashb Oh awesome! I will have a look at the new Jinja2 library. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants