keewis / blackdoc

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

blackdoc fails to format docstrings in docstrings #41

Closed seisman closed 4 years ago

seisman commented 4 years ago

Example code:

def myfunc():
    """Docstring for the function myfunc.

    Here are some docstrings.

    Examples
    --------

    >>> def myfunc2(arg1, arg2):
    ...     '''Docstring for function myfunc2 in docstring
    ...
    ...     More description of the function.
    ...     '''
    ...     pass
    """
    pass

Error message:

blackdoc test.py
error: cannot format test.py: Cannot parse: 10:4: EOF in multi-line string: def myfunc2(arg1, arg2):
Oh no! 💥 💔 💥
1 file fails to reformat.
keewis commented 4 years ago

thanks for the report, @seisman.

There are actually two bugs triggered by your example:

  1. the continuation prompt was not detected for the empty line in the doctest function's docstring (so checking for ... failed), resulting in the error you saw
  2. black replaces ''' with """, which would result in broken code. Fortunately, this is partially hidden by the first bug.

I'm working on a fix for both.

seisman commented 4 years ago

I can confirm that it works. Thanks for your quick fix!