Search code examples
pythonpython-sphinxrestructuredtext

RST codeblock with internal indentation


I'm using Sphinx to document methods that return dictionaries.

def do_stuff(foo, bar):
    """Do some stuff

    :param foo: I'm an argument
    :param bar: So am I

    :return: dict::

        {
          "success": (Boolean) True if stuff was done
          "meta": {
              "aaa": A nested return value
              "bbb": Another nested return value
          }
        }
    """

The formatting for the dictionary object looks off:

enter image description here

As best I can tell, the problem is that string literals in RST are expected to be indented at the same level.

Is there a workaround for this?


Solution

  • Indenting the :: and then further indenting the dictionary, got it recognized as a codeblock. This was using .. autofunction:: do_stuff and sphinx 1.5.2.

    def do_stuff(foo, bar):
        """Do some stuff
    
        :param foo: I'm an argument
        :param bar: So am I
    
        :return: dict
            ::
                {
                  "success": (Boolean) True if stuff was done
                  "meta": {
                      "aaa": A nested return value
                      "bbb": Another nested return value
                  }
                }
        """
    

    When I separated out the dict part, I had to format like so.

    def do_stuff(foo, bar):
        """Do some stuff
    
        :param foo: I'm an argument
        :param bar: So am I
        :rtype: dict
        :return:
            ::
    
                {
                  "success": (Boolean) True if stuff was done
                  "meta": {
                      "aaa": A nested return value
                      "bbb": Another nested return value
                  }
                }
        """
    

    enter image description here