danielgtaylor / danielgtaylor/python-betterproto

Struct cannot be serialized anymore

オープン
#599 コメント 6 件 リアクション 1 件 担当者 0 名 GitHub で見る
bug investigation needed
主要言語
Python
スター
1.8k
フォーク
234
PR マージ指標
30日以内にマージされた PR はありません

説明

### 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

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

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

Reproduce the failure with Struct().from_dict({'a':'a'}).SerializeToString(), then inspect Message.dump, _serialize_single, and _preprocess_single in betterproto/__init__.py. Trace how Struct map values are converted and add a regression test for this example. Done means serialization returns bytes without raising TypeError.

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
api
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。