Shorter optional fields
まだ誰も着手していません。
評価
調査の方向性
proto3 の optional フィールド用の Python フィールドを生成するジェネレーターの経路を追跡することから始めてください。この issue ではリポジトリのファイルやテストは指定されていません。参照されている 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 ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
danielgtaylor/python-betterproto のほかの issue
-
難易度 3/5 1〜2日 初心者へのやさしさ 55/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 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
-
follow-up
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
Download helper still uses huggingface_hub arguments removed in 1.0: USE_SYMLINKS is silently ignored and every download ends with a WinError 3 line対応中かも @li-lizhe が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
IPv6 node addresses produce an invalid request URL対応中かも @RaphaelFakhri が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
typesense/typesense-python#154 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
elastic/elasticsearch-py#3592 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
developmentseed/titiler-cmr#250 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信