numpydoc icon indicating copy to clipboard operation
numpydoc copied to clipboard

Don't force 'returns' and 'raises' sections to be definition lists

Open takluyver opened this issue 10 years ago • 8 comments

In IPython, the Returns and Raises sections of our docstrings are often a single block of text, not a definition list. In particular, where functions return a single value, it seems awkward to give that value a name in the docstring. However, numpydoc assumes that these sections are always a rst definition list, which results in wonky formatting.

Assuming that other projects want to keep the definition list functionality, I see three ways to achieve this:

  1. Detect whether the contents look like a definition list (at least two lines, with the second line indented more than the first), and parse the section accordingly.
  2. Have some attribute on the object that tells numpydoc to parse the docstring differently
  3. Allow alternative section headings (e.g. 'Return value') which mean the same thing but are parsed differently.

I think 1 feels like the best solution - it's surprising to me that the format of these fields is forced, and I don't think it's too much magic to allow different formats. I'm happy to work on this, but I wanted to discuss it before I started.

takluyver avatar May 07 '14 23:05 takluyver

In [0] the case of the single return value is discussed. You still have to provide a return type, don't you?

[0] https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt

stefanv avatar May 07 '14 23:05 stefanv

Yes, but we normally write it in prose rather than a definition list with the type as the term. An example I have open:

    Returns
    -------
    The connection dictionary of the current kernel, as string or dict,
    depending on `unpack`.

We do this even though numpydoc doesn't currently render it nicely, because we don't often check the HTML docs when writing docstrings. So I'd rather relax the formatting restriction a little to make this work than try to convince everyone on the team to format docstrings differently.

takluyver avatar May 08 '14 00:05 takluyver

I guess the question is whether this is any less clear or elegant:

Returns
-------
string or dict
    The connection dictionary of the kernel.  The type depends on `unpack`.

If you feel it is significantly better and can motivate it, then we should rather update the numpy documentation format.

stefanv avatar May 08 '14 00:05 stefanv

To my mind, it's less elegant - I'm not defining 'string or dict' as a description of what I'm returning. But I really don't want to start wrangling over documentation standards, I just want to make this tool a bit more flexible so it can cope with what's already the defacto standard in IPython. Maybe it should have a 'strict' parameter that we can turn off if we don't want to exactly follow the numpy doc format.

takluyver avatar May 08 '14 01:05 takluyver

I'll let @pv and @rgommers chime in.

stefanv avatar May 09 '14 17:05 stefanv

@takluyver's suggestion of a strict default that can be relaxed by a project is fine with me.

I don't think we want to change the numpy documentation standard itself - consistency is useful.

rgommers avatar May 10 '14 10:05 rgommers

"strict" is so wide, though, perhaps we can add an "allow" parameter that takes a tuple of strings, which may contain things as "order", "custom_return", etc.

stefanv avatar May 10 '14 13:05 stefanv

how do we detect this case? By the fact that it's multiline? That seems awkward...

jnothman avatar Nov 01 '17 22:11 jnothman