Skip to content

Commit

Permalink
[BUGFIX] Ensure datetime.time can be serialized to JSON (#10795)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
wookasz and pre-commit-ci[bot] authored Dec 19, 2024
1 parent ab0a7f7 commit 29a4dba
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions great_expectations/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ def convert_to_json_serializable( # noqa: C901, PLR0911, PLR0912
if isinstance(data, np.float64):
return float(data)

if isinstance(data, (datetime.datetime, datetime.date)):
if isinstance(data, (datetime.datetime, datetime.date, datetime.time)):
return data.isoformat()

if isinstance(data, (np.datetime64)):
Expand Down Expand Up @@ -1309,7 +1309,7 @@ def ensure_json_serializable(data: Any) -> None: # noqa: C901, PLR0911, PLR0912
_ = [ensure_json_serializable(x) for x in data.tolist()] # type: ignore[func-returns-value]
return

if isinstance(data, (datetime.datetime, datetime.date)):
if isinstance(data, (datetime.datetime, datetime.date, datetime.time)):
return

if isinstance(data, pathlib.PurePath):
Expand Down
21 changes: 20 additions & 1 deletion tests/test_convert_to_json_serializable.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import datetime
import re

import numpy as np
import pytest

from great_expectations.util import convert_to_json_serializable
from great_expectations.util import convert_to_json_serializable, ensure_json_serializable

try:
from shapely.geometry import LineString, MultiPolygon, Point, Polygon
Expand Down Expand Up @@ -63,3 +64,21 @@ def test_serialization_of_pattern():
pattern_to_test = r"data_(?P<year>\d{4})-(?P<month>\d{2}).csv"
data = re.compile(pattern_to_test)
assert convert_to_json_serializable(data) == pattern_to_test


@pytest.mark.unit
@pytest.mark.parametrize(
"data", [pytest.param({"t": datetime.time(hour=1, minute=30, second=45)}, id="datetime.time")]
)
def test_convert_to_json_serializable_converts_correctly(data: dict):
ret = convert_to_json_serializable(data)
assert ret == {"t": "01:30:45"}


@pytest.mark.unit
@pytest.mark.parametrize(
"data", [pytest.param({"t": datetime.time(hour=1, minute=30, second=45)}, id="datetime.time")]
)
def test_ensure_json_serializable(data: dict):
ensure_json_serializable(data)
# Passes if no exception raised

0 comments on commit 29a4dba

Please sign in to comment.