Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Provide a way to use a custom base class instead of betterproto.Message

未关闭
#664 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
python
领域
tooling

调研方向

先从编译器 CLI 参数的设计以及 issue 中提到的 init Jinja 模板开始,然后检查生成的消息类如何选择 betterproto.Message,以及生成的 init.py 文件如何写入。完成的标准是:为生成的消息使用已配置的自定义基类,并且可以通过生成的包结构导入子包。

由索引模型根据 Issue 内容生成。

描述

enhancement
Summary

Allow to customize the base class, to provide helper methods and enrich the model

What is the feature request for?

The core library

The Problem

We developed an internal library on top of betterproto and pika rabbitmq packages.
During development, we found that

  1. betterproto lacks a way to customize the base class for the generated messages classes.
  2. it's not possible to access message classes in the following way:
import mylib as ml

ml.messages.device.settings.ConfigureDevice(*args)
The Ideal Solution

To ease all above, would be handy to provide:

  1. A way to use a MyMessageBaseClass(betterproto.Message) as base class for generated messages. This would probably be a new cli argument for the compiler e.g. --base_class=mylib.base.MyMessageBaseClass to use in the init jinja template.
  2. import subpackages (if any) in the __init__.py generated files.
The Current Solution

To enrich the classes with helper methods and to override some methods of the betterproto.Message class, we first patched the generated classes, to add mixins and overridden methods at runtime.

This is working well, but Python IDEs' support is limited.

We solved some of the issues above by adding a post compilation step at installation time, to add code to the betterproto generated files.
During this step, we also add subpackages imports to __init__.py files, to access messages conveniently using the fqn (see point 2. of the issue).

主要语言
Python
星标
1.8k
派生
235
PR 合并指标
30 天内没有已合并 PR

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

danielgtaylor/python-betterproto 的其他 Issue

查看 danielgtaylor/python-betterproto 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。