danielgtaylor / python-betterproto

Clean, modern, Python 3.6+ code generator & library for Protobuf 3 and async gRPC
MIT License
1.51k stars 214 forks source link

Add comments to generated code #429

Open AlexanderZender opened 2 years ago

AlexanderZender commented 2 years ago

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.

MicaelJarniac commented 1 year ago

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 commented 1 year ago

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."""
Gobot1234 commented 1 year ago

I was under the impression that betterproto already did this now

MicaelJarniac commented 1 year ago

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 commented 1 year ago

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.

ghost commented 1 year ago

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);
}