From 0d77f36679a5714ea7402085988d4fb9867dd913 Mon Sep 17 00:00:00 2001 From: Francesco Frassinelli Date: Thu, 11 Nov 2021 09:35:00 +0100 Subject: [PATCH] fix: handle TIME column serialization (#16869) * Add support for datetime.time in json_int_dttm_ser * Test base_json_conv support for datetime.time * Group types by conversion function for JSON dump --- superset/utils/core.py | 6 ++---- tests/integration_tests/utils_tests.py | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/superset/utils/core.py b/superset/utils/core.py index fad19f4b7ebbd..4013a65e3d646 100644 --- a/superset/utils/core.py +++ b/superset/utils/core.py @@ -563,7 +563,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): + if isinstance(obj, (uuid.UUID, time, LazyString)): return str(obj) if isinstance(obj, timedelta): return format_timedelta(obj) @@ -572,8 +572,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: @@ -587,7 +585,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: 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):