Skip to content

Commit 8396d8b

Browse files
committed
fix(proto-plus): include the invalid value in __setattr__ TypeError messages
1 parent 5a88a0e commit 8396d8b

1 file changed

Lines changed: 35 additions & 32 deletions

File tree

packages/proto-plus/proto/message.py

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -659,34 +659,35 @@ def __init__(
659659
mapping,
660660
)
661661
)
662-
663662
params = {}
664663
# Update the mapping to address any values that need to be
665664
# coerced.
666665
marshal = self._meta.marshal
667-
for key, value in mapping.items():
668-
(key, pb_type) = self._get_pb_type_from_key(key)
669-
if pb_type is None:
670-
if ignore_unknown_fields:
671-
continue
672-
673-
raise ValueError(
674-
"Unknown field for {}: {}".format(self.__class__.__name__, key)
675-
)
666+
try:
667+
for key, value in mapping.items():
668+
(key, pb_type) = self._get_pb_type_from_key(key)
669+
if pb_type is None:
670+
if ignore_unknown_fields:
671+
continue
672+
673+
raise ValueError(
674+
"Unknown field for {}: {}".format(self.__class__.__name__, key)
675+
)
676676

677-
pb_value = marshal.to_proto(pb_type, value)
677+
pb_value = marshal.to_proto(pb_type, value)
678678

679-
if pb_value is not None:
680-
params[key] = pb_value
679+
if pb_value is not None:
680+
params[key] = pb_value
681681

682-
# Create the internal protocol buffer.
683-
try:
682+
# Create the internal protocol buffer.
684683
super().__setattr__("_pb", self._meta.pb(**params))
685-
except TypeError as ex:
684+
except TypeError as e:
686685
raise TypeError(
687-
f"Failed to initialize {self.__class__.__name__} due to invalid type "
688-
f"or structure in parameters. Underlying error: {ex}"
689-
) from ex
686+
"Failed to initialize {}. Underlying error: {}".format(
687+
self.__class__.__name__, e
688+
)
689+
) from e
690+
690691

691692
def _get_pb_type_from_key(self, key):
692693
"""Given a key, return the corresponding pb_type.
@@ -858,22 +859,24 @@ def __setattr__(self, key, value):
858859
"Unknown field for {}: {}".format(self.__class__.__name__, key)
859860
)
860861

861-
pb_value = marshal.to_proto(pb_type, value)
862+
try:
863+
pb_value = marshal.to_proto(pb_type, value)
862864

863-
# Clear the existing field.
864-
# This is the only way to successfully write nested falsy values,
865-
# because otherwise MergeFrom will no-op on them.
866-
self._pb.ClearField(key)
865+
# Clear the existing field.
866+
# This is the only way to successfully write nested falsy values,
867+
# because otherwise MergeFrom will no-op on them.
868+
self._pb.ClearField(key)
867869

868-
# Merge in the value being set.
869-
if pb_value is not None:
870-
try:
870+
# Merge in the value being set.
871+
if pb_value is not None:
871872
self._pb.MergeFrom(self._meta.pb(**{key: pb_value}))
872-
except TypeError as ex:
873-
raise TypeError(
874-
f"Failed to set field '{key}' on {self.__class__.__name__} with value {value!r}. "
875-
f"Underlying error: {ex}"
876-
) from ex
873+
except TypeError as e:
874+
raise TypeError(
875+
"Failed to set field '{}' on {} to value {}. Underlying error: {}".format(
876+
key, self.__class__.__name__, value, e
877+
)
878+
) from e
879+
877880

878881

879882
def __getstate__(self):

0 commit comments

Comments
 (0)