From c6a681aa6bafa7fe268a607f196483a9a6683d85 Mon Sep 17 00:00:00 2001 From: Francesco Frassinelli Date: Tue, 28 Sep 2021 16:10:51 +0200 Subject: [PATCH 1/3] Add support for datetime.time in json_int_dttm_ser --- superset/utils/core.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/superset/utils/core.py b/superset/utils/core.py index 71d59348c100d..84f025214ab65 100644 --- a/superset/utils/core.py +++ b/superset/utils/core.py @@ -562,6 +562,8 @@ def base_json_conv(obj: Any,) -> Any: # pylint: disable=inconsistent-return-sta return float(obj) if isinstance(obj, uuid.UUID): return str(obj) + if isinstance(obj, time): + return str(obj) if isinstance(obj, timedelta): return format_timedelta(obj) if isinstance(obj, bytes): @@ -584,7 +586,7 @@ def json_iso_dttm_ser(obj: Any, pessimistic: bool = False) -> str: val = base_json_conv(obj) if val is not None: return val - if isinstance(obj, (datetime, date, time, pd.Timestamp)): + if isinstance(obj, (datetime, date, pd.Timestamp)): obj = obj.isoformat() else: if pessimistic: From 968be66b3d732c50bea5dfa3a137f8e002e63bc3 Mon Sep 17 00:00:00 2001 From: Francesco Frassinelli Date: Tue, 28 Sep 2021 16:46:42 +0200 Subject: [PATCH 2/3] Test base_json_conv support for datetime.time --- tests/integration_tests/utils_tests.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration_tests/utils_tests.py b/tests/integration_tests/utils_tests.py index 1f01d437be975..5ac41beca5e4d 100644 --- a/tests/integration_tests/utils_tests.py +++ b/tests/integration_tests/utils_tests.py @@ -117,6 +117,7 @@ def test_base_json_conv(self): assert isinstance(base_json_conv(set([1])), list) is True assert isinstance(base_json_conv(Decimal("1.0")), float) is True assert isinstance(base_json_conv(uuid.uuid4()), str) is True + assert isinstance(base_json_conv(time()), str) is True assert isinstance(base_json_conv(timedelta(0)), str) is True def test_zlib_compression(self): From 70d0527e5432ed5e04444a03b67e49ae8b56c96a Mon Sep 17 00:00:00 2001 From: Francesco Frassinelli Date: Tue, 28 Sep 2021 21:56:22 +0200 Subject: [PATCH 3/3] Group types by conversion function for JSON dump --- superset/utils/core.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/superset/utils/core.py b/superset/utils/core.py index 84f025214ab65..789464f9a9298 100644 --- a/superset/utils/core.py +++ b/superset/utils/core.py @@ -560,9 +560,7 @@ def base_json_conv(obj: Any,) -> Any: # pylint: disable=inconsistent-return-sta return list(obj) if isinstance(obj, decimal.Decimal): return float(obj) - if isinstance(obj, uuid.UUID): - return str(obj) - if isinstance(obj, time): + if isinstance(obj, (uuid.UUID, time, LazyString)): return str(obj) if isinstance(obj, timedelta): return format_timedelta(obj) @@ -571,8 +569,6 @@ def base_json_conv(obj: Any,) -> Any: # pylint: disable=inconsistent-return-sta return obj.decode("utf-8") except Exception: # pylint: disable=broad-except return "[bytes]" - if isinstance(obj, LazyString): - return str(obj) def json_iso_dttm_ser(obj: Any, pessimistic: bool = False) -> str: