Skip to content

Commit e94ac70

Browse files
Format field comments also as docstrings
Closes #303
1 parent 671c0ff commit e94ac70

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

src/betterproto/plugin/models.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,13 @@ def get_comment(
147147
sci_loc.leading_comments.strip().replace("\n", ""), width=79 - indent
148148
)
149149

150-
if path[-2] == 2 and path[-4] != 6:
151-
# This is a field
152-
return f"{pad}# " + f"\n{pad}# ".join(lines)
150+
# This is a field, message, enum, service, or method
151+
if len(lines) == 1 and len(lines[0]) < 79 - indent - 6:
152+
lines[0] = lines[0].strip('"')
153+
return f'{pad}"""{lines[0]}"""'
153154
else:
154-
# This is a message, enum, service, or method
155-
if len(lines) == 1 and len(lines[0]) < 79 - indent - 6:
156-
lines[0] = lines[0].strip('"')
157-
return f'{pad}"""{lines[0]}"""'
158-
else:
159-
joined = f"\n{pad}".join(lines)
160-
return f'{pad}"""\n{pad}{joined}\n{pad}"""'
155+
joined = f"\n{pad}".join(lines)
156+
return f'{pad}"""\n{pad}{joined}\n{pad}"""'
161157

162158
return ""
163159

src/betterproto/templates/template.py.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ class {{ message.py_name }}(betterproto.Message):
4545

4646
{% endif %}
4747
{% for field in message.fields %}
48+
{{ field.get_field_string() }}
4849
{% if field.comment %}
4950
{{ field.comment }}
5051
{% endif %}
51-
{{ field.get_field_string() }}
5252
{% endfor %}
5353
{% if not message.fields %}
5454
pass

0 commit comments

Comments
 (0)