uranusjr commented on code in PR #59566:
URL: https://github.com/apache/airflow/pull/59566#discussion_r2638749320
##########
task-sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -784,17 +784,87 @@ def startup() -> tuple[RuntimeTaskInstance, Context,
Logger]:
return ti, ti.get_template_context(), log
+def _serialize_template_field(template_field: Any, name: str) -> str | dict |
list | int | float:
+ """
+ Return a serializable representation of the templated field.
+
+ If ``templated_field`` contains a class or instance that requires recursive
+ templating, store them as strings. Otherwise simply return the field as-is.
+
+ Used sdk secrets masker to redact secrets in the serialized output.
+ """
+ import json
+
+ from airflow.sdk._shared.secrets_masker import redact
+
+ def is_jsonable(x):
+ try:
+ json.dumps(x)
+ except (TypeError, OverflowError):
+ return False
+ else:
+ return True
+
+ def translate_tuples_to_lists(obj: Any):
+ """Recursively convert tuples to lists."""
+ if isinstance(obj, tuple):
+ return [translate_tuples_to_lists(item) for item in obj]
+ if isinstance(obj, list):
+ return [translate_tuples_to_lists(item) for item in obj]
+ if isinstance(obj, dict):
+ return {key: translate_tuples_to_lists(value) for key, value in
obj.items()}
+ return obj
+
+ def sort_dict_recursively(obj: Any) -> Any:
+ """Recursively sort dictionaries to ensure consistent ordering."""
+ if isinstance(obj, dict):
+ return {k: sort_dict_recursively(v) for k, v in
sorted(obj.items())}
+ if isinstance(obj, list):
+ return [sort_dict_recursively(item) for item in obj]
+ if isinstance(obj, tuple):
+ return tuple(sort_dict_recursively(item) for item in obj)
+ return obj
+
+ max_length = conf.getint("core", "max_templated_field_length")
+
+ if not is_jsonable(template_field):
+ try:
+ serialized = template_field.serialize()
+ except AttributeError:
+ serialized = str(template_field)
+ if len(serialized) > max_length:
+ rendered = redact(serialized, name)
+ return (
+ "Truncated. You can change this behaviour in
[core]max_templated_field_length. "
+ f"{rendered[: max_length - 79]!r}... "
+ )
+ return serialized
+ if not template_field and not isinstance(template_field, tuple):
Review Comment:
Correction: tuple _is_ jsonable (it wouldn’t be a problem if it isn’t since
it’d fall into the previous `if`). The problem is it does not round-trip
properly (it’s turned into a list) and thus needs a special case.
--
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]