Skip to content

Struct cannot be serialized anymore #599

Open
@leonardgerardatomicmachinescom

Description

Summary

Struct().from_dict({'a':'a'}).SerializeToString() raises TypeError: string argument without an encoding

Reproduction Steps

from betterproto.lib.google.protobuf import Struct
Struct().from_dict({'a':'a'}).SerializeToString()

Expected Results

no exception raised and proper serialization bytes returned.

Actual Results

raises TypeError: string argument without an encoding with traceback:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[10], line 1
----> 1 Struct().from_dict({'a':'a'}).SerializeToString()

File [...]/.venv/lib/python3.12/site-packages/betterproto/__init__.py:1138, in Message.SerializeToString(self)
   1125 def SerializeToString(self: T) -> bytes:
   1126     """
   1127     Get the binary encoded Protobuf representation of this message instance.
   1128 
   (...)
   1136         The binary encoded Protobuf representation of this message instance
   1137     """
-> 1138     return bytes(self)

File [...]/.venv/lib/python3.12/site-packages/betterproto/__init__.py:1026, in Message.__bytes__(self)
   1022 """
   1023 Get the binary encoded Protobuf representation of this message instance.
   1024 """
   1025 with BytesIO() as stream:
-> 1026     self.dump(stream)
   1027     return stream.getvalue()

File [...]/.venv/lib/python3.12/site-packages/betterproto/__init__.py:993, in Message.dump(self, stream, delimit)
    991         assert meta.map_types
    992         sk = _serialize_single(1, meta.map_types[0], k)
--> 993         sv = _serialize_single(2, meta.map_types[1], v)
    994         stream.write(
    995             _serialize_single(meta.number, meta.proto_type, sk + sv)
    996         )
    997 else:
    998     # If we have an empty string and we're including the default value for
    999     # a oneof, make sure we serialize it. This ensures that the byte string
   1000     # output isn't simply an empty string. This also ensures that round trip
   1001     # serialization will keep `which_one_of` calls consistent.

File [...]/.venv/lib/python3.12/site-packages/betterproto/__init__.py:464, in _serialize_single(field_number, proto_type, value, serialize_empty, wraps)
    455 def _serialize_single(
    456     field_number: int,
    457     proto_type: str,
   (...)
    461     wraps: str = "",
    462 ) -> bytes:
    463     """Serializes a single field and value."""
--> 464     value = _preprocess_single(proto_type, wraps, value)
    466     output = bytearray()
    467     if proto_type in WIRE_VARINT_TYPES:

File [...]/.venv/lib/python3.12/site-packages/betterproto/__init__.py:415, in _preprocess_single(proto_type, wraps, value)
    412             return b""
    413         value = _get_wrapper(wraps)(value=value)
--> 415     return bytes(value)
    417 return value

TypeError: string argument without an encoding

System Information

$ protoc --version; python --version; pip show betterproto
libprotoc 27.1
Python 3.12.4
Name: betterproto
Version: 2.0.0b7
Summary: A better Protobuf / gRPC generator & library
Home-page: https://github.com/danielgtaylor/python-betterproto
Author: Daniel G. Taylor
Author-email: [email protected]
License: MIT
Location: [...]/.venv/lib/python3.12/site-packages
Requires: grpclib, python-dateutil, typing-extensions
Required-by: [...]

Checklist

  • I have searched the issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have verified this issue occurs on the latest prelease of betterproto which can be installed using pip install -U --pre betterproto, if possible.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions