Search code examples
pythonpython-sphinxdocstring

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.


Solution

  • 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
            ...
        '''