Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convention for multi-line type specifications #87

Open
jnothman opened this issue Jan 23, 2017 · 23 comments
Open

Convention for multi-line type specifications #87

jnothman opened this issue Jan 23, 2017 · 23 comments

Comments

@jnothman
Copy link
Member

jnothman commented Jan 23, 2017

Parameter lists allow a specification like:

my_param : type specification
    Description

In some cases we at scikit-learn have had long type specifications that overflow into the next line under line-length conventions. From the perspective of documentation rendering, escaping the newline works:

my_param : a really really really really really really really long type \
        specification
    Description

(This is effective since Python evaluates the string literal and elides the escaped newline at parse time.)

However, PEP257 says:

Use r"""raw triple double quotes""" if you use any backslashes in your docstrings.

So I'm not sure we're doing the right thing. Is there a convention for overflowed type specifications? Should there be one hacked into the numpydoc parser (such as a hanging indent)?

@stefanv
Copy link
Contributor

stefanv commented Jan 24, 2017

I'd be +1 on parsing indentation that matches the first character of the type description, i.e., something like:

my_param : a really really really really really really really long type
           specification
    Description

@jorisvandenbossche
Copy link
Contributor

@stefanv That only gives a problem for one character param names (not possible to distinguish between description / overflow of type specification). But of course one character keywords is also bad practice :-)

Anyway, also +1 on a specific guideline (and parsing enhancement) here, as this is something we also regularly have to deal with in pandas

@stefanv
Copy link
Contributor

stefanv commented Jan 24, 2017 via email

@jorisvandenbossche
Copy link
Contributor

Also that can give dubious cases / conflicts with current usage. Eg in

a : type
    Possible values:

    - blabla
    - blabla

Although it looks less nice, the extra indent as proposed by @jnothman seems more robust
The extra indent could also be made flexible in number of spaces, so that in practice you can still align it (apart for 1 char param names).

@stefanv
Copy link
Contributor

stefanv commented Jan 24, 2017

We could also parse back-slashes at ends of lines, which would allow the triple quotes to be used.

@jnothman
Copy link
Member Author

jnothman commented Jan 24, 2017 via email

@jnothman
Copy link
Member Author

jnothman commented Jan 24, 2017 via email

@anntzer
Copy link
Contributor

anntzer commented Sep 8, 2017

Right now numpydoc format is actually valid rst (just with some special interpretation of certain markup constructs), e.g. the parameters field is a definition list where the type is a "classifier" (http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#definition-lists). I would argue that it is worthwhile to keep this property, which end-of-line backslashes do (they simply do not appear in the string itself), whereas the proposed "recognize indentation" syntax does not.

@jnothman
Copy link
Member Author

jnothman commented Sep 9, 2017 via email

@stefanv
Copy link
Contributor

stefanv commented Sep 9, 2017

I have no further ideas on how to handle this one.

@anntzer
Copy link
Contributor

anntzer commented Sep 9, 2017

I think the reference to PEP257 is a misunderstanding, the sentence about raw strings probably refers to the case where the docstring contains a backslash character; but in this case, the backslash only appears in the source as an escape, so I don't think we're violating PEP257...

So I'd just leave things as they are.

@jnothman
Copy link
Member Author

I'm happy to go with Antony's suggestion. The key thing to do here is update the documentation.

At some point should we move the docs to live in numpydoc or even be rendered by sphinx??

@stefanv
Copy link
Contributor

stefanv commented Sep 17, 2017

Do you mean the HOWTO_DOCUMENT.txt etc.? Or write a short manual for numpydoc (that does seem like a worthwhile thing to do).

@jnothman
Copy link
Member Author

jnothman commented Sep 17, 2017 via email

@jnothman
Copy link
Member Author

@jorisvandenbossche noted elsewhere that escaping the newline needs to be done without indentation to render correctly in both html and pydoc... but it then looks poor when directly inspecting the source. I suspect we should change the spec to support a hanging indent.

@anntzer
Copy link
Contributor

anntzer commented Jun 15, 2018

(I'm still in favor of requiring a backslash for the continuation (single backslash if using raw-docstrings, double backslash if not), which has the advantage of avoiding the ambiguity with one-character argument names.)

@stefanv
Copy link
Contributor

stefanv commented Jun 15, 2018

The backslash does seem more explicit.

@jnothman
Copy link
Member Author

I think the backslash would be a bit obscure in pydoc, but if that's consensus....

As long as we require descriptions to be indented 4 spaces, we can require that the type spec continuation be indented at least 5.

@anntzer
Copy link
Contributor

anntzer commented Jun 16, 2018

I don't see why this would look "obscure" in pydoc (and I use pydoc very regularly). Perhaps inelegant at worst, but I don't think it's particularly bad either.

@r-owen
Copy link

r-owen commented Jun 9, 2020

I suggest indenting the type spec continuation 8 spaces (a double indent). That clearly distinguishes it from the description and avoids the hassle of having the indent depend on the length of the parameter name (which is difficult to type initially and difficult to maintain if parameters are renamed).

timj added a commit to lsst/daf_butler that referenced this issue Dec 30, 2021
numpydoc has no guidance for how to handle a long parameter type
line. Rearrange things slightly to move the type information
to the parameter documentation instead.

See numpy/numpydoc#87
timj added a commit to lsst/daf_butler that referenced this issue Jan 4, 2022
numpydoc has no guidance for how to handle a long parameter type
line. Rearrange things slightly to move the type information
to the parameter documentation instead.

See numpy/numpydoc#87
@SergejKr
Copy link

Was there any solution to this issue?

@stefanv
Copy link
Contributor

stefanv commented Feb 16, 2023

Was there any solution to this issue?

Unfortunately not. @rossbar, thoughts?

At this point we have two viable solutions on the table: use a backslash, or over-indent the second line. Either seem fine, so we should probably just choose a convention and document it.

@lucascolley
Copy link
Contributor

Any update here? If not, what indentation is needed to use the backslash with this example?

"""
...
Parameters
    ----------
    ...
    xi : 2-D ndarray of floats with shape (m, D), or length D tuple of ndarrays broadcastable to the same shape.
        Points at which to interpolate data.
    ...
"""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants