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

refactor: Explicitly use T iso date separator #2090

Merged
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
19 changes: 14 additions & 5 deletions singer_sdk/_singerlib/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
import sys
import typing as t
from dataclasses import asdict, dataclass, field
from datetime import timezone
from datetime import datetime, timezone

import simplejson as json
from dateutil.parser import parse

if t.TYPE_CHECKING:
from datetime import datetime


class SingerMessageType(str, enum.Enum):
"""Singer specification message types."""
Expand All @@ -25,6 +22,18 @@ class SingerMessageType(str, enum.Enum):
BATCH = "BATCH"


def _default_encoding(obj: t.Any) -> str: # noqa: ANN401
"""Default JSON encoder.

Args:
obj: The object to encode.

Returns:
The encoded object.
"""
return obj.isoformat(sep="T") if isinstance(obj, datetime) else str(obj)


def exclude_null_dict(pairs: list[tuple[str, t.Any]]) -> dict[str, t.Any]:
"""Exclude null values from a dictionary.

Expand Down Expand Up @@ -211,7 +220,7 @@ def format_message(message: Message) -> str:
Returns:
The formatted message.
"""
return json.dumps(message.to_dict(), use_decimal=True, default=str)
return json.dumps(message.to_dict(), use_decimal=True, default=_default_encoding)


def write_message(message: Message) -> None:
Expand Down
2 changes: 1 addition & 1 deletion singer_sdk/helpers/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def to_json_compatible(val: t.Any) -> t.Any: # noqa: ANN401
If given a naive datetime object, pendulum automatically makes it utc
"""
if isinstance(val, (datetime.datetime, pendulum.DateTime)):
return pendulum.instance(val).isoformat()
return pendulum.instance(val).isoformat("T")
return val


Expand Down