readthedocs / sphinx-autoapi

A new approach to API documentation in Sphinx.
https://sphinx-autoapi.readthedocs.io/
MIT License
415 stars 126 forks source link

autoapi complains about code indentation in docstrings #359

Closed mfncooper closed 1 year ago

mfncooper commented 1 year ago

If I have a docstring that includes a code example, and that example includes a line that needs to wrap, autoapi will output the following: "WARNING: Unexpected indentation.". It doesn't matter what indentation you try to use for the wrapped code; autoapi will always complain. (Or at least, I haven't been able to find anything that autoapi will accept, after trying all the usual "standards" for wrapping code, and some random other ones too.) For example:

"""
Module X

Code example:
    foo = make_thing()
    foo.take_some_action(some_long_arg(
        some_other_long_arg()))
"""

autoapi will issue the warning for the indentation of the some_other_long_arg() line.

Oddly, if you remove the first line (i.e. the foo = line), such that the long, wrapped statement is the only statement, autoapi does not complain about the indentation. But as soon as there are other lines of code with the original base indentation, autoapi issues the warning, and there appears to be no way to placate it.

(Also, I notice that this warning is in bright red, while, for example, duplicate object warnings are in muted red. I've no idea why they're in different colors, or whether or not that has significance.)

mfncooper commented 1 year ago

Never mind. It seems I need to put .. code-block:: python in the docstring to get code to appear properly. Ugly when reading the code itself, and messy when someone uses Python's help() function, but it does the right thing with Sphinx / autoapi.