From 95cd49068cf3ed433d247028ca5b56da78a71cc3 Mon Sep 17 00:00:00 2001 From: Xuxchloris <7482714452@qq.com> Date: Fri, 14 Aug 2026 19:31:09 +0000 Subject: [PATCH] fix(ws): drop deprecated datetime.utcfromtimestamp/utcnow in vendored well_known_types Python 3.12+ emits DeprecationWarning for datetime.utcfromtimestamp() / datetime.utcnow(); the vendored google.protobuf well_known_types.py still used both. Mirrors the upstream protobuf fix: - _EPOCH_DATETIME_NAIVE = datetime.datetime(1970, 1, 1) (identical value) - GetCurrentTime uses datetime.datetime.now(tz=datetime.timezone.utc) Fixes #145. Adds timestamp tests (GetCurrentTime under DeprecationWarning-as-error, naive/aware FromDatetime/ToDatetime roundtrips) under lark_oapi/ws/tests. --- .../protobuf/internal/well_known_types.py | 4 +-- .../tests/test_well_known_types_timestamp.py | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 lark_oapi/ws/tests/test_well_known_types_timestamp.py diff --git a/lark_oapi/ws/pb/google/protobuf/internal/well_known_types.py b/lark_oapi/ws/pb/google/protobuf/internal/well_known_types.py index 1ea4f88b5..b37b34c5f 100644 --- a/lark_oapi/ws/pb/google/protobuf/internal/well_known_types.py +++ b/lark_oapi/ws/pb/google/protobuf/internal/well_known_types.py @@ -88,7 +88,7 @@ def Is(self, descriptor): return '/' in self.type_url and self.TypeName() == descriptor.full_name -_EPOCH_DATETIME_NAIVE = datetime.datetime.utcfromtimestamp(0) +_EPOCH_DATETIME_NAIVE = datetime.datetime(1970, 1, 1) _EPOCH_DATETIME_AWARE = datetime.datetime.fromtimestamp( 0, tz=datetime.timezone.utc) @@ -192,7 +192,7 @@ def FromJsonString(self, value): def GetCurrentTime(self): """Get the current UTC into Timestamp.""" - self.FromDatetime(datetime.datetime.utcnow()) + self.FromDatetime(datetime.datetime.now(tz=datetime.timezone.utc)) def ToNanoseconds(self): """Converts Timestamp to nanoseconds since epoch.""" diff --git a/lark_oapi/ws/tests/test_well_known_types_timestamp.py b/lark_oapi/ws/tests/test_well_known_types_timestamp.py new file mode 100644 index 000000000..c1f5c5b74 --- /dev/null +++ b/lark_oapi/ws/tests/test_well_known_types_timestamp.py @@ -0,0 +1,32 @@ +import datetime +import time +import warnings + +from lark_oapi.ws.pb.google.protobuf import timestamp_pb2 + + +def test_timestamp_get_current_time_no_deprecation_warning(): + """GetCurrentTime must not emit DeprecationWarning (issue #145).""" + ts = timestamp_pb2.Timestamp() + with warnings.catch_warnings(): + warnings.simplefilter("error", DeprecationWarning) + ts.GetCurrentTime() + assert abs(ts.ToSeconds() - time.time()) < 5 + + +def test_timestamp_from_to_datetime_roundtrip_naive(): + ts = timestamp_pb2.Timestamp() + ts.FromDatetime(datetime.datetime(2024, 1, 2, 3, 4, 5, 123456)) + dt = ts.ToDatetime() + assert dt == datetime.datetime(2024, 1, 2, 3, 4, 5, 123456) + + +def test_timestamp_from_to_datetime_roundtrip_aware(): + ts = timestamp_pb2.Timestamp() + ts.FromDatetime( + datetime.datetime(2024, 1, 2, 3, 4, 5, 123456, tzinfo=datetime.timezone.utc) + ) + dt = ts.ToDatetime(tzinfo=datetime.timezone.utc) + assert dt == datetime.datetime( + 2024, 1, 2, 3, 4, 5, 123456, tzinfo=datetime.timezone.utc + )