reStructuredText markup

This document describes the custom reStructuredText markup introduced by Sphinx to support Python documentation and how it should be used.

Quick reference

This table summarizes which markup should be used for some commonly used elements:

Element

Markup

See also

arguments/parameters

*arg*

Inline markup

variables/literals/code

``foo``, ``42``, ``len(s) - 1``

Inline markup

True/False/None

``True``, ``False``, ``None``

Inline markup

function definitions

.. function:: print(*args)

Directives

function references

:func:`print`

Roles

attribute definitions

.. attribute: `attr-name`

Information units

attribute references

:attr:`attr-name`

Roles

reference labels

.. _label-name:

Cross-linking markup

internal references

:ref:`label-name`

Cross-linking markup

external links

`Link text <https://example.com>`__

Hyperlinks

roles w/ custom text

:role:`custom text <target>`

Roles

roles w/ only last part

:role:`~hidden.hidden.visible`

Roles

roles w/o link

:role:`!target`

Roles

issues

:gh:`ID`, :issue:`ID`

Roles

CPython source

:source:`PATH`

Roles

comments

.. a comment

Comments

reStructuredText primer

This section is a brief introduction to reStructuredText (reST) concepts and syntax, intended to provide authors with enough information to author documents productively. Since reST was designed to be a simple, unobtrusive markup language, this will not take too long.

See also

The authoritative reStructuredText User Documentation.

Use of whitespace

All reST files use an indentation of 3 spaces; no tabs are allowed. The maximum line length is 80 characters for normal text, but tables, deeply indented code samples and long links may extend beyond that. Code example bodies should use normal Python 4-space indentation.

Make use of multiple blank lines where applicable to clarify the structure of the reST file. Extra blank lines help group sections together to make the organization of the file clearer.

A sentence-ending period may be followed by one or two spaces. While reST ignores the second space, it is customarily put in by some users, for example to aid Emacs’ auto-fill mode.

Paragraphs

The paragraph is the most basic block in a reST document. Paragraphs are simply chunks of text separated by one or more blank lines. As in Python, indentation is significant in reST, so all lines of the same paragraph must be left-aligned to the same level of indentation.

Inline markup

The standard reST inline markup is quite simple: use

  • one asterisk: *text* for emphasis (italics),

  • two asterisks: **text** for strong emphasis (boldface), and

  • backquotes: ``text`` for code samples, variables, and literals.

If asterisks or backquotes appear in running text and could be confused with inline markup delimiters, they have to be escaped with a backslash.

Be aware of some restrictions of this markup:

  • it may not be nested,

  • content may not start or end with whitespace: * text* is wrong,

  • it must be separated from surrounding text by non-word characters. Use a backslash escaped space to work around that: thisis\ *one*\ word.

These restrictions may be lifted in future versions of the docutils.

reST also allows for custom “interpreted text roles”, which signify that the enclosed text should be interpreted in a specific way. Sphinx uses this to provide semantic markup and cross-referencing of identifiers, as described in the appropriate section. The general syntax is :rolename:`content`.

Lists and quotes

List markup is natural: just place an asterisk at the start of a paragraph and indent properly. The same goes for numbered lists; they can also be automatically numbered using a # sign:

* This is a bulleted list.
* It has two items, the second
  item uses two lines.

1. This is a numbered list.
2. It has two items too.

#. This is a numbered list.
#. It has two items too.

Nested lists are possible, but be aware that they must be separated from the parent list items by blank lines:

* this is
* a list

  * with a nested list
  * and some subitems

* and here the parent list continues

Definition lists are created as follows:

term (up to a line of text)
   Definition of the term, which must be indented

   and can even consist of multiple paragraphs

next term
   Description.

Paragraphs are quoted by just indenting them more than the surrounding paragraphs.

Source code

Literal code blocks are introduced by ending a paragraph with the special marker ::. The literal block must be indented:

This is a normal text paragraph. The next paragraph is a code sample::

   It is not processed in any way, except
   that the indentation is removed.

   It can span multiple lines.

This is a normal text paragraph again.

The handling of the :: marker is smart:

  • If it occurs as a paragraph of its own, that paragraph is completely left out of the document.

  • If it is preceded by whitespace, the marker is removed.

  • If it is preceded by non-whitespace, the marker is replaced by a single colon.

That way, the second sentence in the above example’s first paragraph would be rendered as “The next paragraph is a code sample:”.

Sections

Section headers are created by underlining (and optionally overlining) the section title with a punctuation character, at least as long as the text:

=================
This is a heading
=================

Normally, there are no heading levels assigned to certain characters as the structure is determined from the succession of headings. However, for the Python documentation, here is a suggested convention:

  • # with overline, for parts

  • * with overline, for chapters

  • =, for sections

  • -, for subsections

  • ^, for subsubsections

  • ", for paragraphs

Explicit markup

“Explicit markup” is used in reST for most constructs that need special handling, such as footnotes, specially-highlighted paragraphs, comments, and generic directives.

An explicit markup block begins with a line starting with .. followed by whitespace and is terminated by the next paragraph at the same level of indentation. (There needs to be a blank line between explicit markup and normal paragraphs. This may all sound a bit complicated, but it is intuitive enough when you write it.)

Directives

A directive is a generic block of explicit markup. Besides roles, it is one of the extension mechanisms of reST, and Sphinx makes heavy use of it.

Basically, a directive consists of a name, arguments, options and content. (Keep this terminology in mind, it is used in the next section describing custom directives.) Looking at this example,

.. function:: foo(x)
              foo(y, z)
   :bar: no

   Return a line of text input from the user.

function is the directive name. It is given two arguments here, the remainder of the first line and the second line, as well as one option bar (as you can see, options are given in the lines immediately following the arguments and indicated by the colons).

The directive content follows after a blank line and is indented relative to the directive start.

Footnotes

For footnotes, use [#]_ to mark the footnote location, and add the footnote body at the bottom of the document after a “Footnotes” rubric heading, like so:

Lorem ipsum [#]_ dolor sit amet ... [#]_

.. rubric:: Footnotes

.. [#] Text of the first footnote.
.. [#] Text of the second footnote.

You can also explicitly number the footnotes for better context.

Comments

Every explicit markup block (starting with .. ) which isn’t a valid markup construct is regarded as a comment:

.. This is a comment

Source encoding

Since the easiest way to include special characters like em dashes or copyright signs in reST is to directly write them as Unicode characters, one has to specify an encoding:

All Python documentation source files must be in UTF-8 encoding, and the HTML documents written from them will be in that encoding as well.

Gotchas

There are some problems one commonly runs into while authoring reST documents:

  • Separation of inline markup: As said above, inline markup spans must be separated from the surrounding text by non-word characters, you have to use an escaped space to get around that.

Typographic conventions

Big O notation

Big O notation is used to describe the performance of algorithms.

Use italics for the big O and variables. For example:

reStructuredText

Rendered

*O*\ (1)

O(1)

*O*\ (log *n*)

O(log n)

*O*\ (*n*)

O(n)

*O*\ (*n* log *n*)

O(n log n)

*O*\ (*n*\ :sup:`2`)

O(n2)

Additional markup constructs

Sphinx adds a lot of new directives and interpreted text roles to standard reST markup. This section contains the reference material for these facilities. Documentation for “standard” reST constructs is not included here, though they are used in the Python documentation.

Note

This is just an overview of Sphinx’ extended markup capabilities; full coverage can be found in its own documentation.

Module-specific markup

The markup described in this section is used to provide information about a module being documented. Each module should be documented in its own file. Normally this markup appears after the title heading of that file; a typical file might start like this:

:mod:`!parrot` -- Dead parrot access
====================================

.. module:: parrot
   :synopsis: Analyze and reanimate dead parrots.
module

This directive marks the beginning of the description of a module, package, or submodule. The name should be fully qualified (that is, including the package name for submodules).

The synopsis option should consist of one sentence describing the module’s purpose – it is currently only used in the Global Module Index.

The deprecated option can be given (with no value) to mark a module as deprecated; it will be designated as such in various locations then.

Note

It is important to make the section title of a module-describing file meaningful since that value will be inserted in the table-of-contents trees in overview files.

Information units

There are a number of directives used to describe specific features provided by modules. Each directive requires one or more signatures to provide basic information about what is being described, and the content should be the description. The basic version makes entries in the general index; if no index entry is desired, you can give the directive option flag :noindex:. The following example shows all of the features of this directive type:

.. function:: spam(eggs)
              ham(eggs)
   :noindex:

   Spam or ham the foo.

The signatures of object methods or data attributes should not include the class name, but be nested in a class directive. The generated files will reflect this nesting, and the target identifiers (for HTML output) will use both the class and method name, to enable consistent cross-references. If you describe methods belonging to an abstract protocol such as context managers, use a class directive with a (pseudo-)type name too to make the index entries more informative.

The directives are:

c:function

Describes a C function. The signature should be given as in C, for example:

.. c:function:: PyObject* PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)

This is also used to describe function-like preprocessor macros. The names of the arguments should be given so they may be used in the description.

Note that you don’t have to backslash-escape asterisks in the signature, as it is not parsed by the reST inliner.

c:member

Describes a C struct member. Example signature:

.. c:member:: PyObject* PyTypeObject.tp_bases

The text of the description should include the range of values allowed, how the value should be interpreted, and whether the value can be changed. References to structure members in text should use the member role.

c:macro

Describes a “simple” C macro. Simple macros are macros which are used for code expansion, but which do not take arguments so cannot be described as functions. This is not to be used for simple constant definitions. Examples of its use in the Python documentation include PyObject_HEAD and Py_BEGIN_ALLOW_THREADS.

c:type

Describes a C type. The signature should just be the type name.

c:var

Describes a global C variable. The signature should include the type, such as:

.. c:var:: PyObject* PyClass_Type
data

Describes global data in a module, including both variables and values used as “defined constants.” Class and object attributes are not documented using this directive.

exception

Describes an exception class. The signature can, but need not include parentheses with constructor arguments.

function

Describes a module-level function. The signature should include the parameters, enclosing optional parameters in brackets. Default values can be given if it enhances clarity. For example:

.. function:: repeat([repeat=3[, number=1000000]])

Object methods are not documented using this directive. Bound object methods placed in the module namespace as part of the public interface of the module are documented using this, as they are equivalent to normal functions for most purposes.

The description should include information about the parameters required and how they are used (especially whether mutable objects passed as parameters are modified), side effects, and possible exceptions. A small example may be provided.

coroutinefunction

Describes a module-level coroutine. The description should include similar information to that described for function.

decorator

Describes a decorator function. The signature should not represent the signature of the actual function, but the usage as a decorator. For example, given the functions

def removename(func):
    func.__name__ = ''
    return func

def setnewname(name):
    def decorator(func):
        func.__name__ = name
        return func
    return decorator

the descriptions should look like this:

.. decorator:: removename

   Remove name of the decorated function.

.. decorator:: setnewname(name)

   Set name of the decorated function to *name*.

To link to a decorator that is marked up with this directive, use the :deco: role.

class

Describes a class. The signature can include parentheses with parameters which will be shown as the constructor arguments.

attribute

Describes an object data attribute. The description should include information about the type of the data to be expected and whether it may be changed directly. This directive should be nested in a class directive, like in this example:

.. class:: Spam

   Description of the class.

   .. attribute:: ham

      Description of the attribute.

Refer to an attribute using the :attr: role:

Use the :attr:`ham` attribute to spam the eggs.

If is also possible to document an attribute outside of a class directive, for example if the documentation for different attributes and methods is split in multiple sections. The class name should then be included explicitly:

.. attribute:: Spam.eggs
method

Describes an object method. The parameters should not include the self parameter. The description should include similar information to that described for function. This directive should be nested in a class directive, like in the example above.

coroutinemethod

Describes an object coroutine method. The parameters should not include the self parameter. The description should include similar information to that described for function. This directive should be nested in a class directive.

decoratormethod

Same as decorator, but for decorators that are methods.

Refer to a decorator method using the :meth: role.

staticmethod

Describes an object static method. The description should include similar information to that described for function. This directive should be nested in a class directive.

classmethod

Describes an object class method. The parameters should not include the cls parameter. The description should include similar information to that described for function. This directive should be nested in a class directive.

abstractmethod

Describes an object abstract method. The description should include similar information to that described for function. This directive should be nested in a class directive.

opcode

Describes a Python bytecode instruction.

option

Describes a Python command line option or switch. Option argument names should be enclosed in angle brackets. Example:

.. option:: -m <module>

   Run a module as a script.
envvar

Describes an environment variable that Python uses or defines.

There is also a generic version of these directives:

describe

This directive produces the same formatting as the specific ones explained above but does not create index entries or cross-referencing targets. It is used, for example, to describe the directives in this document. Example:

.. describe:: opcode

   Describes a Python bytecode instruction.

Showing code examples

Examples of Python source code or interactive sessions are represented using standard reST literal blocks. They are started by a :: at the end of the preceding paragraph and delimited by indentation.

Representing an interactive session requires including the prompts and output along with the Python code. No special markup is required for interactive sessions. After the last line of input or output is presented, there should be no trailing prompt. An example of correct usage is:

>>> 1 + 1
2

Syntax highlighting is handled in a smart way:

  • There is a “highlighting language” for each source file. By default, this is 'python' as the majority of files will have to highlight Python snippets.

  • Within Python highlighting mode, interactive sessions are recognized automatically and highlighted appropriately.

  • The highlighting language can be changed using the highlight directive, used as follows:

    .. highlight:: c
    

    This language is used until the next highlight directive is encountered.

  • The code-block directive can be used to specify the highlight language of a single code block, for example:

    .. code-block:: c
    
       #include <stdio.h>
    
       void main() {
           printf("Hello world!\n");
       }
    
  • The values normally used for the highlighting language are:

    • python (the default)

    • c

    • rest

    • none (no highlighting)

  • If highlighting with the current language fails, the block is not highlighted in any way.

Longer displays of verbatim text may be included by storing the example text in an external file containing only plain text. The file may be included using the literalinclude directive. For example, to include the Python source file example.py, use:

.. literalinclude:: example.py

The file name is relative to the current file’s path. Documentation-specific include files should be placed in the Doc/includes subdirectory.

Note

There is a standard include directive, but it raises errors if the file is not found. literalinclude is preferred because it only emits a warning instead of raising an error.

Roles

As previously mentioned, Sphinx uses interpreted text roles of the form :rolename:`content` to insert semantic markup in documents.

In the CPython documentation, there are a couple common cases where simpler markup should be used:

  • *arg* (rendered as arg) for function and method arguments.