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 2 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
20 changes: 20 additions & 0 deletions tests/test_ge_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
convert_ndarray_datetime_to_float_dtype_utc_timezone,
convert_ndarray_float_to_datetime_tuple,
convert_ndarray_to_datetime_dtype_best_effort,
convert_to_json_serializable,
deep_filter_properties_iterable,
ensure_json_serializable,
filter_properties_dict,
hyphen,
is_ndarray_datetime_dtype,
Expand Down Expand Up @@ -557,3 +559,21 @@ def test_convert_ndarray_float_to_datetime_tuple(
def test_hyphen():
txt: str = "validation_result"
assert hyphen(txt=txt) == "validation-result"


@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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests look good, but they should probably live in tests.test_convert_to_json_serializable, unless there's a compelling reason to break the pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll move. I didn't notice that file assuming we were following the pattern of test file per module

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that would be super reasonable - at some point we could refactor this to be more logical. thanks for addressing!

Loading