我想要在 python 中生成 protobuf 类的智能感知。但生成的protobuf类的实现比较特殊,代码如下:
class X(_message.Message):
__metaclass__ = _reflection.GeneratedProtocolMessageType
DESCRIPTOR = _X
大多数Python IDE只能智能感知__metaclass__
and DESCRIPTOR
两个成员而不是 .proto 文件中定义的成员。
怎样制作呢?
如果您使用的是最新的 Python 版本(3.7+),那么您可以尝试我的https://github.com/danielgtaylor/python-betterproto项目。它生成具有正确类型的数据类,VSCode、PyCharm 和其他可使用这些数据类来提供类型提示和智能感知。
例如,给定以下输入:
syntax = "proto3";
// Some documentation about the Test message.
message Test {
// Some documentation about the count.
int32 count = 1;
}
你会得到这样的输出:
# Generated by the protocol buffer compiler. DO NOT EDIT!
# sources: int32.proto
# plugin: python-betterproto
from dataclasses import dataclass
import betterproto
@dataclass
class Test(betterproto.Message):
"""Some documentation about the Test message."""
# Some documentation about the count.
count: int = betterproto.int32_field(1)
它比官方生成的描述符类更容易阅读。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)