From 9e01e2e543fef22fef5a3eb16b057cdec9779dd6 Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Tue, 1 Oct 2024 16:35:29 -0700 Subject: [PATCH 1/4] 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 --- google/api_core/protobuf_helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google/api_core/protobuf_helpers.py b/google/api_core/protobuf_helpers.py index d777c5f89..b8738e5fb 100644 --- a/google/api_core/protobuf_helpers.py +++ b/google/api_core/protobuf_helpers.py @@ -63,8 +63,8 @@ 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__ + "Could not convert\n Any[{}] to\n {}".format( + any_pb.TypeName(), msg_pb.DESCRIPTOR.full_name ) ) From 7f9a7b97527f104853df8c1c01d8f20f36b7a762 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 9 Oct 2024 16:57:57 +0000 Subject: [PATCH 2/4] update test --- google/api_core/protobuf_helpers.py | 4 +--- tests/unit/test_protobuf_helpers.py | 8 +++++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/google/api_core/protobuf_helpers.py b/google/api_core/protobuf_helpers.py index b8738e5fb..3adaf02c0 100644 --- a/google/api_core/protobuf_helpers.py +++ b/google/api_core/protobuf_helpers.py @@ -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\n Any[{}] to\n {}".format( - any_pb.TypeName(), msg_pb.DESCRIPTOR.full_name - ) + f"Could not convert Any[{any_pb.TypeName()}] to {msg_pb.DESCRIPTOR.full_name}" ) # Done; return the message. diff --git a/tests/unit/test_protobuf_helpers.py b/tests/unit/test_protobuf_helpers.py index 5b2c6dfdb..b7568353a 100644 --- a/tests/unit/test_protobuf_helpers.py +++ b/tests/unit/test_protobuf_helpers.py @@ -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 @@ -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 Any[google.type.Date] to google.type.TimeOfDay" + ), + ): protobuf_helpers.from_any_pb(timeofday_pb2.TimeOfDay, in_message) From 7bce39dbfc8921b442d5c4dabfffea1faacbc796 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 9 Oct 2024 17:30:37 +0000 Subject: [PATCH 3/4] update error message --- google/api_core/protobuf_helpers.py | 2 +- tests/unit/test_protobuf_helpers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/google/api_core/protobuf_helpers.py b/google/api_core/protobuf_helpers.py index 3adaf02c0..680c71205 100644 --- a/google/api_core/protobuf_helpers.py +++ b/google/api_core/protobuf_helpers.py @@ -63,7 +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( - f"Could not convert Any[{any_pb.TypeName()}] to {msg_pb.DESCRIPTOR.full_name}" + f"Could not convert `google.protobuf.any_pb2.Any` with underlying type `{any_pb.TypeName()}` to `{msg_pb.DESCRIPTOR.full_name}`" ) # Done; return the message. diff --git a/tests/unit/test_protobuf_helpers.py b/tests/unit/test_protobuf_helpers.py index b7568353a..9dae90743 100644 --- a/tests/unit/test_protobuf_helpers.py +++ b/tests/unit/test_protobuf_helpers.py @@ -69,7 +69,7 @@ def test_from_any_pb_failure(): with pytest.raises( TypeError, match=re.escape( - "Could not convert Any[google.type.Date] to google.type.TimeOfDay" + "Could not convert `google.protobuf.any_pb2.Any` with underlying type `google.type.Date` to `google.type.TimeOfDay`" ), ): protobuf_helpers.from_any_pb(timeofday_pb2.TimeOfDay, in_message) From dc338495299fee8be4bf39b858015bcc0c7cd181 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Tue, 22 Oct 2024 14:19:59 +0000 Subject: [PATCH 4/4] address review feedback --- google/api_core/protobuf_helpers.py | 2 +- tests/unit/test_protobuf_helpers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/google/api_core/protobuf_helpers.py b/google/api_core/protobuf_helpers.py index 680c71205..30cd7c859 100644 --- a/google/api_core/protobuf_helpers.py +++ b/google/api_core/protobuf_helpers.py @@ -63,7 +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( - f"Could not convert `google.protobuf.any_pb2.Any` with underlying type `{any_pb.TypeName()}` to `{msg_pb.DESCRIPTOR.full_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. diff --git a/tests/unit/test_protobuf_helpers.py b/tests/unit/test_protobuf_helpers.py index 9dae90743..5678d3bc5 100644 --- a/tests/unit/test_protobuf_helpers.py +++ b/tests/unit/test_protobuf_helpers.py @@ -69,7 +69,7 @@ def test_from_any_pb_failure(): with pytest.raises( TypeError, match=re.escape( - "Could not convert `google.protobuf.any_pb2.Any` with underlying type `google.type.Date` to `google.type.TimeOfDay`" + "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)