Copilot commented on code in PR #63871:
URL: https://github.com/apache/airflow/pull/63871#discussion_r3066484773
##########
airflow-core/src/airflow/serialization/helpers.py:
##########
@@ -61,25 +65,32 @@ def translate_tuples_to_lists(obj: Any):
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."""
+ def sort_and_make_static_dict_recursively(obj: Any) -> Any:
+ """Recursively sort dictionaries and make callable value static to
ensure consistent ordering."""
+ if callable(obj):
+ return make_callable_static(obj)
if isinstance(obj, dict):
- return {k: sort_dict_recursively(v) for k, v in
sorted(obj.items())}
+ return {k: sort_and_make_static_dict_recursively(v) for k, v in
sorted(obj.items())}
if isinstance(obj, list):
- return [sort_dict_recursively(item) for item in obj]
+ return [sort_and_make_static_dict_recursively(item) for item in
obj]
if isinstance(obj, tuple):
- return tuple(sort_dict_recursively(item) for item in obj)
+ return tuple(sort_and_make_static_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()
+ # If the callable objects are in dict values, it makes is_jsonable
False.
+ # So, exclude dicts here; they are handled separately in the logic
below.
+ if isinstance(template_field, dict):
+ template_field =
sort_and_make_static_dict_recursively(template_field)
+ serialized = str(template_field)
Review Comment:
`sort_and_make_static_dict_recursively()` uses `sorted(obj.items())` when
serializing *non-JSONable* dicts (the new callable-in-values case). This can
raise `TypeError` for dicts with mixed / non-orderable key types (e.g. `{1:
"a", "b": <lambda>}`) where previously this path fell back to
`str(template_field)` and would not error. Consider guarding the sort with
`try/except TypeError` (fallback to unsorted iteration) or sorting with a
stable key function (e.g. based on `str(key)`) to avoid breaking DAG
serialization on such inputs.
--
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]