Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I insert highlight or code-block into Sphinx-style docstrings?

For example:

def foo():
    '''
    .. highlight:: python
    import sys
    '''

Doesn't produce desired output (it prints the word "highlight" verbatim and doesn't format the following code in any special way). Same happens for code-block.

I tried different indentation etc. No matter what, generator succeeds with roughly the same, but not the desired output.

like image 377
wvxvw Avatar asked Oct 23 '17 14:10

wvxvw


People also ask

How do you use a sphinx code block?

Literal code blocks (ref) are introduced by ending a paragraph with the special marker :: . The literal block must be indented (and, like all paragraphs, separated from the surrounding ones by blank lines): This is a normal text paragraph.

How to format docstring for Sphinx?

The Sphinx docstring formatA pair of :param: and :type: directive options must be used for each parameter we wish to document. The :raises: option is used to describe any errors that are raised by the code, while the :return: and :rtype: options are used to describe any values returned by our code.

What do you write in Docstrings?

For a function, its docstrings should consist of three essential components: a one-sentence summary, parameters (name, type, explanation, and the default value if set), and the return value (type and explanation). If the function can raise exceptions, it should be specified too.

What is code block in Python?

A Python program is constructed from code blocks. A block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block.


1 Answers

Comparing your code with the docs, you are missing indentation and an empty line between the highlight and the actual code. It should be like this:

def foo():
    '''
    .. highlight:: python
    .. code-block:: python

        import sys
        ...
    '''
like image 174
Chen A. Avatar answered Oct 24 '22 23:10

Chen A.