Zeroes are not serialized with bytes() in proto2 syntax with required field
还没有人认领这个 Issue。
评估
调研方向
先使用生成的 MyMessage 和 MyEnum.FOOBAR 示例重现该问题,然后比较所需 proto2 枚举的 bytes()、to_json() 和 to_dict()。当二进制表示包含零值枚举且不需要 include_default_values=True 时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
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
- Create a simple proto2 file:
message MyMessage {
enum MyEnum {
FOOBAR = 0;
BAZ = 1;
}
}
- Compile to python:
@dataclass
class MyMessage(betterproto.Message):
# @required
action: "MyEnum" = betterproto.enum_field(1)
class MyEnum(betterproto.Enum):
FOOBAR = 0
BAZ = 1
- Use the
FOOBARvalue (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
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
danielgtaylor/python-betterproto 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 55/100
-
难度 5/5 一周以上 新手友好度 15/100
-
bug investigation needed
难度 3/5 1-2 天 新手友好度 42/100
-
bug investigation needed
难度 3/5 1-2 天 新手友好度 45/100
-
难度 5/5 一周以上 新手友好度 10/100
danielgtaylor/python-betterproto#673 · 1 条评论 ·
查看 danielgtaylor/python-betterproto 的全部 Issue
相似的 Issue
-
Uninstall guide omits the CloudWatch log groups AWS CodeBuild creates for useCodeBuild pipelines未关闭bug documentation
难度 1/5 1-3 小时 新手友好度 88/100
awslabs/visual-asset-management-system#552 ·
维护者通常 1 天内回复
-
netbox status: needs triage type: documentation
难度 1/5 1 小时以内 新手友好度 76/100
netbox-community/netbox#23460 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 75/100
awslabs/threat-composer#362 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
ansys/pyansys-common-mcp#213 ·
维护者通常 3 天内回复