Reputation: 11
I'm trying to figure out how to make my comments in my .proto file appear in the files generated for Python by protoc.
I have a proto file service.proto
.
In it I have added comments on some of the messages as well as on some of the fields.
So for example:
// Comment regarding this message in general
message StepConfiguration {
// Comment regarding this field
bool skip_step = 1;
}
I have tried to execute the following command:
python -m grpc_tools.protoc -I protos/V1/ --python_out=client/ --pyi_out=client/ --grpc_python_out=client/ protos/V1/service.proto
This will generate the following 3 files:
If I look through these 3 files, none of my comments carry over into them from the proto file. Is there any way to configure protoc to do this? Or is there another tool I can use in combination with protoc to do this?
My main goal is for PyCharm to be able to present these comments when using the generated code.
Other information of possible relevance:
Upvotes: 1
Views: 847
Reputation: 11
to get comments for python generated code take a look at this plugin: https://github.com/nipunn1313/mypy-protobuf
using official protoc compiller from google: https://grpc.io/docs/protoc-installation/
first install it to your code generator
python3 -m venv .venv
source .venv/bin/activate
pip install mypy-protobuf
then use it to generate code:
mkdir -p client
protoc -I protos/V1/ --plugin=$PWD/.venv/bin/protoc-gen-mypy --mypy_out=client/ --python_out=client/ --grpc_python_out=client/ protos/V1/service.proto
the comments in .proto will be provided as docstrings
the example file service.proto
// proto file global comment
syntax = "proto3";
// Comment regarding this message in general
message StepConfiguration {
// Comment regarding this field
bool skip_step = 1;
}
will result into following service.pyi:
"""
@generated by mypy-protobuf. Do not edit manually!
isort:skip_file
proto file global comment"""
import builtins
import google.protobuf.descriptor
import google.protobuf.message
import sys
if sys.version_info >= (3, 8):
import typing as typing_extensions
else:
import typing_extensions
DESCRIPTOR: google.protobuf.descriptor.FileDescriptor
@typing_extensions.final
class StepConfiguration(google.protobuf.message.Message):
"""Comment regarding this message in general"""
DESCRIPTOR: google.protobuf.descriptor.Descriptor
SKIP_STEP_FIELD_NUMBER: builtins.int
skip_step: builtins.bool
"""Comment regarding this field"""
def __init__(
self,
*,
skip_step: builtins.bool = ...,
) -> None: ...
def ClearField(self, field_name: typing_extensions.Literal["skip_step", b"skip_step"]) -> None: ...
global___StepConfiguration = StepConfiguration
Upvotes: 1