uranusjr commented on code in PR #55068:
URL: https://github.com/apache/airflow/pull/55068#discussion_r2986542549
##########
task-sdk/src/airflow/sdk/definitions/_internal/templater.py:
##########
@@ -117,6 +117,48 @@ def _should_render_native(self, dag: DAG | None = None) ->
bool:
return dag.render_template_as_native_obj if dag else False
+ def _iter_templated_fields(
+ self,
+ parent: Any,
+ template_fields: Iterable[str],
+ ) -> Iterator[tuple[str, Any]]:
+ """
+ Iterate over template fields yielding ``(attr_name, value)`` pairs for
non-empty fields.
+
+ Fields whose value is falsy are skipped. Objects that do not support
+ ``__bool__`` (e.g. Pandas DataFrames) are still yielded.
+ """
+ for attr_name in template_fields:
+ try:
+ value = getattr(parent, attr_name)
+ except AttributeError:
+ raise AttributeError(
+ f"{attr_name!r} is configured as a template field "
+ f"but {type(parent).__name__} does not have this
attribute."
+ )
+ try:
+ if not value:
+ continue
+ except Exception:
+ # This may happen if the templated field points to a class
which does not support
+ # ``__bool__``, such as Pandas DataFrames:
+ #
https://github.com/pandas-dev/pandas/blob/9135c3aaf12d26f857fcc787a5b64d521c51e379/pandas/core/generic.py#L1465
+ if hasattr(self, "task_id"):
+ log.info(
+ "Unable to check if the value of type '%s' is False
for task '%s', field '%s'.",
+ type(value).__name__,
+ self.task_id,
+ attr_name,
+ )
+ else:
+ log.info(
+ "Unable to check if the value of type '%s' is False
for field '%s'.",
+ type(value).__name__,
+ attr_name,
+ )
+ # We may still want to render custom classes which do not
support __bool__
Review Comment:
Not sure if this message would be particularly useful. Probably can just
return silently?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]