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

[AIRFLOW-3362] [WIP]Template to support jinja2 native python types #4900

Closed
wants to merge 2 commits into from

Conversation

ryanyuan
Copy link
Contributor

Make sure you have checked all steps below.

Jira

  • My PR addresses the following Airflow-3362 issues and references them in the PR title. For example, "[AIRFLOW-XXX] My Airflow PR"
    • https://issues.apache.org/jira/browse/AIRFLOW-XXX
    • In case you are fixing a typo in the documentation you can prepend your commit with [AIRFLOW-XXX], code changes always need a Jira issue.
    • In case you are proposing a fundamental code change, you need to create an Airflow Improvement Proposal(AIP).

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

Adopt Jinja2 NativeEnvironment
Template to support jinja2 native python types
@@ -18,7 +18,10 @@
# under the License.
import six

from jinja2 import Environment
try:
from jinja2.nativetypes import NativeEnvironment as Environment
Copy link
Member

Choose a reason for hiding this comment

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

We should just update the minimum version of Jinja we require instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should just update the minimum version of Jinja we require instead.

Updating the minimum version of Jinja cannot resolve the #4770 (list as templated field issue). Referring to your suggestion in that PR, we can either import jinja2.NativeEnvironment or convert specific templated fields into list type in those operators.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I mean update jinja version to one that always has NativeEnvironment - so we don't have to have the except ImportError.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right! Thanks for the suggestion!

@mik-laj
Copy link
Member

mik-laj commented Mar 23, 2019

Have you analyzed the use of literal_eval? It is currently used in some operators

import ast
self.parameters = ast.literal_eval(self.parameters)

if self.command is not None and self.command.strip().find('[') == 0:
commands = ast.literal_eval(self.command)
else:
commands = self.command
return commands

@ryanyuan
Copy link
Contributor Author

ryanyuan commented Apr 5, 2019

@mik-laj Yes, in #4770, I used ast.literal_eval() to do the job, but on a local level. And I was suggested to do it, in #4848, on a global level using Jinja2's NativeEnvironment.

Now I am having a bottleneck, which is Jinja2 trying to get the templated field's value of date type.
E.g.
Templated value is 2019-11-11. And Jinja2 sees it as subtraction and returns 1997.

So I am thinking to do ast.literal_eval() inside airflow.models' render_template() function. @ashb

@ashb
Copy link
Member

ashb commented Apr 5, 2019

K, so full Native Environment won't work, that's a shame.

One change I'd suggest to #4770 is to at least override the render_template function on the operator, rather than having to do it in the execute function.

@ashb
Copy link
Member

ashb commented Jun 21, 2019

Closing this PR as the Native type support does funny things on dates:

Templated value is 2019-11-11. And Jinja2 sees it as subtraction and returns 1997.

Shame

@ashb ashb closed this Jun 21, 2019
kaxil added a commit to astronomer/airflow that referenced this pull request Mar 4, 2021
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']
```
kaxil added a commit to astronomer/airflow that referenced this pull request May 5, 2021
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']
```
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.

3 participants