Repository navigation
Conversation
Adopt Jinja2 NativeEnvironment
Template to support jinja2 native python types
|
|
||
| from jinja2 import Environment | ||
| try: | ||
| from jinja2.nativetypes import NativeEnvironment as Environment |
There was a problem hiding this comment.
We should just update the minimum version of Jinja we require instead.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Sorry, I mean update jinja version to one that always has NativeEnvironment - so we don't have to have the except ImportError.
There was a problem hiding this comment.
You are right! Thanks for the suggestion!
|
Have you analyzed the use of literal_eval? It is currently used in some operators airflow/airflow/contrib/operators/jenkins_job_trigger_operator.py Lines 138 to 139 in 2c255e9 airflow/airflow/operators/docker_operator.py Lines 244 to 248 in 067a1e3 |
|
@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. So I am thinking to do ast.literal_eval() inside airflow.models' render_template() function. @ashb |
|
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 |
|
Closing this PR as the Native type support does funny things on dates:
Shame |
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'] ```
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'] ```
Make sure you have checked all steps below.
Jira
Description
Tests
Commits
Documentation
Code Quality
flake8