Skip to content

Commit 0d5ed37

Browse files
MarkDaoustparthea
andauthoredOct 22, 2024··
fix: Improve Any decode error (#712)
* 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>
1 parent 4dbb405 commit 0d5ed37

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed
 

‎google/api_core/protobuf_helpers.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ def from_any_pb(pb_type, any_pb):
6363
# Unpack the Any object and populate the protobuf message instance.
6464
if not any_pb.Unpack(msg_pb):
6565
raise TypeError(
66-
"Could not convert {} to {}".format(
67-
any_pb.__class__.__name__, pb_type.__name__
68-
)
66+
f"Could not convert `{any_pb.TypeName()}` with underlying type `google.protobuf.any_pb2.Any` to `{msg_pb.DESCRIPTOR.full_name}`"
6967
)
7068

7169
# Done; return the message.

‎tests/unit/test_protobuf_helpers.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import pytest
16+
import re
1617

1718
from google.api import http_pb2
1819
from google.api_core import protobuf_helpers
@@ -65,7 +66,12 @@ def test_from_any_pb_failure():
6566
in_message = any_pb2.Any()
6667
in_message.Pack(date_pb2.Date(year=1990))
6768

68-
with pytest.raises(TypeError):
69+
with pytest.raises(
70+
TypeError,
71+
match=re.escape(
72+
"Could not convert `google.type.Date` with underlying type `google.protobuf.any_pb2.Any` to `google.type.TimeOfDay`"
73+
),
74+
):
6975
protobuf_helpers.from_any_pb(timeofday_pb2.TimeOfDay, in_message)
7076

7177

0 commit comments

Comments
 (0)
Please sign in to comment.