From: John Snow <jsnow@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: "Peter Maydell" <peter.maydell@linaro.org>,
"Cleber Rosa" <crosa@redhat.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
qemu-devel@nongnu.org, "Eduardo Habkost" <ehabkost@redhat.com>
Subject: Re: [PATCH 14/37] qapi/common.py: Move comments into docstrings
Date: Thu, 17 Sep 2020 14:44:53 -0400 [thread overview]
Message-ID: <49e28f59-012c-9b7b-02b7-1854f85884b2@redhat.com> (raw)
In-Reply-To: <87d02kpizr.fsf@dusky.pond.sub.org>
On 9/17/20 10:37 AM, Markus Armbruster wrote:
> John Snow <jsnow@redhat.com> writes:
>
>> As docstrings, they'll show up in documentation and IDE help.
>>
>> Signed-off-by: John Snow <jsnow@redhat.com>
>> ---
>> scripts/qapi/common.py | 50 ++++++++++++++++++++++++++++++------------
>> 1 file changed, 36 insertions(+), 14 deletions(-)
>>
>> diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
>> index af01348b35..38d380f0a9 100644
>> --- a/scripts/qapi/common.py
>> +++ b/scripts/qapi/common.py
>> @@ -20,10 +20,18 @@
>> c_name_trans = str.maketrans('.-', '__')
>>
>>
>> -# ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
>> -# ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
>> -# ENUM24_Name -> ENUM24_NAME
>> def camel_to_upper(value: str) -> str:
>> + """
>> + Converts CamelCase to CAMEL_CASE.
>> +
>> + Examples:
>> + ENUMName -> ENUM_NAME
>> + EnumName1 -> ENUM_NAME1
>> + ENUM_NAME -> ENUM_NAME
>> + ENUM_NAME1 -> ENUM_NAME1
>> + ENUM_Name2 -> ENUM_NAME2
>> + ENUM24_Name -> ENUM24_NAME
>> + """
>> c_fun_str = c_name(value, False)
>> if value.isupper():
>> return c_fun_str
>> @@ -45,21 +53,33 @@ def camel_to_upper(value: str) -> str:
>> def c_enum_const(type_name: str,
>> const_name: str,
>> prefix: Optional[str] = None) -> str:
>> + """
>> + Generate a C enumeration constant name.
>> +
>> + :param type_name: The name of the enumeration.
>> + :param const_name: The name of this constant.
>> + :param prefix: Optional, prefix that overrides the type_name.
>> + """
>
> Not actually a move. Suggest to retitle
>
> qapi/common: Turn comments in docstrings, and add more
>
OK.
>> if prefix is not None:
>> type_name = prefix
>> return camel_to_upper(type_name) + '_' + c_name(const_name, False).upper()
>>
>>
>> -# Map @name to a valid C identifier.
>> -# If @protect, avoid returning certain ticklish identifiers (like
>> -# C keywords) by prepending 'q_'.
>> -#
>> -# Used for converting 'name' from a 'name':'type' qapi definition
>> -# into a generated struct member, as well as converting type names
>> -# into substrings of a generated C function name.
>> -# '__a.b_c' -> '__a_b_c', 'x-foo' -> 'x_foo'
>> -# protect=True: 'int' -> 'q_int'; protect=False: 'int' -> 'int'
>> def c_name(name: str, protect: bool = True) -> str:
>> + """
>> + Map `name` to a valid C identifier.
>> +
>> + Used for converting 'name' from a 'name':'type' qapi definition
>> + into a generated struct member, as well as converting type names
>> + into substrings of a generated C function name.
>> +
>> + '__a.b_c' -> '__a_b_c', 'x-foo' -> 'x_foo'
>> + protect=True: 'int' -> 'q_int'; protect=False: 'int' -> 'int'
>> +
>> + :param name: The name to map.
>> + :param protect: If true, avoid returning certain ticklish identifiers
>> + (like C keywords) by prepending 'q_'.
>> + """
>> # ANSI X3J11/88-090, 3.1.1
>> c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
>> 'default', 'do', 'double', 'else', 'enum', 'extern',
>> @@ -135,9 +155,11 @@ def pop(self, amount: int = 4) -> int:
>> INDENT = Indent(0)
>>
>>
>> -# Generate @code with @kwds interpolated.
>> -# Obey INDENT level, and strip EATSPACE.
>> def cgen(code: str, **kwds: Union[str, int]) -> str:
>> + """
>> + Generate `code` with `kwds` interpolated.
>> + Obey `INDENT`, and strip `EATSPACE`.
>> + """
>> raw = code % kwds
>> if INDENT:
>> raw, _ = re.subn(r'^(?!(#|$))', str(INDENT), raw, flags=re.MULTILINE)
>
> Can you point to documentation on the docstring conventions and markup
> to use?
>
Short answer: No.
Long answer:
It's actually completely arbitrary, with major competing de-facto
standards. Their primary function is to be stored to the __doc__
attribute of a module/class/method and can be displayed when using the
interactive function help(...).
https://www.python.org/dev/peps/pep-0257/ covers docstrings only in an
extremely broad sense. In summary, it asks:
- Use full sentences that end in periods
- Use the triple-double quote style
- multi-line docstrings should have their closing quote on a line by itself
- multi-line docstrings should use a one-sentence summary, a blank line,
and then a more elaborate description.
It recommends you document arguments, types, return values and types,
exceptions and so on but does not dictate a format. Two popular
conventions are the google-style [1] and the NumPy-style [2] docstring
formats.
I write docstrings assuming we will be using *Sphinx* as our
documentation tool. Sphinx does not read docstrings at all by default,
but *autodoc* does. Autodoc assumes your docstrings are written in the
Sphinx dialect of ReST.
What you really want to look for is the "Python domain" documentation in
Sphinx:
https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html
Autodoc will annotate function/method docstrings with "py:function" or
"py:method" as appropriate, but the actual contents of the block are
still up to you.
For those, you want to look up the Python domain info field lists that
are supported by Sphinx, which are here:
https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#info-field-lists
Taken together with PEP 257, you generally want something like this:
"""
Function f converts uncertainty into happiness.
Function f only works on days that do not end in 'y'. Caution should be
used when integrating this function into threaded code.
:param uncertainty: All of your doubts.
:raise RuntimeError: When it is a day ending in 'y'.
:return: Your newfound happiness.
"""
I use the single-backtick as the Sphinx-ese "default role" resolver,
which generally should resolve to a reference to some Python entity. The
double-backtick is used to do formatted text for things like string
literals and so on.
Coffee break.
Having said this, I have not found any tool to date that actually
*checks* these comments for consistency. The pycharm IDE interactively
highlights them when it senses that you have made a mistake, but that
cannot be worked into our CI process that I know of - it's a proprietary
checker.
So right now, they're just plaintext that I am writing to approximate
the Sphinx style until such time as I enable autodoc for the module and
fine-tune the actual formatting and so on.
--js
[1]
https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
[2]
https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_numpy.html#example-numpy
next prev parent reply other threads:[~2020-09-17 18:47 UTC|newest]
Thread overview: 98+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-15 22:39 [PATCH 00/37] qapi: static typing conversion, pt1 John Snow
2020-09-15 22:39 ` [PATCH 01/37] python: Require 3.6+ John Snow
2020-09-16 8:42 ` Markus Armbruster
2020-09-16 8:51 ` Daniel P. Berrangé
2020-09-15 22:39 ` [PATCH 02/37] [DO-NOT-MERGE] qapi: add debugging tools John Snow
2020-09-15 22:39 ` [PATCH 03/37] qapi-gen: Separate arg-parsing from generation John Snow
2020-09-15 22:39 ` [PATCH 04/37] qapi: move generator entrypoint into module John Snow
2020-09-16 11:54 ` Markus Armbruster
2020-09-16 14:24 ` John Snow
2020-09-17 7:46 ` Markus Armbruster
2020-09-15 22:39 ` [PATCH 05/37] qapi: Remove wildcard includes John Snow
2020-09-15 22:39 ` [PATCH 06/37] qapi: delint using flake8 John Snow
2020-09-16 12:12 ` Markus Armbruster
2020-09-16 14:29 ` John Snow
2020-09-17 7:54 ` Markus Armbruster
2020-09-17 16:57 ` John Snow
2020-09-18 10:33 ` Markus Armbruster
2020-09-18 18:13 ` John Snow
2020-09-21 7:31 ` Markus Armbruster
2020-09-21 14:50 ` John Snow
2020-09-15 22:39 ` [PATCH 07/37] qapi: add pylintrc John Snow
2020-09-16 12:30 ` Markus Armbruster
2020-09-16 14:37 ` John Snow
2020-09-17 7:58 ` Markus Armbruster
2020-09-17 17:06 ` John Snow
2020-09-15 22:39 ` [PATCH 08/37] qapi/common.py: Remove python compatibility workaround John Snow
2020-09-16 12:34 ` Markus Armbruster
2020-09-16 14:38 ` John Snow
2020-09-15 22:39 ` [PATCH 09/37] qapi/common.py: Add indent manager John Snow
2020-09-16 15:13 ` Markus Armbruster
2020-09-16 22:25 ` John Snow
2020-09-17 8:07 ` Markus Armbruster
2020-09-17 17:18 ` John Snow
2020-09-18 10:55 ` Markus Armbruster
2020-09-18 16:08 ` John Snow
2020-09-21 7:43 ` Markus Armbruster
2020-09-15 22:40 ` [PATCH 10/37] qapi/common.py: delint with pylint John Snow
2020-09-17 14:15 ` Markus Armbruster
2020-09-17 17:48 ` John Snow
2020-09-18 11:03 ` Markus Armbruster
2020-09-15 22:40 ` [PATCH 11/37] qapi/common.py: Replace one-letter 'c' variable John Snow
2020-09-17 14:17 ` Markus Armbruster
2020-09-17 17:51 ` John Snow
2020-09-15 22:40 ` [PATCH 12/37] qapi/common.py: check with pylint John Snow
2020-09-15 22:40 ` [PATCH 13/37] qapi/common.py: add notational type hints John Snow
2020-09-17 14:32 ` Markus Armbruster
2020-09-17 18:18 ` John Snow
2020-09-17 20:06 ` John Snow
2020-09-18 11:14 ` Markus Armbruster
2020-09-18 15:24 ` John Snow
2020-09-15 22:40 ` [PATCH 14/37] qapi/common.py: Move comments into docstrings John Snow
2020-09-17 14:37 ` Markus Armbruster
2020-09-17 18:44 ` John Snow [this message]
2020-09-17 19:14 ` Eduardo Habkost
2020-09-17 19:31 ` John Snow
2020-09-24 15:06 ` Markus Armbruster
2020-09-24 16:31 ` John Snow
2020-09-25 7:49 ` Markus Armbruster
2020-09-25 14:07 ` John Snow
2020-09-15 22:40 ` [PATCH 15/37] qapi/common.py: split build_params into new file John Snow
2020-09-17 14:42 ` Markus Armbruster
2020-09-17 18:53 ` John Snow
2020-09-17 19:40 ` John Snow
2020-09-15 22:40 ` [PATCH 16/37] qapi: establish mypy type-checking baseline John Snow
2020-09-18 11:55 ` Markus Armbruster
2020-09-18 14:27 ` John Snow
2020-09-21 8:05 ` Markus Armbruster
2020-09-21 14:41 ` John Snow
2020-09-25 1:18 ` Eduardo Habkost
2020-09-18 19:03 ` John Snow
2020-09-21 8:05 ` Markus Armbruster
2020-09-21 14:46 ` John Snow
2020-09-15 22:40 ` [PATCH 17/37] qapi/events.py: add notational type hints John Snow
2020-09-15 22:40 ` [PATCH 18/37] qapi/events.py: Move comments into docstrings John Snow
2020-09-15 22:40 ` [PATCH 19/37] qapi/commands.py: Don't re-bind to variable of different type John Snow
2020-09-15 22:40 ` [PATCH 20/37] qapi/commands.py: add notational type hints John Snow
2020-09-15 22:40 ` [PATCH 21/37] qapi/commands.py: enable checking with mypy John Snow
2020-09-15 22:40 ` [PATCH 22/37] qapi/source.py: add notational type hints John Snow
2020-09-15 22:40 ` [PATCH 23/37] qapi/source.py: delint with pylint John Snow
2020-09-15 22:40 ` [PATCH 24/37] qapi/gen.py: Fix edge-case of _is_user_module John Snow
2020-09-15 22:40 ` [PATCH 25/37] qapi/gen.py: add notational type hints John Snow
2020-09-15 22:40 ` [PATCH 26/37] qapi/gen.py: Enable checking with mypy John Snow
2020-09-15 22:40 ` [PATCH 27/37] qapi/gen.py: Remove unused parameter John Snow
2020-09-15 22:40 ` [PATCH 28/37] qapi/gen.py: update write() to be more idiomatic John Snow
2020-09-15 22:40 ` [PATCH 29/37] qapi/gen.py: delint with pylint John Snow
2020-09-15 22:40 ` [PATCH 30/37] qapi/introspect.py: Add a typed 'extra' structure John Snow
2020-09-15 22:40 ` [PATCH 31/37] qapi/introspect.py: add _gen_features helper John Snow
2020-09-15 22:40 ` [PATCH 32/37] qapi/introspect.py: create a typed 'Node' data structure John Snow
2020-09-15 22:40 ` [PATCH 33/37] qapi/introspect.py: add notational type hints John Snow
2020-09-15 22:40 ` [PATCH 34/37] qapi/types.py: " John Snow
2020-09-15 22:40 ` [PATCH 35/37] qapi/types.py: remove one-letter variables John Snow
2020-09-15 22:40 ` [PATCH 36/37] qapi/visit.py: remove unused parameters from gen_visit_object John Snow
2020-09-15 22:40 ` [PATCH 37/37] qapi/visit.py: add notational type hints John Snow
2020-09-16 22:33 ` [PATCH 00/37] qapi: static typing conversion, pt1 John Snow
2020-09-17 20:22 ` John Snow
2020-09-18 7:50 ` Markus Armbruster
2020-09-18 13:07 ` Philippe Mathieu-Daudé
2020-09-18 14:30 ` John Snow
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=49e28f59-012c-9b7b-02b7-1854f85884b2@redhat.com \
--to=jsnow@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=armbru@redhat.com \
--cc=crosa@redhat.com \
--cc=ehabkost@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).