IDE 可以智能感知 protobuf-python 成员 [英] IDE can intellisense protobuf-python members
问题描述
我想要一个在 python 中生成的 protobuf 类的智能感知.但是生成的protobuf类的实现比较特殊,代码如下:
I want a intellisense of protobuf class generated in python. But the implementation of generated protobuf class are special, the code is like:
class X(_message.Message):
__metaclass__ = _reflection.GeneratedProtocolMessageType
DESCRIPTOR = _X
大多数python IDE只能智能感知__metaclass__
和DESCRIPTOR
这两个成员,而不是.proto文件中定义的成员.
Most python IDE only can intellisense __metaclass__
and DESCRIPTOR
two members rather than the members defined in .proto file.
如何制作?
推荐答案
如果您使用的是最新的 Python 版本 (3.7+),那么您可以试试我的 https://github.com/danielgtaylor/python-betterproto 项目.它生成具有适当类型的数据类,VSCode、PyCharm 和其他人可以使用这些数据类来提供类型提示.智能感知.
If you're using a recent Python version (3.7+) then you can try out my https://github.com/danielgtaylor/python-betterproto project. It generates dataclasses with proper types that VSCode, PyCharm, and others can use to provide type hints & intellisense.
例如,给定这个输入:
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)
它比官方生成的描述符类更容易阅读.
It is much, much easier to read than the official generated descriptor classes.
这篇关于IDE 可以智能感知 protobuf-python 成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!