Skip to content

BUG: Plot(s) not rendered if docstring ends with normal text after show command #162

Closed
@lagru

Description

@lagru

I'm not entirely sure whether this is a bug in numpydoc or matplotlib.sphinxext (where the error message happens). This is my best guess right now and I hope someone with more insight into the inner workings of both can pinpoint the bug.

Bug description

When using the plot directive implicitly in docstrings sometimes the plots are not included in the rendered HTML documentation. This happens when the docstring ends with a normal text line after the >>> plt.show() statement. It seems like The cause lies in how SphinxDocString parses the input (see snippets below). In this special case the functions in plot_directive.py won't prefix the last line with #. As a consequence the last line raises an exception on line 524 when calling six.exec_(code, ns) in plot_directive.py (function run_code).

Reproduction

I have created a minimal dummy project to demonstrate the behavior:
https://github.com/lagru/docstring-plot-bug-demo

As stated the possible cause might lie here:

from numpydoc.docscrape_sphinx import SphinxDocString
doc = SphinxDocString("""
Examples
--------
>>> import matplotlib.pyplot as plt
>>> plt.plot([1,2,3],[4,5,6])
>>> plt.show()

Some text.
""", config=dict(use_plots=True))
print(doc)

which yields

.. rubric:: Examples

.. plot::

    >>> import matplotlib.pyplot as plt
    >>> plt.plot([1,2,3],[4,5,6])
    >>> plt.show()
    
    Some text.

However I would expect this output:

.. rubric:: Examples

.. plot::

    >>> import matplotlib.pyplot as plt
    >>> plt.plot([1,2,3],[4,5,6])
    >>> plt.show()
    
Some text.

Note how the last line isn't part of the plot:: directive in contrast to the actual output.

Other info

Setup
OS: Manjaro 17.1.7, x86_64 Linux 4.14.30-1-MANJARO
python 3.6.5
numpydoc 0.7.0
sphinx 1.7.2
matplotlib 2.2.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions