danielgtaylor / python-betterproto

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

Compiled comments are stripped of newlines #80

Closed boukeversteegh closed 10 months ago

boukeversteegh commented 4 years ago

Compare line breaks in comments 👇


// `Any` contains an arbitrary serialized protocol buffer message along with a
// URL that describes the type of the serialized message.
//
// Protobuf library provides support to pack/unpack Any values in the form
// of utility functions or additional generated methods of the Any type.
message Any {

}

@dataclass
class Any(betterproto.Message):
    """
    `Any` contains an arbitrary serialized protocol buffer message along with a
    URL that describes the type of the serialized message. Protobuf library
    provides support to pack/unpack Any values in the form of utility functions
    or additional generated methods of the Any type. 
    """
nhuray commented 3 years ago

@boukeversteegh I saw you are involve in the some issues related to the support of Any type.

Do you know if there's some news for supporting Any in the coming releases ?

davidmankin commented 2 years ago

For whoever picks this up, in issue #310 I suggested also updating the code examples in the docstring to be betterproto style code instead of C++, Java, and google-Python.

MicaelJarniac commented 1 year ago

Another example:

syntax = "proto3";

message MyMessage {
  // JSON data in the following format:
  // {
  //     "name": "Foo",
  //     "age": 0,
  //     "job": {
  //         "area": "Bar",
  //         "salary": 0
  //     }
  // }
  string json = 1;
}
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: mymessage.proto
# plugin: python-betterproto
from dataclasses import dataclass

import betterproto

@dataclass(eq=False, repr=False)
class MyMessage(betterproto.Message):
    json: str = betterproto.string_field(1)
    """
    JSON data in the following format: {     "name": "Foo",     "age": 0,
    "job": {         "area": "Bar",         "salary": 0     } }
    """

It's great that it detects the comments and turns them into docstrings, but it breaks the formatting. I'd like to get the following result:

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

import betterproto

@dataclass(eq=False, repr=False)
class MyMessage(betterproto.Message):
    json: str = betterproto.string_field(1)
    """
    JSON data in the following format:
    {
        "name": "Foo",
        "age": 0,
        "job": {
            "area": "Bar",
            "salary": 0
        }
    }
    """
mbrancato commented 10 months ago

Adding that there are examples of this already in the existing tests:

https://github.com/danielgtaylor/python-betterproto/blob/5666393f9d10e13609d8eeac8d1ab3815dce5fd6/tests/inputs/example/example.proto#L834-L880

https://github.com/danielgtaylor/python-betterproto/blob/5666393f9d10e13609d8eeac8d1ab3815dce5fd6/src/betterproto/lib/google/protobuf/__init__.py#L1234-L1257

Gobot1234 commented 10 months ago

This was fixed in #532

mbrancato commented 10 months ago

Any reason the tests pass if the generated example in master does not appear to have this fix?

Gobot1234 commented 10 months ago

I'm not entirely sure we test it but also testing stability of comments seems very fragile.