Shorter optional fields
还没有人认领这个 Issue。
评估
调研方向
首先跟踪为 proto3 optional 字段生成 Python 字段的生成器路径;该 issue 没有指定 repository 文件或测试。查看所引用的 cattrs/converters.py 示例,并在选择方案之前比较 Ideas A、B 和 C。完成的标准是:optional 字段生成更短的代码,同时保留正确的 Optional 类型和 presence tracking。
由索引模型根据 Issue 内容生成。
描述
Optional fields generate very long and repetitive code.
The idea is to use shorter and simpler code, and internally consider it as a field having optional=True, group=f"_{field_name}".
Related to #459.
Proto file
syntax = "proto3";
message Foo {
optional uint32 bar = 1;
};
Currently generated code
from dataclasses import dataclass
import betterproto
@dataclass(eq=False, repr=False)
class Foo(betterproto.Message):
bar: Optional[int] = betterproto.uint32_field(
1, optional=True, group="_bar"
)
Idea A
Read the type-hint of the field, and if it's a union of a type with None, consider it an optional field.
This might be a bad idea if unions of a type + None are used for something other than optional fields.
An example implementation of detecting optional fields can be found here: cattrs/converters.py.
from dataclasses import dataclass
from typing import Union, Optional
import betterproto
@dataclass(eq=False, repr=False)
class Foo(betterproto.Message):
bar: Optional[int] = betterproto.uint32_field(1)
Idea B
Use a custom type-hint that indicates a field is optional, and is correctly understood by type-checkers as Optional.
from dataclasses import dataclass
import betterproto
@dataclass(eq=False, repr=False)
class Foo(betterproto.Message):
bar: betterproto.OptionalField[int] = betterproto.uint32_field(1)
It could possibly be implemented something like this:
Annotated
from typing import Optional, TypeVar
from typing_extensions import Annotated
T = TypeVar("T")
optional_metadata = object() # or another sentinel
OptionalField = Annotated[Optional[T], optional_metadata]
Idea C
Automatically add group=f"_{field_name}" if optional=True and no group specified.
from dataclasses import dataclass
import betterproto
@dataclass(eq=False, repr=False)
class Foo(betterproto.Message):
bar: Optional[int] = betterproto.uint32_field(1, optional=True)
- 主要语言
- 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
-
bug investigation needed
难度 4/5 3-5 天 新手友好度 38/100
danielgtaylor/python-betterproto#676 · 3 条评论 ·
查看 danielgtaylor/python-betterproto 的全部 Issue
相似的 Issue
-
难度 1/5 1-3 小时 新手友好度 88/100
fossasia/visdom-dev#69 ·
维护者通常 3 天内回复
-
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
BUG: PROD: v1.3.0-beta Onboarding Profile Picture Misaligns When Name Validation Error Appears可能已有人在做 @RitikaSen27 今天认领。 未关闭bug good first issue
难度 2/5 1-3 小时 新手友好度 82/100
AOSSIE-Org/PictoPy#1647 · 2 条评论 ·
维护者通常 2 天内回复
-
[oblt-aw][autodoc] Update workflow docs for gh-aw v0.92.5 and detector permission scope可能已有人在做 @github-actions 今天认领。 未关闭
难度 1/5 1-3 小时 新手友好度 88/100
维护者通常 1 天内回复