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

Support jinja2 native Python types #14603

Merged
merged 4 commits into from
May 6, 2021
Merged

Conversation

kaxil
Copy link
Member

@kaxil kaxil commented Mar 4, 2021

This is second attempt to #4900

Docs: https://jinja.palletsprojects.com/en/2.11.x/nativetypes/

>>> from jinja2 import nativetypes
>>> ne = nativetypes.NativeEnvironment()
>>> import pendulum
>>> ne.from_string('{{ x }}').render(x=pendulum.now())
<Pendulum [2021-03-04T15:33:17.073343+00:00]>

>>> ne.from_string('{{ x }}').render(x=pendulum.now().isoformat())
'2021-03-04T15:33:29.516540+00:00'
>>> ne.from_string('{{ x }}').render(x="2012-10-10")
'2012-10-10'

Current:

>>> environment.Environment().from_string('{{ ["w","x"] }}').render()
"['w', 'x']"

Proposed:

>>> nativetypes.NativeEnvironment().from_string('{{ ["w","x"] }}').render()
['w', 'x']

The problem with replacing Environment as NativeEnvironment only is
that NativeEnvironment does not raise an error on Undefined templates so it won't respect DAG.template_undefined:

https://github.com/pallets/jinja/blob/2.11.3/src/jinja2/nativetypes.py#L70-L94


^ Add meaningful description above

Read the Pull Request Guidelines for more information.
In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in UPDATING.md.

Copy link
Member Author

@kaxil kaxil left a comment

Choose a reason for hiding this comment

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

If the approach looks correct I will go ahead and docs for the same.

@kaxil kaxil marked this pull request as ready for review March 5, 2021 09:31
Copy link
Member

@ashb ashb left a comment

Choose a reason for hiding this comment

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

Not a huge fan of needing to add a new arg (I understand why we need it though);

Perhaps we could make something like this work:

from airflow.templates import native_template as nt

task = MockOperator(task_id="op1", arg1=nt("{{ foo }}"(, arg2=nt("{{ bar }}") arg3='{{ ds }}' )

@turbaszek
Copy link
Member

I think that flag on DAG level will make things more consistent. The nt solution proposed by @ashb is interesting one but it's a bit of additional work - at least to me.

Whatever we will decide - lets extend the docs to cover this feature 👌

@kaxil
Copy link
Member Author

kaxil commented Apr 24, 2021

Re-thinking this -- I agree with Tomek that keeping it same per DAG makes it more consistent and reason about and remove complexity.

@ashb WDYT? If you agree with current approach, I can add docs to describe this feature

@kaxil kaxil added this to the Airflow 2.1 milestone Apr 24, 2021
@ashb
Copy link
Member

ashb commented Apr 30, 2021

Sure, I guess this will work and it's unlikely people will want to mix them in one dag? Not sure.

Docs now please!

@kaxil
Copy link
Member Author

kaxil commented May 5, 2021

Sure, I guess this will work and it's unlikely people will want to mix them in one dag? Not sure.

Docs now please!

Done in 4aacd2b

@github-actions
Copy link

github-actions bot commented May 5, 2021

The Workflow run is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.

kaxil added 4 commits May 5, 2021 16:00
This is second attempt to apache#4900

Docs: https://jinja.palletsprojects.com/en/2.11.x/nativetypes/

```python
>>> from jinja2 import nativetypes
>>> ne = nativetypes.NativeEnvironment()
>>> import pendulum
>>> ne.from_string('{{ x }}').render(x=pendulum.now())
<Pendulum [2021-03-04T15:33:17.073343+00:00]>

>>> ne.from_string('{{ x }}').render(x=pendulum.now().isoformat())
'2021-03-04T15:33:29.516540+00:00'
>>> ne.from_string('{{ x }}').render(x="2012-10-10")
'2012-10-10'
```

Current:

```python
>>> environment.Environment().from_string('{{ ["w","x"] }}').render()
"['w', 'x']"
```

Proposed:
```python
>>> nativetypes.NativeEnvironment().from_string('{{ ["w","x"] }}').render()
['w', 'x']
```
The problem with replacing `NativeEnvironment` as `Environment` only is
that `NativeEnvironment` does not raise an error on Undefined templates:

https://github.com/pallets/jinja/blob/2.11.3/src/jinja2/nativetypes.py#L70-L94
@kaxil
Copy link
Member Author

kaxil commented May 6, 2021

Added docs & Tests are green, ping @ashb @turbaszek

Copy link
Member

@potiuk potiuk left a comment

Choose a reason for hiding this comment

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

Nice!

@github-actions github-actions bot added the full tests needed We need to run full set of tests for this PR to merge label May 6, 2021
@github-actions
Copy link

github-actions bot commented May 6, 2021

The PR most likely needs to run full matrix of tests because it modifies parts of the core of Airflow. However, committers might decide to merge it quickly and take the risk. If they don't merge it quickly - please rebase it to the latest master at your convenience, or amend the last commit of the PR, and push it with --force-with-lease.

@kaxil kaxil merged commit 15f5bf4 into apache:master May 6, 2021
@kaxil kaxil deleted the jinja-native branch May 6, 2021 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
full tests needed We need to run full set of tests for this PR to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants