styleguide
styleguide copied to clipboard
Python: Clarify if order of docstring sections is fixed
The python style guide mentions the docstring sections Args, Returns/Yields and Raises but does not clearly specify whether their order is enforced.
All examples show
Args: ...
Returns: ...
Raises: ...
Should this be taken as the only acceptable order? Or would e.g. Raises: before Returns: be allowed?
Question arose in https://github.com/danymat/neogen/pull/194
@gpshead, even just a confirmation whether google tooling currently enforces a specific order would be useful.
yes, Args should always come before Returns/Yields. Raises should be after those.
Thank you @vapier 😊
Out of curiosity I'd like to ask a couple things
- Does
GoogleDocstringCheckerexplicitly enforce this section order? - If so, what is the underlying reason for placing
Raises:afterReturns:? To me the more appropriate thing would beArgs:-> [Raises:->]Returns:because the start of a function is the input, args, and a raise interrupts a return, soRaises:is "between" the input arguments and its expected return.
GoogleDocstringChecker does not check ordering. we prob should add that. CrOS's docstring checker does and has for ages fwiw.
while not explicitly stated, you can see example docstrings using this order in the style guide. as with the first rule, be consistent, it follows that this order is what the guide assumes.
wrt Raises ordering, i understand your point about docs reflecting code flow, but i would argue that:
- functions not raising exceptions explicitly is pretty common which means many docstrings would be
Args -> Returns(or justArgsor justReturns), so puttingRaisesat the end is a bit more consistent. - the docstring content is for programmers to understand how to use the API, and it should focus on common usage, and raising exceptions is ... exceptional. programmers would primarily care about
Args("how do i call this thing"), then secondarily care aboutReturns("how do i use the result"). only sometimes would specific exceptions be handled by the caller to do something else (and not be a lame log+re-raise trampoline), at which point people would look atRaises. so sticking the uncommon content right between the two most common blocks doesn't make sense.
As long as it's an enforced expectation that makes me more comfortable recommending this order to others as it comes up in future PRs. And the reasoning is understandable (preferring the most common case). Thank you!