Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struct cannot be serialized anymore #599

Open
3 tasks done
leonardgerardatomicmachinescom opened this issue Aug 17, 2024 · 2 comments
Open
3 tasks done

Struct cannot be serialized anymore #599

leonardgerardatomicmachinescom opened this issue Aug 17, 2024 · 2 comments
Labels
bug Something isn't working investigation needed

Comments

@leonardgerardatomicmachinescom

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: danielgtaylor@gmail.com
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.
@leonardgerardatomicmachinescom leonardgerardatomicmachinescom changed the title Struct cannot be serialized anymore! Struct cannot be serialized anymore Aug 17, 2024
@zacharya19
Copy link

Getting an issue with Struct Serialization as well, but from another place (seems like the Rust codec is the cause):

In [8]: s = Struct().from_dict({"a": 1})

In [9]: s.SerializeToString()
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[9], line 1
----> 1 s.SerializeToString()

File ~/.local/lib/python3.10/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 ~/.local/lib/python3.10/site-packages/betterproto/__init__.py:1890, in __bytes_patch(self)
   1889 def __bytes_patch(self) -> bytes:
-> 1890     return betterproto_rust_codec.serialize(self)

RuntimeError: Given object is not a valid betterproto message.

@zacharya19
Copy link

The issue @leonardgerardatomicmachinescom described is shown when Rust codec isn't installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working investigation needed
Projects
None yet
Development

No branches or pull requests

2 participants