Generate additional Type Hints for the Dictionary Form of Messages & Enums
还没有人认领这个 Issue。
评估
调研方向
首先跟踪生成的 dataclass 消息以及现有的 from_dict() 和 to_dict() 入口点;该 issue 没有指明任何仓库文件或测试。比较所请求的 DocumentTypeStringified 和 DocumentDict 示例,解决 bytes 和命名方面的边界情况,并验证生成的类型提示是否支持字典转换和验证用例。
由索引模型根据 Issue 内容生成。
描述
Summary
I frequently use the dictionary form of betterproto messages (from_dict(), to_dict()) with stringified enums as arguments for web endpoints and database ODMs. Having an exact type hint for the dictionary form would allow for automatic data validation and more.
What is the feature request for?
The core library
The Problem
I frequently use the dictionary form of betterproto messages (from_dict(), to_dict()) as arguments for web endpoints (Quart, Quart Schema) and database ODMs (such as Beanie for MongoDB).
These frameworks often use pydantic for their validation mechanism, as well as documentation generation (such as Swagger). Having an exact type hint for the dictionary form would allow for automatic data validation and more.
Using the original Message type hints directly (i.e. using the @dataclass annotated message classes) is an option. Yet in some of my cases I need to use the stringified form of the protobuf enums, rather than the integer based one. Generating a dynamic type hint in runtime is cumbersome and not a solution.
The Ideal Solution
It would be great to have the dictionary types of messages to be generated side by side with the current dataclasses:
They dictionary types could be later used inside validation frameworks.
See the following example:
enum DocumentType {
UNKNOWN = 0;
CONTRACT = 1;
INVOICE = 2;
}
message Document {
DocumentType type = 1;
string content = 2;
}
This would generate to
# Currently generated
class DocumentType(betterproto.Enum):
UNKNOWN = 0
CONTRACT = 1
INVOICE = 2
@dataclass(eq=False, repr=False)
class Document(betterproto.Message):
type: "DocumentType" = betterproto.enum_field(1)
content: str = betterproto.string_field(2, group="_data")
# Additionally generated
class DocumentTypeStringified(str, Enum):
UNKNOWN = "UNKNOWN"
CONTRACT = "CONTRACT"
INVOICE = "INVOICE"
class DocumentDict(TypedDict):
type: DocumentTypeStringified
content: str
Now we could use this inside Quart-Schema for parameter validation and documentation:
app = Quart(__name__)
QuartSchema(app)
@app.post(
"/documents/<document_name>",
)
@validate_request(
DocumentDict,
)
async def create_document(document_name: str, data: DocumentDict):
document = Document().from_dict(data)
await db.save_document(document)
return jsonify(success=True), 200
Of course, there might be some corner-cases, e.g. what to do in case of bytes? Should this be done by manually extending the DocumentDict class and override it? How should dataclass names be extended without interfering with other messages? Maybe put them into their own python file?
All in all it seems to be a most valuable and maybe even an easy to develop solution for many good use cases.
What do you think?
Best regards
Markus
The Current Solution
Generating a dynamic type hint from proto enums in runtime is cumbersome and not very maintainable.
DocumentTypeEnumStringified= Enum("DocumentTypeEnumStringified", [item.name for item in DocumentTypeEnum])
class MyDbDocument(Document):
type: Annotated[str, DocumentTypeEnumStringified]
- 主要语言
- 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 天内回复