8000 fix: construct messages with nested duration in protobuf 5.28+ by parthea · Pull Request #519 · googleapis/proto-plus-python · GitHub
[go: up one dir, main page]

Skip to content

fix: construct messages with nested duration in protobuf 5.28+ #519

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

Merged
merged 4 commits into from
Jan 21, 2025
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
0