-
Notifications
You must be signed in to change notification settings - Fork 14.5k
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
Improve handling of string type and non-attribute template_fields
#21054
Improve handling of string type and non-attribute template_fields
#21054
Conversation
airflow/models/baseoperator.py
Outdated
f"The `template_fields` value for {self.task_type} is a string " | ||
"but should be a Sequence type. Converting to a Sequence type for execution. " | ||
f"Please update {self.task_type} accordingly.", |
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.
f"The `template_fields` value for {self.task_type} is a string " | |
"but should be a Sequence type. Converting to a Sequence type for execution. " | |
f"Please update {self.task_type} accordingly.", | |
f"The `template_fields` value for {self.task_type} is a string " | |
"but should be a list of string. Wrapping it in a list for execution. " | |
f"Please update {self.task_type} accordingly.", |
str
is technically a sequence, so sequence of string is a better term. But I feel the term sequence itself might not be the easiest to understand for less experienced developers, so let’s just recommend using a list instead, which is the most fool-proof syntax.
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.
Excellent point.
airflow/models/baseoperator.py
Outdated
if hasattr(parent, attr_name): | ||
content = getattr(parent, attr_name) | ||
if content: | ||
rendered_content = self.render_template(content, context, jinja_env, seen_oids) | ||
setattr(parent, attr_name, rendered_content) | ||
else: | ||
raise AttributeError( | ||
f"{attr_name!r} is configured as a template field " | ||
f"but {parent.task_type} does not have this attribute." | ||
) |
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.
if hasattr(parent, attr_name): | |
content = getattr(parent, attr_name) | |
if content: | |
rendered_content = self.render_template(content, context, jinja_env, seen_oids) | |
setattr(parent, attr_name, rendered_content) | |
else: | |
raise AttributeError( | |
f"{attr_name!r} is configured as a template field " | |
f"but {parent.task_type} does not have this attribute." | |
) | |
try: | |
content = getattr(parent, attr_name) | |
except AttributeError: | |
raise AttributeError( | |
f"{attr_name!r} is configured as a template field " | |
f"but {parent.task_type} does not have this attribute." | |
) | |
else: | |
if content: | |
rendered_content = self.render_template(content, context, jinja_env, seen_oids) | |
setattr(parent, attr_name, rendered_content) | |
EAFP over LBYL.
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.
except ... else
not needed.
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.
Ah yes. More succinct as well.
airflow/models/baseoperator.py
Outdated
if hasattr(self, field): | ||
content = getattr(self, field) | ||
if content and isinstance(content, str): | ||
self.log.info('Rendering template for %s', field) | ||
self.log.info(content) | ||
else: | ||
raise AttributeError( | ||
f"{field!r} is configured as a template field " | ||
f"but {self.task_type} does not have this attribute." | ||
) |
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.
Same as above.
e745924
to
abd1b85
Compare
abd1b85
to
69a0d62
Compare
Pushed some fixes for static check errors...come on Josh. 🤦 |
Hint:
And a sneaky preview of new Breeze2 equivalent:
|
cc: @Bowrna ^^ |
Yeah that's usually my go-to move but I completely spaced on it this time. That new command is lovely. I'll have to try it out. |
The PR most likely needs to run full matrix of tests because it modifies parts of the core of Airflow. However, committers might decide to merge it quickly and take the risk. If they don't merge it quickly - please rebase it to the latest main at your convenience, or amend the last commit of the PR, and push it with --force-with-lease. |
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.
Very nice. Love the explicit communication!
Related: #19047, #19052
Closes: #12876
This PR intends to improve both the handling of
template_fields
values declared as strings (most typically seen astemplate_fields = ("some_field")
as well as the error message for declaringtemplate_fields
that are not attributes of the operator. In the first case, a warning is logged and thetemplate_fields
is "automagically" converted to a list for the task execution. For the second case, the error message is updated from the defaultAttributeError
to something more specific to what is happening when attempting to render any template fields.^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in UPDATING.md.