Plugins » Sphinx

Makes it pos­sible to doc­u­ment APIs with the Py­thon doc theme us­ing ex­tern­al files in a way sim­il­ar to Sphinx.

How to use

Pel­ic­an

Down­load the m/sphinx.py file, put it in­clud­ing the m/ dir­ect­ory in­to one of your PLUGIN_PATHS and add m.sphinx pack­age to your PLUGINS in pelicanconf.py. The M_SPHINX_INVENTORIES op­tion is de­scribed in the Links to ex­tern­al Sphinx doc­u­ment­a­tion sec­tion be­low.

PLUGINS += ['m.sphinx']
M_SPHINX_INVENTORIES = [...]

Py­thon doc theme

List the plu­gin in your PLUGINS. The M_SPHINX_INVENTORIES con­fig­ur­a­tion op­tion is de­scribed in the Links to ex­tern­al Sphinx doc­u­ment­a­tion sec­tion be­low, ad­di­tion­ally it sup­ports also the in­verse — sav­ing in­tern­al sym­bols to a file to be linked from else­where, see Cre­at­ing an In­ter­sphinx in­vent­ory file for a de­scrip­tion of the M_SPHINX_INVENTORY_OUTPUT op­tion.

PLUGINS += ['m.sphinx']
M_SPHINX_INVENTORIES = [...]
M_SPHINX_INVENTORY_OUTPUT = 'objects.inv'
M_SPHINX_PARSE_DOCSTRINGS = False

Cre­at­ing an In­ter­sphinx in­vent­ory file

In the Py­thon doc theme, the M_SPHINX_INVENTORY_OUTPUT op­tion can be used to pro­duce an In­ter­sphinx in­vent­ory file — ba­sic­ally an in­verse of M_SPHINX_INVENTORIES. Set it to a fi­le­name and the plu­gin will fill it with all names present in the doc­u­ment­a­tion. Com­monly, Sphinx pro­jects ex­pect this file to be named objects.inv and loc­ated in the doc­u­ment­a­tion root, so do­ing the fol­low­ing will en­sure it can be eas­ily used:

M_SPHINX_INVENTORY_OUTPUT = 'objects.inv'

Mod­ule, class, enum, func­tion, prop­erty and data docs

In the Py­thon doc theme, the .. py:module::, .. py:class::, .. py:enum::, .. py:enumvalue::, .. py:function::, .. py:property:: and .. py:data:: dir­ect­ives provide a way to sup­ply mod­ule, class, enum, func­tion / meth­od, prop­erty and data doc­u­ment­a­tion con­tent.

Dir­ect­ive op­tion is the name to doc­u­ment, dir­ect­ive con­tents are the ac­tu­al con­tents; in ad­di­tion all dir­ect­ives ex­cept .. py:enumvalue:: have an :summary: op­tion that can over­ride the doc­string ex­trac­ted us­ing in­spec­tion. No re­stric­tions are made on the con­tents, it’s also pos­sible to make use of any ad­di­tion­al plu­gins in the markup. Ex­ample:

.. py:module:: mymodule
    :summary: A top-level module.

    This is the top-level module.

    Usage
    -----

    .. code:: pycon

        >>> import mymodule
        >>> mymodule.foo()
        Hello world!

.. py:data:: mymodule.ALMOST_PI
    :summary: :math:`\pi`, but *less precise*.

By de­fault, un­like doc­strings, the :summary: is in­ter­preted as reST, which means you can keep the doc­string format­ting sim­pler (for dis­play in­side IDEs or via the built­in help()), while sup­ply­ing an al­tern­at­ive and more com­plex-format­ted sum­mary for the ac­tu­al rendered docs. It’s how­ever pos­sible to en­able reST pars­ing for doc­strings as well — see Us­ing parsed doc­strings be­low.

Mod­ules

The .. py:module:: dir­ect­ive doc­u­ments a Py­thon mod­ule. In ad­di­tion, the dir­ect­ive sup­ports a :data name: op­tion for con­veni­ent doc­u­ment­ing of mod­ule-level data. The op­tion is equi­val­ent to filling out just a :summary: of the .. py:data:: dir­ect­ive de­scribed be­low.

.. py:module:: math
    :summary: Common mathematical functions
    :data pi:   The value of :math:`\pi`
    :data tau:  The value of :math:`\tau`. Or :math:`2 \pi`.

    This module defines common mathematical functions and constants as
    defined by the C standard.

Classes

Use .. py:class:: for doc­u­ment­ing classes. Sim­il­arly to mod­ule docs, this dir­ect­ive sup­ports an ad­di­tion­al :data name: op­tion for doc­u­ment­ing class-level data as well as :property name: for prop­er­ties. Both of those are equi­val­ent to filling out a :summary: of the .. py:data:: / .. py:property:: dir­ect­ives de­scribed be­low.

.. py:class:: mymodule.MyContainer
    :summary: A container of key/value pairs
    :property size: Number of entries in the container

    Provides a key/value storage with :math:`\mathcal{O}(\log{}n)`-complexity
    access.

Enums and enum val­ues

Use .. py:enum:: for doc­u­ment­ing enums. Val­ues can be doc­u­mented either us­ing the .. py:enumvalue:: dir­ect­ive, or in case of short de­scrip­tions, con­veni­ently dir­ectly in the .. py:enum:: dir­ect­ive via :value name: op­tions. Ex­ample:

.. py:enum:: mymodule.MemoryUsage
    :summary: Specifies memory usage configuration
    :value LOW: Optimized for low-memory big-storage devices, such as
        refrigerators.
    :value HIGH: The memory usage will make you angry.

.. py:enumvalue:: mymodule.MemoryUsage.DEFAULT

    Default memory usage. Behavior depends on platform:

    -   On low-memory devices such as refrigerators equivalent to :ref:`LOW`.
    -   On high-end desktop PCs, this is equivalent to :ref:`HIGH`.
    -   On laptops, this randomly chooses between the two values based
        Murphy's law. Enjoy the battery life when you need it the least.

Func­tions

The .. py:function:: dir­ect­ive sup­ports ad­di­tion­al op­tions — :param name: for doc­u­ment­ing para­met­ers, :raise name: for doc­u­ment­ing raised ex­cep­tions and :return: for doc­u­ment­ing the re­turn value. It’s al­lowed to have either none or all para­met­ers doc­u­mented (the self para­met­er can be omit­ted), hav­ing them doc­u­mented only par­tially or doc­u­ment­ing para­met­ers that are not present in the func­tion sig­na­ture will cause a warn­ing. Doc­u­ment­ing one para­met­er mul­tiple times causes a warn­ing, on the oth­er hand list­ing one ex­cep­tion mul­tiple times is a val­id use case. Ex­ample:

.. py:function:: mymodule.MyContainer.add
    :summary: Add a key/value pair to the container
    :param key:                 Key to add
    :param value:               Corresponding value
    :param overwrite_existing:  Overwrite existing value if already present
        in the container
    :raise ValueError:  If the key type is not hashable
    :return:                    The inserted tuple or the existing
        key/value pair in case :p:`overwrite_existing` is not set

    The operation has a :math:`\mathcal{O}(\log{}n)` complexity.

For over­loaded func­tions (such as those com­ing from py­bind11), it’s pos­sible to spe­cify the full sig­na­ture to dis­tin­guish between par­tic­u­lar over­loads. Dir­ect­ives with the full sig­na­ture have a pri­or­ity, if no sig­na­ture matches giv­en func­tion, a sig­na­ture-less dir­ect­ive is searched for as a fall­back. Ex­ample:

.. py:function:: magnum.math.dot(a: magnum.Complex, b: magnum.Complex)
    :summary: Dot product of two complex numbers

.. py:function:: magnum.math.dot(a: magnum.Quaternion, b: magnum.Quaternion)
    :summary: Dot product of two quaternions

.. py:function:: magnum.math.dot
    :summary: Dot product

    .. this documentation will be used for all other overloads

Prop­er­ties

Use .. py:property:: for doc­u­ment­ing prop­er­ties. This dir­ect­ive sup­ports the :raise name: op­tion sim­il­arly as for func­tions, plus the usu­al :summary:. For con­veni­ence, prop­er­ties that have just a sum­mary can be also doc­u­mented dir­ectly in the en­clos­ing .. py:class:: dir­ect­ive as shown above.

.. py:property:: mymodule.MyContainer.size
    :summary: Number of entries in the container

    You can also use ``if not container`` for checking if the container is
    empty.

Data

Use .. py:data:: for doc­u­ment­ing mod­ule-level and class-level data. This dir­ect­ive doesn’t sup­port any ad­di­tion­al op­tions be­sides :summary:. For con­veni­ence, data that have just a sum­mary can be also doc­u­mented dir­ectly in the en­clos­ing .. py:module:: / .. py:class:: dir­ect­ive as shown above.

.. py:data:: math.pi
    :summary: The value of :math:`\tau`. Or :math:`2 \pi`.

    They say `pi is wrong <https://tauday.com/>`_.

Us­ing parsed doc­strings

By de­fault, doc­strings are treated by the Py­thon doc gen­er­at­or as plain text and only ex­tern­ally-sup­plied docs are parsed. This is done be­cause, for ex­ample in Py­thon stand­ard lib­rary, em­bed­ded doc­strings are of­ten very terse without any markup and full docs are ex­tern­al. If you want the doc­strings to be parsed, en­able the M_SPHINX_PARSE_DOCSTRINGS op­tion. Com­pared to the dir­ect­ives above, there’s only one dif­fer­ence — in­stead of a :summary: op­tion, the first para­graph is taken as a sum­mary, the second para­graph as the op­tion list (if it con­tains op­tion fields) and the rest as doc­u­ment­a­tion con­tent. Con­tinu­ing with the .. py:function:: ex­ample above, em­bed­ded in a doc­string it would look like this in­stead:

def add(self, key, value, *, overwrite_existing=False):
    """Add a key/value pair to the container

    :param key:                 Key to add
    :param value:               Corresponding value
    :param overwrite_existing:  Overwrite existing value if already present
        in the container
    :return:                    The inserted tuple or the existing
        key/value pair in case ``overwrite_existing`` is not set

    The operation has a :math:`\mathcal{O}(\log{}n)` complexity.
    """