Python: Handle ros2 uint8[] field as str without ValueError #975
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Public-Facing Changes
None
Description
When calling the
write_message
method, the message argument often originates from some other serialized form, likeProtobuf
orJSON
. However, bytes fields can become mismatched during these serialization processes. For instance, even though a Protobuf message may contain bytes fields, usingMessageToDict
to convert it to a Python dictionary will base64 encode those bytes, as it adheres to the Proto3 JSON mapping guidelines.This leads to a confusing situation for the user. They might assume that the field is still in
bytes
and defined asuint8[]
in the ROS2 message. But when invokingwrite_message
, they encounter the following cryptic error:While the error is technically correct, it's confusing for users who are not aware of the limitations of JSON or the transformations performed by
MessageToDict
.This pull request aims to alleviate this issue by adding an additional check to see if the problematic field is base64-encoded. If it is, we decode it and proceed without errors, as demonstrated in
test_write_bytes_object_from_json_deserialization
. If decoding fails, the function will still raise the original error.