8000 Fix documentation (#630) · danielgtaylor/python-betterproto@c621ef8 · GitHub
[go: up one dir, main page]

Skip to content

Commit c621ef8

Browse files
Fix documentation (#630)
* Fix missing documentation * Add test * Add test * Format * Reformat
1 parent 34b8249 commit c621ef8

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

src/betterproto/plugin/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ def __post_init__(self) -> None:
661661

662662
@dataclass
663663
class ServiceCompiler(ProtoContentBase):
664+
source_file: FileDescriptorProto
664665
parent: OutputTemplate = PLACEHOLDER
665666
proto_obj: DescriptorProto = PLACEHOLDER
666667
path: List[int] = PLACEHOLDER
@@ -682,6 +683,7 @@ def py_name(self) -> str:
682683

683684
@dataclass
684685
class ServiceMethodCompiler(ProtoContentBase):
686+
source_file: FileDescriptorProto
685687
parent: ServiceCompiler
686688
proto_obj: MethodDescriptorProto
687689
path: List[int] = PLACEHOLDER

src/betterproto/plugin/parser.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def generate_code(request: CodeGeneratorRequest) -> CodeGeneratorResponse:
143143
for output_package_name, output_package in request_data.output_packages.items():
144144
for proto_input_file in output_package.input_files:
145145
for index, service in enumerate(proto_input_file.service):
146-
read_protobuf_service(service, index, output_package)
146+
read_protobuf_service(proto_input_file, service, index, output_package)
147147

148148
# Generate output files
149149
output_paths: Set[pathlib.Path] = set()
@@ -249,12 +249,21 @@ def read_protobuf_type(
249249

250250

251251
def read_protobuf_service(
252-
service: ServiceDescriptorProto, index: int, output_package: OutputTemplate
252+
source_file: FileDescriptorProto,
253+
service: ServiceDescriptorProto,
254+
index: int,
255+
output_package: OutputTemplate,
253256
) -> None:
254257
service_data = ServiceCompiler(
255-
parent=output_package, proto_obj=service, path=[6, index]
258+
source_file=source_file,
259+
parent=output_package,
260+
proto_obj=service,
261+
path=[6, index],
256262
)
257263
for j, method in enumerate(service.method):
258264
ServiceMethodCompiler(
259-
parent=service_data, proto_obj=method, path=[6, index, 2, j]
265+
source_file=source_file,
266+
parent=service_data,
267+
proto_obj=method,
268+
path=[6, index, 2, j],
260269
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
syntax = "proto3";
2+
package documentation;
3+
4+
// Documentation of message
5+
message Test {
6+
// Documentation of field
7+
uint32 x = 1;
8+
}
9+
10+
// Documentation of enum
11+
enum Enum {
12+
// Documentation of variant
13+
Enum_Variant = 0;
14+
}
15+
16+
// Documentation of service
17+
service Service {
18+
// Documentation of method
19+
rpc get(Test) returns (Test);
20+
}

tests/test_documentation.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import ast
2+
import inspect
3+
4+
5+
def test_documentation():
6+
from .output_betterproto.documentation import (
7+
Enum,
8+
ServiceBase,
9+
ServiceStub,
10+
Test,
11+
)
12+
13+
assert Test.__doc__ == "Documentation of message"
14+
15+
source = inspect.getsource(Test)
16+
tree = ast.parse(source)
17+
assert tree.body[0].body[2].value.value == "Documentation of field"
18+
19+
assert Enum.__doc__ == "Documentation of enum"
20+
21+
source = inspect.getsource(Enum)
22+
tree = ast.parse(source)
23+
assert tree.body[0].body[2].value.value == "Documentation of variant"
24+
25+
assert ServiceBase.__doc__ == "Documentation of service"
26+
assert ServiceBase.get.__doc__ == "Documentation of method"
27+
28+
assert ServiceStub.__doc__ == "Documentation of service"
29+
assert ServiceStub.get.__doc__ == "Documentation of method"

0 commit comments

Comments
 (0)
0