Skip to content

Commit 9b5594a

Browse files
Format field comments also as docstrings (#304)
Closes #303 * Format field comments also as docstrings To make it clear that they refer to the item above. * Fix placement of enum item docstrings * Add line breaks after class attribute or enum item docstrings
1 parent d991040 commit 9b5594a

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

src/betterproto/plugin/models.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,13 @@ def get_comment(
142142
sci_loc.leading_comments.strip().replace("\n", ""), width=79 - indent
143143
)
144144

145-
if path[-2] == 2 and path[-4] != 6:
146-
# This is a field
147-
return f"{pad}# " + f"\n{pad}# ".join(lines)
145+
# This is a field, message, enum, service, or method
146+
if len(lines) == 1 and len(lines[0]) < 79 - indent - 6:
147+
lines[0] = lines[0].strip('"')
148+
return f'{pad}"""{lines[0]}"""'
148149
else:
149-
# This is a message, enum, service, or method
150-
if len(lines) == 1 and len(lines[0]) < 79 - indent - 6:
151-
lines[0] = lines[0].strip('"')
152-
return f'{pad}"""{lines[0]}"""'
153-
else:
154-
joined = f"\n{pad}".join(lines)
155-
return f'{pad}"""\n{pad}{joined}\n{pad}"""'
150+
joined = f"\n{pad}".join(lines)
151+
return f'{pad}"""\n{pad}{joined}\n{pad}"""'
156152

157153
return ""
158154

src/betterproto/templates/template.py.j2

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ class {{ enum.py_name }}(betterproto.Enum):
2828

2929
{% endif %}
3030
{% for entry in enum.entries %}
31+
{{ entry.name }} = {{ entry.value }}
3132
{% if entry.comment %}
3233
{{ entry.comment }}
34+
3335
{% endif %}
34-
{{ entry.name }} = {{ entry.value }}
3536
{% endfor %}
3637

3738

@@ -45,10 +46,11 @@ class {{ message.py_name }}(betterproto.Message):
4546

4647
{% endif %}
4748
{% for field in message.fields %}
49+
{{ field.get_field_string() }}
4850
{% if field.comment %}
4951
{{ field.comment }}
52+
5053
{% endif %}
51-
{{ field.get_field_string() }}
5254
{% endfor %}
5355
{% if not message.fields %}
5456
pass

0 commit comments

Comments
 (0)