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

Stop serializing timezone-naive datetime as a timezone-aware datetime with UTC tz #36379

Merged
merged 1 commit into from
Dec 25, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions airflow/serialization/serializers/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
serialize as serialize_timezone,
)
from airflow.utils.module_loading import qualname
from airflow.utils.timezone import convert_to_utc, is_naive

if TYPE_CHECKING:
import datetime
Expand All @@ -45,10 +44,8 @@ def serialize(o: object) -> tuple[U, str, int, bool]:

if isinstance(o, datetime):
qn = qualname(o)
if is_naive(o):
o = convert_to_utc(o)

tz = serialize_timezone(o.tzinfo)
tz = serialize_timezone(o.tzinfo) if o.tzinfo else None

return {TIMESTAMP: o.timestamp(), TIMEZONE: tz}, qn, __version__, True

Expand Down Expand Up @@ -83,7 +80,11 @@ def deserialize(classname: str, version: int, data: dict | str) -> datetime.date
else:
tz = timezone(data[TIMEZONE])
else:
tz = deserialize_timezone(data[TIMEZONE][1], data[TIMEZONE][2], data[TIMEZONE][0])
tz = (
deserialize_timezone(data[TIMEZONE][1], data[TIMEZONE][2], data[TIMEZONE][0])
if data[TIMEZONE]
else None
)

if classname == qualname(datetime.datetime) and isinstance(data, dict):
return datetime.datetime.fromtimestamp(float(data[TIMESTAMP]), tz=tz)
Expand Down
5 changes: 5 additions & 0 deletions tests/serialization/serializers/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ def test_datetime(self):
d = deserialize(s)
assert i.timestamp() == d.timestamp()

i = datetime.datetime.now()
s = serialize(i)
d = deserialize(s)
assert i.timestamp() == d.timestamp()

def test_deserialize_datetime_v1(self):
s = {
"__classname__": "pendulum.datetime.DateTime",
Expand Down