-
Notifications
You must be signed in to change notification settings - Fork 14.3k
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
Conversation
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
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