keewis / blackdoc

run black on documentation code snippets
https://blackdoc.readthedocs.io
MIT License
47 stars 4 forks source link

Parsing error when using `code-block` directive #150

Closed jrbourbeau closed 2 years ago

jrbourbeau commented 2 years ago

When using blackdoc (v0.3.7) on this .rst snippet


.. code-block:: python

   >>> import dask.dataframe as dd

I get

❯ python -m blackdoc test.rst
error: cannot format /Users/james/projects/dask/dask/test.rst: Cannot parse: 4:0: >>> import dask.dataframe as dd
Oh no! 💥 💔 💥
1 file fails to reformat.

However, when I update the .rst to


.. ::

   >>> import dask.dataframe as dd

which should be equivalent, blackdoc is happy.

keewis commented 2 years ago

thanks for the report, @jrbourbeau. This should be pretty easy to fix (it's a matter of skipping the rst formatter for code blocks with prompt lines; there's similar code for the ipython prompt), but it might take me some time to create a PR.

keewis commented 2 years ago

Note that

.. ::

    >>> import dask.dataframe as dd

is equivalent to

>>> import dask.dataframe as dd

(i.e. blackdoc does not care about the block for doctest lines... except the bug reported here, of course)

However, this:

.. ::

    import dask.dataframe as dd

or

description::

    import dask.dataframe as dd

will most likely always be ignored, because the language depends on a sphinx setting / a directive somewhere earlier in the document.

In any case, the bug should be fixed by #151.

jrbourbeau commented 2 years ago

Thanks for the fix @keewis! Looking forward to taking this for a spin