Skip to content

Commit 974ef59

Browse files
anandoleecopybara-github
authored andcommitted
Add test when try to convert datetime to Duration, and try to convert timedelta to Timestamp. It raises AttributeError but will turn to TypeError in OSS 34.0 release.
PiperOrigin-RevId: 797464099
1 parent 0b1ec4a commit 974ef59

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

python/google/protobuf/internal/duration_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ def test_duration_add_annotation(self):
9393
# Duration + Duration
9494
self.assertEqual(dr + dr2, dr2 + dr)
9595

96+
def test_assign_datetime_to_duration(self):
97+
message = well_known_types_test_pb2.WKTMessage()
98+
with self.assertRaises((TypeError, AttributeError)):
99+
message.optional_duration = datetime.datetime.now()
100+
96101

97102
if __name__ == '__main__':
98103
unittest.main()

python/google/protobuf/internal/timestamp_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ def test_timestamp_add_annotation(self):
9090
# Timestamp + Duration and Duration + Timestamp
9191
self.assertEqual(ts + msg.optional_duration, msg.optional_duration + ts)
9292

93+
def test_assign_duration_to_timestamp(self):
94+
message = well_known_types_test_pb2.WKTMessage()
95+
with self.assertRaises((TypeError, AttributeError)):
96+
message.optional_timestamp = datetime.timedelta(microseconds=123)
97+
9398

9499
if __name__ == '__main__':
95100
unittest.main()

python/google/protobuf/internal/well_known_types_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,10 @@ def testInvalidTimestamp(self):
540540
self.assertRaisesRegex(ValueError, 'Timestamp is not valid',
541541
message.FromSeconds, -62135596801)
542542
msg = well_known_types_test_pb2.WKTMessage()
543-
with self.assertRaises(AttributeError):
543+
with self.assertRaises((TypeError, AttributeError)):
544544
msg.optional_timestamp = 1
545545

546-
with self.assertRaises(AttributeError):
546+
with self.assertRaises((TypeError, AttributeError)):
547547
msg2 = well_known_types_test_pb2.WKTMessage(optional_timestamp=1)
548548

549549
with self.assertRaises(TypeError):
@@ -606,10 +606,10 @@ def testInvalidDuration(self):
606606
message.ToJsonString,
607607
)
608608
msg = well_known_types_test_pb2.WKTMessage()
609-
with self.assertRaises(AttributeError):
609+
with self.assertRaises((TypeError, AttributeError)):
610610
msg.optional_duration = 1
611611

612-
with self.assertRaises(AttributeError):
612+
with self.assertRaises((TypeError, AttributeError)):
613613
msg2 = well_known_types_test_pb2.WKTMessage(optional_duration=1)
614614

615615
with self.assertRaises(TypeError):

0 commit comments

Comments
 (0)