Skip to content

Commit

Permalink
fix: Improve Any decode error (#712)
Browse files Browse the repository at this point in the history
* Improve Any decoding-error.

Was:

TypeError: Could not convert Any to PredictLongRunningResponse

Now:

TypeError: Could not convert
   Any[google.ai.generativelanguage.v1main.PredictLongRunningResponse] to
       google.ai.generativelanguage.v1beta.PredictLongRunningResponse

* update test

* update error message

* address review feedback

---------

Co-authored-by: Anthonios Partheniou <partheniou@google.com>
  • Loading branch information
MarkDaoust and parthea authored Oct 22, 2024
1 parent 4dbb405 commit 0d5ed37
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 1 addition & 3 deletions google/api_core/protobuf_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ def from_any_pb(pb_type, any_pb):
# Unpack the Any object and populate the protobuf message instance.
if not any_pb.Unpack(msg_pb):
raise TypeError(
"Could not convert {} to {}".format(
any_pb.__class__.__name__, pb_type.__name__
)
f"Could not convert `{any_pb.TypeName()}` with underlying type `google.protobuf.any_pb2.Any` to `{msg_pb.DESCRIPTOR.full_name}`"
)

# Done; return the message.
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/test_protobuf_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import pytest
import re

from google.api import http_pb2
from google.api_core import protobuf_helpers
Expand Down Expand Up @@ -65,7 +66,12 @@ def test_from_any_pb_failure():
in_message = any_pb2.Any()
in_message.Pack(date_pb2.Date(year=1990))

with pytest.raises(TypeError):
with pytest.raises(
TypeError,
match=re.escape(
"Could not convert `google.type.Date` with underlying type `google.protobuf.any_pb2.Any` to `google.type.TimeOfDay`"
),
):
protobuf_helpers.from_any_pb(timeofday_pb2.TimeOfDay, in_message)


Expand Down

0 comments on commit 0d5ed37

Please sign in to comment.