This is an automated email from the ASF dual-hosted git repository.
uranusjr pushed a commit to branch v3-0-test
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/v3-0-test by this push:
new 401096b07c7 [v3-0-test] Serialize nan and infinities to string
(#53835) (#53844)
401096b07c7 is described below
commit 401096b07c7d33ff4d2e628b74a5c1fd0a9a6dc1
Author: github-actions[bot]
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Tue Jul 29 11:49:19 2025 +0800
[v3-0-test] Serialize nan and infinities to string (#53835) (#53844)
Co-authored-by: Tzu-ping Chung <[email protected]>
---
airflow-core/src/airflow/serialization/serialized_objects.py | 4 ++++
airflow-core/tests/unit/serialization/test_serialized_objects.py | 4 ++++
2 files changed, 8 insertions(+)
diff --git a/airflow-core/src/airflow/serialization/serialized_objects.py
b/airflow-core/src/airflow/serialization/serialized_objects.py
index e3cf0f019d5..324e7c69ded 100644
--- a/airflow-core/src/airflow/serialization/serialized_objects.py
+++ b/airflow-core/src/airflow/serialization/serialized_objects.py
@@ -25,6 +25,7 @@ import datetime
import enum
import itertools
import logging
+import math
import weakref
from collections.abc import Collection, Generator, Iterable, Mapping
from functools import cache, cached_property
@@ -731,6 +732,9 @@ class BaseSerialization:
# enum.IntEnum is an int instance, it causes json dumps error so
we use its value.
if isinstance(var, enum.Enum):
return var.value
+ # These are not allowed in JSON.
https://datatracker.ietf.org/doc/html/rfc8259#section-6
+ if isinstance(var, float) and (math.isnan(var) or math.isinf(var)):
+ return str(var)
return var
elif isinstance(var, dict):
return cls._encode(
diff --git a/airflow-core/tests/unit/serialization/test_serialized_objects.py
b/airflow-core/tests/unit/serialization/test_serialized_objects.py
index a0167f1b7db..7a8225a13cb 100644
--- a/airflow-core/tests/unit/serialization/test_serialized_objects.py
+++ b/airflow-core/tests/unit/serialization/test_serialized_objects.py
@@ -18,6 +18,7 @@
from __future__ import annotations
import json
+import math
from collections.abc import Iterator
from datetime import datetime, timedelta
@@ -205,6 +206,9 @@ class MockLazySelectSequence(LazySelectSequence):
[
("test_str", None, equals),
(1, None, equals),
+ (math.nan, None, lambda a, b: b == "nan"),
+ (math.inf, None, lambda a, b: b == "inf"),
+ (-math.inf, None, lambda a, b: b == "-inf"),
(timezone.utcnow(), DAT.DATETIME, equal_time),
(timedelta(minutes=2), DAT.TIMEDELTA, equals),
(Timezone("UTC"), DAT.TIMEZONE, lambda a, b: a.name == b.name),