Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IDE can intellisense protobuf-python members

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

Most python IDE only can intellisense __metaclass__ and DESCRIPTOR two members rather than the members defined in .proto file.

How to make it?

like image 785
jean Avatar asked May 10 '14 09:05

jean


People also ask

What is Protobuf Python?

Protocol buffers (Protobuf) are a language-agnostic data serialization format developed by Google. Protobuf is great for the following reasons: Low data volume: Protobuf makes use of a binary format, which is more compact than other formats such as JSON. Persistence: Protobuf serialization is backward-compatible.

How do I read a proto file?

If you cannot open your PROTO file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a PROTO file directly in the browser: Just drag the file onto this browser window and drop it.


1 Answers

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.

For example, given this input:

syntax = "proto3";

// Some documentation about the Test message.
message Test {
    // Some documentation about the count.
    int32 count = 1;
}

You would get output like this:

# 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.

like image 73
Daniel Avatar answered Oct 06 '22 10:10

Daniel