Improve handling of string type and non-attribute template_fields#21054
Conversation
There was a problem hiding this comment.
| 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.
| 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.
Ah yes. More succinct as well.
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. |
potiuk
left a comment
There was a problem hiding this comment.
Very nice. Love the explicit communication!
Related: #19047, #19052
Closes: #12876
This PR intends to improve both the handling of
template_fieldsvalues declared as strings (most typically seen astemplate_fields = ("some_field")as well as the error message for declaringtemplate_fieldsthat are not attributes of the operator. In the first case, a warning is logged and thetemplate_fieldsis "automagically" converted to a list for the task execution. For the second case, the error message is updated from the defaultAttributeErrorto 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.