Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Zeroes are not serialized with bytes() in proto2 syntax with required field

オープン
#676 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

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

調査の方向性

まず、生成された MyMessage と MyEnum.FOOBAR の例で問題を再現し、その後、必要な proto2 enum について bytes()、to_json()、to_dict() を比較します。include_default_values=True を要求しなくても、バイナリ表現にゼロ値の enum が含まれれば完了です。

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

説明

bug investigation needed
Summary

There should be an alternative bytes method that allows to serialize zeroes for proto2 syntax. Currently zeroes enums are skipped as they are "default" values which is incorrect because unless the field is optional, it must be present in encoded form.

Reproduction Steps
  1. Create a simple proto2 file:
message MyMessage {
    enum MyEnum {
        FOOBAR = 0;
        BAZ = 1;
    }
}
  1. Compile to python:
@dataclass
class MyMessage(betterproto.Message):
	# @required
	action: "MyEnum" = betterproto.enum_field(1)

class MyEnum(betterproto.Enum):
	FOOBAR = 0
	BAZ = 1
  1. Use the FOOBAR value (0):
print(
	MyMessage(
		action=MyEnum.FOOBAR,
	)
)

You can see the MyEnum.FOOBAR here. However when you try to serialize it, the encoder skips this value incorrectly thinking it's default and hence shouldn't be added:

print(
	MyMessage(
		action=MyEnum.FOOBAR,
	).to_json()
)
# or
print(
	MyMessage(
		action=MyEnum.FOOBAR,
	).to_dict()
)
# or
print(
	bytes(MyMessage(
		action=MyEnum.FOOBAR,
	))
)

This can only be "fixed" by passing include_default_values=True in to_json or to_dict:

print(
	MyMessage(
		action=MyEnum.FOOBAR,
	).to_json(include_default_values=True)
)

But I need binary representation of the MyMessage class

Expected Results

N/A

Actual Results

N/A

System Information

Latest beta version

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.
主要言語
Python
スター
1.8k
フォーク
235
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

danielgtaylor/python-betterproto のほかの issue

danielgtaylor/python-betterproto の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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