python-betterproto icon indicating copy to clipboard operation
python-betterproto copied to clipboard

Add comments to generated code

Open AlexanderZender opened this issue 3 years ago • 7 comments

The google GRPC compiler copies the comments made in the proto files to their .py counter parts.

For code documentation it would be great if the betterproto compiler could also copy over the added comments from methods and messages.

AlexanderZender avatar Oct 01 '22 11:10 AlexanderZender

You mean something like the following?

syntax = "proto3";

package hello;

// Greeting represents a message you can tell a user.
message Greeting {
  string message = 1;  // This is a comment.
}
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: example.proto
# plugin: python-betterproto
from dataclasses import dataclass

import betterproto


@dataclass
class Greeting(betterproto.Message):
    """Greeting represents a message you can tell a user."""

    message: str = betterproto.string_field(1)  # This is a comment.

MicaelJarniac avatar May 09 '23 14:05 MicaelJarniac

Apparently, the following might actually also be valid, and could potentially be used in auto docs:

# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: example.proto
# plugin: python-betterproto
from dataclasses import dataclass

import betterproto


@dataclass
class Greeting(betterproto.Message):
    """Greeting represents a message you can tell a user."""

    message: str = betterproto.string_field(1)
    """This is a comment."""

MicaelJarniac avatar May 09 '23 14:05 MicaelJarniac

I was under the impression that betterproto already did this now

Gobot1234 avatar May 09 '23 14:05 Gobot1234

I think it does only the class docstring ("""Greeting represents a message you can tell a user."""), but ignores comments on the individual attributes (This is a comment.).

MicaelJarniac avatar May 09 '23 16:05 MicaelJarniac

Upon further testing, it seems like it does include some comments on attributes as well, but when they're written above the attribute, and not in front of them, like so:

syntax = "proto3";

package hello;

// Greeting represents a message you can tell a user.
message Greeting {
  // This is a comment about `message`.
  string message = 1;
}
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: example.proto
# plugin: python-betterproto
from dataclasses import dataclass

import betterproto


@dataclass
class Greeting(betterproto.Message):
    """Greeting represents a message you can tell a user."""

    message: str = betterproto.string_field(1)
    """This is a comment about `message`."""

Given this, I'm now unsure of whether or not it'd be a good idea to include the comments written on the same line as the attribute. Maybe something like this could work:

syntax = "proto3";

package hello;

// Greeting represents a message you can tell a user.
message Greeting {
  // This is a comment about `message`.
  string message = 1;  // And an inline comment.
}
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: example.proto
# plugin: python-betterproto
from dataclasses import dataclass

import betterproto


@dataclass
class Greeting(betterproto.Message):
    """Greeting represents a message you can tell a user."""

    message: str = betterproto.string_field(1)  # And an inline comment.
    """This is a comment about `message`."""

This would keep the current behavior and also support inline comments.

MicaelJarniac avatar May 11 '23 12:05 MicaelJarniac

Hi, is it possible to include comments describing services and their methods in Python files generated by betterproto?

Proto file example:

// Description of the service
service Test {
   // Description of the method
   rpc ExampleMethod(ExampleRequest) returns (ExampleResponse);
}

jasmina1122 avatar Jul 26 '23 13:07 jasmina1122