Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 23, 2026. It is now read-only.
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
7 changes: 4 additions & 3 deletions proto/marshal/rules/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ def to_proto(self, value):
try:
# Try the fast path first.
return self._descriptor(**value)
except (TypeError, ValueError) as ex:
# If we have a TypeError or Valueerror,
except (TypeError, ValueError, AttributeError) as ex:
# If we have a TypeError, ValueError or AttributeError,
# try the slow path in case the error
# was:
# - an int64/string issue.
# - a missing key issue in case a key only exists with a `_` suffix.
# See related issue: https://github.com/googleapis/python-api-core/issues/227.
# - a missing key issue due to nested struct. See: b/321905145.
# - a missing key issue due to nested struct. See: https://github.com/googleapis/proto-plus-python/issues/424.
# - a missing key issue due to nested duration. See: https://github.com/googleapis/google-cloud-python/issues/13350.
return self._wrapper(value)._pb
return value

Expand Down
20 changes: 20 additions & 0 deletions tests/test_marshal_types_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,26 @@ class Foo(proto.Message):
assert Foo.pb(foo).ttl.seconds == 120


def test_duration_write_string_nested():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: for clarity, I suggest:
s/another_field/foo_field/g
s/some_field/bar_field/g

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in fcb5bb2

class Foo(proto.Message):
foo_field: duration_pb2.Duration = proto.Field(
proto.MESSAGE,
number=1,
message=duration_pb2.Duration,
)

Foo(foo_field="300s")

class Bar(proto.Message):
bar_field = proto.Field(proto.MESSAGE, number=1, message=Foo)

bar = Bar({"bar_field": {"foo_field": "300s"}})
assert isinstance(bar.bar_field.foo_field, timedelta)
assert isinstance(Bar.pb(bar).bar_field.foo_field, duration_pb2.Duration)
assert bar.bar_field.foo_field.seconds == 300
assert Bar.pb(bar).bar_field.foo_field.seconds == 300


def test_duration_del():
class Foo(proto.Message):
ttl = proto.Field(
Expand Down
Loading