Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Ensure datetime.time can be serialized to JSON #10795

Merged
merged 7 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading