From 9b5594adbed39b0f7180f55ff2aee996cafcb08e Mon Sep 17 00:00:00 2001 From: Michael Osthege Date: Wed, 26 Jan 2022 23:25:48 +0100 Subject: [PATCH] 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 --- src/betterproto/plugin/models.py | 16 ++++++---------- src/betterproto/templates/template.py.j2 | 6 ++++-- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/betterproto/plugin/models.py b/src/betterproto/plugin/models.py index 325137c..c2fccfc 100644 --- a/src/betterproto/plugin/models.py +++ b/src/betterproto/plugin/models.py @@ -142,17 +142,13 @@ def get_comment( sci_loc.leading_comments.strip().replace("\n", ""), width=79 - indent ) - if path[-2] == 2 and path[-4] != 6: - # This is a field - return f"{pad}# " + f"\n{pad}# ".join(lines) + # This is a field, message, enum, service, or method + if len(lines) == 1 and len(lines[0]) < 79 - indent - 6: + lines[0] = lines[0].strip('"') + return f'{pad}"""{lines[0]}"""' else: - # This is a message, enum, service, or method - if len(lines) == 1 and len(lines[0]) < 79 - indent - 6: - lines[0] = lines[0].strip('"') - return f'{pad}"""{lines[0]}"""' - else: - joined = f"\n{pad}".join(lines) - return f'{pad}"""\n{pad}{joined}\n{pad}"""' + joined = f"\n{pad}".join(lines) + return f'{pad}"""\n{pad}{joined}\n{pad}"""' return "" diff --git a/src/betterproto/templates/template.py.j2 b/src/betterproto/templates/template.py.j2 index 622de5a..f346672 100644 --- a/src/betterproto/templates/template.py.j2 +++ b/src/betterproto/templates/template.py.j2 @@ -28,10 +28,11 @@ class {{ enum.py_name }}(betterproto.Enum): {% endif %} {% for entry in enum.entries %} + {{ entry.name }} = {{ entry.value }} {% if entry.comment %} {{ entry.comment }} + {% endif %} - {{ entry.name }} = {{ entry.value }} {% endfor %} @@ -45,10 +46,11 @@ class {{ message.py_name }}(betterproto.Message): {% endif %} {% for field in message.fields %} + {{ field.get_field_string() }} {% if field.comment %} {{ field.comment }} + {% endif %} - {{ field.get_field_string() }} {% endfor %} {% if not message.fields %} pass