Difference between revisions of "Sphinx gotchas"

From Software Heritage Wiki
Jump to navigation Jump to search
(→‎Napoleon: add gotcha: Args sections)
(reorder good/bad sections)
Line 6: Line 6:
  
 
All sorts of [http://www.sphinx-doc.org/en/stable/rest.html#lists-and-quote-like-blocks '''lists'''] require an empty line before the first bullet and after the last one, to be properly interpreted as list. No indentation is required for list elements w.r.t. surrounding text, and line continuations should be indented like the first character after the bullet
 
All sorts of [http://www.sphinx-doc.org/en/stable/rest.html#lists-and-quote-like-blocks '''lists'''] require an empty line before the first bullet and after the last one, to be properly interpreted as list. No indentation is required for list elements w.r.t. surrounding text, and line continuations should be indented like the first character after the bullet
 +
 +
Bad:
 +
<pre>
 +
this is a bad example that will not be interpreted as a list
 +
preceding text
 +
- foo
 +
- bar
 +
- baz
 +
following text
 +
</pre>
  
 
Good:
 
Good:
Line 23: Line 33:
 
Bad:
 
Bad:
 
<pre>
 
<pre>
this is a bad example that will not be interpreted as a list
 
preceding text
 
 
- foo
 
- foo
- bar
+
- nested lists also requires empty lines, but they are missing here
- baz
+
  - inner list 1
following text
+
  - inner list 2
 +
- outer list continues here
 
</pre>
 
</pre>
  
Line 45: Line 54:
 
surrounding text
 
surrounding text
 
</pre>
 
</pre>
 +
 +
=== Verbatim source code ===
 +
 +
Verbatim [http://www.sphinx-doc.org/en/stable/rest.html#source-code '''code blocks'''], e.g., for code examples, requires double colon at the end of a line, then an empty line, and then the code block itself, indented:
  
 
Bad:
 
Bad:
 
<pre>
 
<pre>
- foo
+
This does not work as there is a single column and no empty line before code:
- nested lists also requires empty lines
+
    def foo(bar, baz):
  - inner list 1
+
        qux = bar + baz
  - inner list 2
+
 
- outer list continues here
+
        return qux
 
</pre>
 
</pre>
  
=== Verbatim source code ===
+
Good:
 
 
Verbatim [http://www.sphinx-doc.org/en/stable/rest.html#source-code '''code blocks'''], e.g., for code examples, requires double colon at the end of a line, then an empty line, and then the code block itself, indented:
 
 
 
 
<pre>
 
<pre>
 
a nice example of python code follows::
 
a nice example of python code follows::
Line 72: Line 82:
 
'''Inline code samples''' use double backquotes, and not single ones.
 
'''Inline code samples''' use double backquotes, and not single ones.
  
Good:
+
Bad:
 
<pre>
 
<pre>
you have to instantiate the method ``def foo(bar): pass`` in order to use this abstract class
+
you have to instantiate the method `def foo(bar): pass` in order to use this abstract class
 
</pre>
 
</pre>
  
Bad:
+
Good:
 
<pre>
 
<pre>
you have to instantiate the method `def foo(bar): pass` in order to use this abstract class
+
you have to instantiate the method ``def foo(bar): pass`` in order to use this abstract class
 
</pre>
 
</pre>
  
Line 92: Line 102:
 
Entries in Args section do ''not'' start with bullets, but just with argument names (as any other Napoleon section).
 
Entries in Args section do ''not'' start with bullets, but just with argument names (as any other Napoleon section).
 
Continuation lines should be indented.
 
Continuation lines should be indented.
 +
 +
Bad:
 +
<pre>
 +
Args:
 +
    - foo (int): first argument
 +
    - bar: second argument
 +
    - baz (bool): third argument
 +
</pre>
  
 
Good:
 
Good:
Line 100: Line 118:
 
         long description of what it does
 
         long description of what it does
 
     baz (bool): third argument
 
     baz (bool): third argument
</pre>
 
 
Bad:
 
<pre>
 
Args:
 
    - foo (int): first argument
 
    - bar: second argument
 
    - baz (bool): third argument
 
 
</pre>
 
</pre>
  
Line 114: Line 124:
 
In Returns section you need to use ":" carefully as, if present, it will be interpreted as a separator between return type and description. Also, the description of return value should not start on the same line of "Returns:", but on the subsequent one, indented.
 
In Returns section you need to use ":" carefully as, if present, it will be interpreted as a separator between return type and description. Also, the description of return value should not start on the same line of "Returns:", but on the subsequent one, indented.
  
Good:
+
Bad:
 
<pre>
 
<pre>
 
Returns:
 
Returns:
     this works, a dict with keys
+
     this does not work (colon will be interpreted as type/desc separator), a dict with keys:
  
 
     - foo
 
     - foo
 
     - bar
 
     - bar
</pre>
+
<pre>
  
Bad:
+
Good:
 
<pre>
 
<pre>
 
Returns:
 
Returns:
     this does not work, a dict with keys:
+
     this works (there is no colon) a dict with keys
  
 
     - foo
 
     - foo
 
     - bar
 
     - bar
<pre>
+
</pre>
 
+
 
 
Good:
 
Good:
 
<pre>
 
<pre>
 
Returns:
 
Returns:
     dicts: this works again, a dict with keys:
+
     dict: this works again (''first'' colon identifies the type) a dict with keys:
  
 
     - foo
 
     - foo

Revision as of 13:55, 7 September 2017

Here is a list of common gotchas when formatting Python docstrings for Sphinx and the Napoleon style.

Sphinx

Lists

All sorts of lists require an empty line before the first bullet and after the last one, to be properly interpreted as list. No indentation is required for list elements w.r.t. surrounding text, and line continuations should be indented like the first character after the bullet

Bad:

this is a bad example that will not be interpreted as a list
preceding text
- foo
- bar
- baz
following text

Good:

this is some text preceding the list

- foo
- bar
- baz
- this is a rather long-ish paragraph inserted in the list
  with line continuation
- qux

this is some text following the list

Bad:

- foo
- nested lists also requires empty lines, but they are missing here
  - inner list 1
  - inner list 2
- outer list continues here

Good:

surrounding text

- foo
- nested lists also requires empty lines

  - inner list 1
  - inner list 2

- outer list continues here

surrounding text

Verbatim source code

Verbatim code blocks, e.g., for code examples, requires double colon at the end of a line, then an empty line, and then the code block itself, indented:

Bad:

This does not work as there is a single column and no empty line before code:
    def foo(bar, baz):
        qux = bar + baz

        return qux

Good:

a nice example of python code follows::

    def foo(bar, baz):
        qux = bar + baz

        return qux

here we can restart text flow

Inline code samples use double backquotes, and not single ones.

Bad:

you have to instantiate the method `def foo(bar): pass` in order to use this abstract class

Good:

you have to instantiate the method ``def foo(bar): pass`` in order to use this abstract class

Napoleon

Docstring sections

See the list of docstring sections supported by Napoleon. Everything else will not be typeset with a dedicated heading, you will have to do so explicitly using reStructuredText markup.

Args

Entries in Args section do not start with bullets, but just with argument names (as any other Napoleon section). Continuation lines should be indented.

Bad:

Args:
    - foo (int): first argument
    - bar: second argument
    - baz (bool): third argument

Good:

Args:
    foo (int): first argument
    bar: second argument, which happen to have a fairly
        long description of what it does
    baz (bool): third argument

Returns

In Returns section you need to use ":" carefully as, if present, it will be interpreted as a separator between return type and description. Also, the description of return value should not start on the same line of "Returns:", but on the subsequent one, indented.

Bad:

Returns:
    this does not work (colon will be interpreted as type/desc separator), a dict with keys:

    - foo
    - bar
<pre>

Good:
<pre>
Returns:
    this works (there is no colon) a dict with keys

    - foo
    - bar

Good:

Returns:
    dict: this works again (''first'' colon identifies the type) a dict with keys:

    - foo
    - bar

Bad:

Returns: this is not good either, you need to start a paragraph

See also