-
Notifications
You must be signed in to change notification settings - Fork 13.7k
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
Create Undefined Jinja Variables Rule #11241
Create Undefined Jinja Variables Rule #11241
Conversation
Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/airflow/blob/master/CONTRIBUTING.rst)
|
Can you please rebase to latest v1-10-test ? It should work now. |
@ashmeet13 ^^ |
Hey @potiuk rebased the branch.
Could you please guide towards how should I be fixing them? |
The quarantined tests are kind and f ok to fail. Those are flaky tests that we know are not stable. The k8s ones are likely a transient error . So do not worry about those |
Got it @potiuk. Thanks for letting me now. |
f08f4c7
to
60d021a
Compare
Hey @potiuk, made a few changes in my branch today and rebased to latest Also - who do I request a review from? |
Not sure what's going on here but there is at least one commit that should not be there from what I see (the first one). So something's going on with this (and I See some strange/intermittent errors). can you please try to rebase again on top of v1-10-test and push ? v1-10-test seems to build all right. |
I think I have messed up the branch. When I try to rebase again as you suggested it continues to Not really sure what to do. Sorry for the trouble. What would your suggestion be to do? |
60d021a
to
fcb3b8d
Compare
fcb3b8d
to
25b6cb0
Compare
The Build Workflow run is cancelling this PR. It in earlier duplicate of 1029499 run. |
Hey @potiuk - took a while but I believe the branch is fixed now. Marking the branch |
|
||
return messages | ||
|
||
def _dag_level_(self, dag): |
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.
I don't understand the name of this method. Could you use a more descriptive name?
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.
Fixing this with a better name.
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.
Fixed names in 08b0f75
dags = dagbag.dags | ||
messages = [] | ||
for dag_id, dag in dags.items(): | ||
dag_messages = self._dag_level_(dag) |
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.
Do you think it is worth omitting checking some DAGs in some cases?
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.
Didn't exactly get this?
Which DAGs could be omitted from checking?
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.
DAGs in which the user has decided about a specific behavior should not be checked.
with DAG(dag_id="case_a"):
with DAG(dag_id="case_b", template_undefined=jinja2.StrictUndefined):
with DAG(dag_id="case_c", template_undefined=jinja2.DebugUndefined):
with DAG(dag_id="case_d", template_undefined=jinja2.Undefined):
Only dag_a
should be checked. The others should be omitted as they will work properly in Airlfow 2.0.
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.
Any progress?
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.
I missed this comment - my fault.
How exactly would we check this?
My thought process -
This would require the check to read the Python scripts in which the DAGs have been defined. Then read line by line where the DAG is defined and then check whether template_undefined
is defined or not.
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.
It seems to me that it is only possible if we make changes to the DAG constructor and get_template_env method.
Line 238 in d8aa24b
template_undefined: Type[jinja2.StrictUndefined] = jinja2.StrictUndefined, |
- template_undefined: Type[jinja2.StrictUndefined] = jinja2.StrictUndefined,
+ template_undefined: Optional[Type[jinja2.StrictUndefined]] = None,
Line 968 in d8aa24b
'undefined': self.template_undefined, |
- 'undefined': self.template_undefined,
+ 'undefined': self.template_undefined or jinja2.StrictUndefined,,
After making the above changes, we can check the class attribute to test our condition without any major problems.
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.
I see. Makes sense.
I believe this would require a new PR for the change?
Should I raise one for the same?
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.
Why does this require new PR? I think you can update this PR.
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.
Got it. Working on the changes.
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.
Added the changes w.r.t to v1-10-stable
as the target branch.
The code changes you had suggested were w.r.t to master
branch.
Also added the logic to skip check for DAG when template_undefined
is explicitly mentioned
for key, value in rendered_content.items(): | ||
debug_error_messages.union(self._check_rendered_content(str(value))) | ||
return debug_error_messages | ||
|
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.
What is the expected behavior in the "else" case?
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.
My bad. Got neglected from my end. Working on adding this logic.
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.
Added the logic in 08b0f75
097e383
to
3edc89c
Compare
Ignore the Quarantined one, core:Sqlite one seems to be just flaky so that can be ignored too |
title = "Jinja Template Variables cannot be undefined" | ||
|
||
description = """\ | ||
Jinja Templates have been updated to the following rule - jinja2.StrictUndefined |
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.
Here it should be written that the user should correct the templates or pass template_undefined=jinja2.Undefined
to the DAG.
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.
Got it. Added these instructions in description
title = "Jinja Template Variables cannot be undefined" | ||
|
||
description = """\ | ||
Jinja Templates have been updated to the following rule - jinja2.StrictUndefined |
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.
Jinja Templates have been updated to the following rule - jinja2.StrictUndefined | |
The default behavior for DAG's Jinja templates has changed. Now, more restrictive validation of non-existent variables is applied - `jinja2.StrictUndefined`. |
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.
Added this suggestion
The Workflow run is cancelling this PR. Building images for the PR has failed. Follow the the workflow link to check the reason. |
@turbaszek Can you look at it? |
@turbaszek requesting for a review whenever you get the chance. |
6a128da
to
2d509cf
Compare
@ashmeet13 this change looks good to me, can you please squash the commits and the rebase onto current |
Yup. Just to confirm what exactly needs to be done? Could you clarify @turbaszek |
Sorry my bad! I meant
In this way we will be able to run whole CI tests hopefully |
5017fdc
to
5ef4259
Compare
I guess that was a bad rebase :) @ashmeet13 ? Seems that it is going to be better now. |
Yup. |
The checks have passed. Anything else needed to be done @turbaszek ? |
Adding a rule to check for undefined jinja variables when upgrading to Airflow2.0
5ef4259
to
ee2ccf7
Compare
Can this PR be merged @turbaszek @potiuk if no other changes required? |
Awesome work, congrats on your first merged pull request! |
) Adding a rule to check for undefined jinja variables when upgrading to Airflow2.0 (cherry picked from commit 18100a0)
Hi,
Adding a rule to check for Undefined Jinja Variables (Issue: #11144) when upgrading to Airflow2.0
Logic - Use a DagBag to pull all dags and iterate over every dag. For every dag the task will be rendered using
an updated Jinja Environment using - jinja2.DebugUndefined. This will render the template leaving undefined variables
as they were. Using regex we can extract the variables and present possible error cases when upgrading.
Since I am fairly new to Airflow - please do guide if there is a better approach to implement this rule.
Thanks!
Related: #8765
Fixes: #11144