File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -34,14 +34,15 @@ def to_proto(self, value):
34
34
try :
35
35
# Try the fast path first.
36
36
return self ._descriptor (** value )
37
- except (TypeError , ValueError ) as ex :
38
- # If we have a TypeError or Valueerror ,
37
+ except (TypeError , ValueError , AttributeError ) as ex :
38
+ # If we have a TypeError, Valueerror or AttributeError ,
39
39
# try the slow path in case the error
40
40
# was:
41
41
# - an int64/string issue.
42
42
# - a missing key issue in case a key only exists with a `_` suffix.
43
43
# See related issue: https://github.com/googleapis/python-api-core/issues/227.
44
44
# - a missing key issue due to nested struct. See: b/321905145.
45
+ # - a missing key issue due to nested duration. See: b/383651746.
45
46
return self ._wrapper (value )._pb
46
47
return value
47
48
Original file line number Diff line number Diff line change @@ -241,6 +241,26 @@ class Foo(proto.Message):
241
241
assert Foo .pb (foo ).ttl .seconds == 120
242
242
243
243
244
+ def test_duration_write_string_nested ():
245
+ class Foo (proto .Message ):
246
+ another_field : duration_pb2 .Duration = proto .Field (
247
+ proto .MESSAGE ,
248
+ number = 1 ,
249
+ message = duration_pb2 .Duration ,
250
+ )
251
+
252
+ Foo (another_field = "300s" )
253
+
254
+ class Bar (proto .Message ):
255
+ some_field = proto .Field (proto .MESSAGE , number = 1 , message = Foo )
256
+
257
+ bar = Bar ({"some_field" :{"another_field" :"300s" }})
258
+ assert isinstance (bar .some_field .another_field , timedelta )
259
+ assert isinstance (Bar .pb (bar ).some_field .another_field , duration_pb2 .Duration )
260
+ assert bar .some_field .another_field .seconds == 300
261
+ assert Bar .pb (bar ).some_field .another_field .seconds == 300
262
+
263
+
244
264
def test_duration_del ():
245
265
class Foo (proto .Message ):
246
266
ttl = proto .Field (
You can’t perform that action at this time.
0 commit comments