我想要在python中生成的protobuf类的智能感知。但生成的protobuf类的实现是特殊的,代码如下:
class X(_message.Message):
__metaclass__ = _reflection.GeneratedProtocolMessageType
DESCRIPTOR = _X大多数python IDE只能智能感知__metaclass__和DESCRIPTOR两个成员,而不能感知.proto文件中定义的成员。
怎么做呢?
发布于 2019-10-31 13:06:03
如果您使用的是最新的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)它比官方生成的描述符类更容易阅读。
https://stackoverflow.com/questions/23579278
复制相似问题