Closed ddluke closed 11 months ago
This sounds like it's up to Airflow to address with how they use Jinja. Jinja is a string templating language, so the fact that rendering produces strings is not a bug. You may also be interested in Jinja's NativeEnvironment
instead of the default one.
Thanks for your reply, I understand that jinja is a string templating language. Alas, I cannot easily replace the templating engine used by apache-airflow. And Jinja's NativeEnvironment
alas doesn't help either (see how python version above becomes 3.1
, where it should really be 3.10
).
Note that this is my first issue in this project, I hope the classification of a
bug
is correct, but please correct me if I'm mistaken here.Description
There appears to be no way to prevent jinja from mutating/corrupting types of the templating context when rendering.
This issue causes nightmares in the situation where jinja is not used to render some sort of html template or the likes. A very popular and broadly applied usecase would be apache-airflow, where jinja templating is a builtin feature of operators.
In this case, the operator can mark individual fields of the class constructor as
template-able
, and dynamic jinja blocks inside arbitrary operator instance fields can be replaced with dynamic jinja references at execution time.The issue is: There appears to be no way to tell jinja to preserve the types of the templating context when resolving jinja refs.
At least the following two approaches do not work (are there any other approaches I'm not aware of?):
The default jinja environment apparently dumps all resolved jinja references to string, which causes data corruption if the jinja template ref points to anything other than a string (some integer, a list, a dict, a datetime object, etc.).
However, the jinja native environment is also no help in this scenario, because it also causes data corruption (the
ast.literal_eval
approach is not safe to use as seen in below example)Code Replication
The following code snipped and the two test functions demonstrate the issue that results from this
bug
(if it is one)Environment