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

Format attributes like parameters #106

Merged
merged 5 commits into from
Sep 9, 2017

Conversation

jnothman
Copy link
Member

@jnothman jnothman commented Aug 7, 2017

Resolves #102.
Resolves #104.
Resolves scikit-learn/scikit-learn#9495

  • Provide links to autogen when possible
  • Use fake (commented) autosummary nodes (as already used in scipy docs)

I'm a bit confused about how/whether the autosummaries inside numpydoc-munged docstrings work. I'm not quite sure whether this patch is working on scipy docs and would appreciate someone checking. It's good for scikit-learn.

- Provide links to autogen when possible

- Use fake (commented) autosummary nodes (as already used in scipy docs)
@jnothman
Copy link
Member Author

jnothman commented Aug 15, 2017

Is there something special I need to do to make autogen work for attributes on the scipy docs? I'm not getting the right results on numpydoc master (i.e. autogen attributes in scipy.spatial.Delaunay.html aren't hyperlinked).

@rgommers
Copy link
Member

(i.e. autogen attributes in scipy.spatial.Delaunay.html aren't hyperlinked).

Are you getting similar results as at https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.Delaunay.html#scipy.spatial.Delaunay? Looks like the listed attributes that are defined with @property are hyperlinked, the rest isn't. Which makes sense; I wouldn't expect hyperlinks for normal attributes.

@rgommers
Copy link
Member

Had a look at building the scipy docs with/without this PR.

Without:
image

With:
image
docs built with:

$ cd doc
$ rm -rf sphinxext/numpydoc  # ensure to pick up the installed version
$ make html-scipyorg

Conclusions:

  • hyperlinks are in both cases only generated for properties, which is OK.
  • without this PR the properties are listed first, irrespective of the ordering in the Attributes table. With this PR the listing is in the order of the table. OK with me, no preference either way
  • type descriptors are now visible, without this PR they are not.

Overall LGTM.

@jnothman
Copy link
Member Author

jnothman commented Aug 15, 2017 via email

@pv
Copy link
Member

pv commented Aug 15, 2017 via email

@jnothman
Copy link
Member Author

If there is a property by the listed name, it's docstring is truncated and displayed if present, taking precedence over a given description. This emulates current behaviour.

Copy link
Member Author

@jnothman jnothman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also should remind that Methods are still listed using the current _str_member_list. I would use this logic there too, but I don't currently know how to straightforwardly replicate the function signature munging.

param_obj = None
obj_doc = pydoc.getdoc(param_obj)

if param_obj and (obj_doc or not desc):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pv, this is the logic for constructing the description when the attribute corresponds to an object. Firstly, above, we only link to the attribute's autodoc if it has either a docstring or is not described in the numpydoc (and I'd be okay with removing the or not desc, except that it is current behaviour).

If it has a docstring, we try to emulate autosummary: split the object's docstring at its first blank line. Then take a prefix up to the first . if present, or the whole first paragraph otherwise.

I should probably test this logic more thoroughly before merge.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it turns out the "or not desc" logic here wasn't working because desc is being provided as [''] when there is no description line. Which means no one depends on this logic, which means I'm getting rid of it because it's weird (unless you can think of a use-case for linking to an autogen page where the attribute has no docstring).

And remove quirky 'or not desc' case
@jnothman
Copy link
Member Author

jnothman commented Sep 4, 2017

I'd really like to see this merged and released, or scikit-learn will have to go back to using a custom variant of numpydoc. I think this improves docs rendering for scipy, as shown above, and @rgommers approves. But at the moment I neither have clear consensus nor authority to merge.

@CalebBell
Copy link

One more vote towards merging this PR here. It'd be great for lots of other projects, like mine:

image

@stefanv
Copy link
Contributor

stefanv commented Sep 7, 2017

I'm fine with this going in; I am a little concerned about the added complexity, and the lack of documentation to describe the changes. Should we consider starting a developer doc, so that people can at least work on the project with an understanding for the historical motivations behind choices? Otherwise, just much more detailed docstrings for newly added features?

@jnothman
Copy link
Member Author

jnothman commented Sep 7, 2017

I agree it adds a lot of complexity. I'm happy to factor bits out to improve readability.

I disagree that this is about historical motivations. It is about wanting something that functions like autosummary and also like parameter listing, where no corresponding python object can be retrieved.

It also fixes issues about the inconsistent display of types and lack of support for block markup in descriptions of attributes.

I agree that numpydoc should have better documentation:

  • To pull that out of numpy
  • To exemplify usage
  • As an informal test of behaviour

@stefanv
Copy link
Contributor

stefanv commented Sep 7, 2017

To clarify: I did not suggest that this was about a historical motivation. Merely that, developers coming in from the future (!) will have to look at these (historical) decisions we are making now, and that it would help if we noted down their motivation (as we make them).

@jnothman
Copy link
Member Author

jnothman commented Sep 8, 2017

How does this look to you, @stefanv?

@stefanv
Copy link
Contributor

stefanv commented Sep 8, 2017

That's much better; @pv?

@pv
Copy link
Member

pv commented Sep 9, 2017

The screenshot for scipy docs above looks fine to me, so no objections.

@stefanv stefanv merged commit b215bed into numpy:master Sep 9, 2017
@jnothman
Copy link
Member Author

jnothman commented Sep 9, 2017 via email

@amueller
Copy link
Contributor

Awesome, thanks @jnothman !

@jorisvandenbossche
Copy link
Contributor

Rather late to the party, but on the pandas side we are also looking into moving to use upstream numpydoc instead our vendored version + hacks.
However, I think we (currently) would prefer how it was before: an autosummary table for the attributes, to have it consistently with the methods (see eg http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html).

The main reason for that is because we don't list attributes explicitly in our docstrings, and thus solely rely on the inferred docstrings. That also means that we never have type descriptions for attributes, hence making the parameter listing much more spacious (for nothing) compared to the autosummary methods listing.

Would it be possible to have this as an option? Altough I suppose that would even further complicate the code ..

@jnothman
Copy link
Member Author

jnothman commented Nov 9, 2017 via email

@jorisvandenbossche
Copy link
Contributor

to be sure, you don't care whether we're using autosummary or not, you care that it looks different?

Not only that. I also think the methods listing looks better for our use case (but, that can also just be the conservative bit in me regarding changing how things look ..). I think this stems a bit from a different usage of the class docstring pages. In scikit-learn they contain all the information, for both the attributes and the methods all documentation is listed on that page. In the pandas docs, we have separate pages for attributes/methods, and on the class docstring page we only want to have a summary table for them and link to the actual pages.
I don't think the one approach is necessarily better than the other, but for the approach in the pandas docs, I think the more brief methods listing is more appropriate.

And if we changed the method summary to look the same would that be better or worse? And if we made a way for you to use CSS to style these as tables, would that be better?

Not fully sure. Currently, the autosummary table will automatically only give the first line of the docstring (and this is an aspect we want to keep I think). If that can be achieved with css to style them as summary tables, then yes, that would be OK.

Except that you may also want to have sections like examples and see also, which is why you want to link to their own separate documentation, I presume.

Indeed, although I will not say this is the case currently for many docstrings. But ideally, yes. (.values is an example with a more extensive docstring: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.values.html)

Just out of curiosity: how would a method look like in such a parameter-style listing? What would be put in the type description? The signature? Or return type?
Another question: for attributes/properties to have a type description in such a parameter-style listing, is the only way to manually describe them inside the class docstring? Or can that be inferred from their own docstring? (in pandas we do not have the habit to describe attributes in class docstrings like is done in scikit-learn)

@jnothman
Copy link
Member Author

jnothman commented Nov 10, 2017 via email

@jorisvandenbossche
Copy link
Contributor

To come back to the original question: would you be open to a config option to choose the type of listing ?

@jorisvandenbossche
Copy link
Contributor

I don't think adding the option is too hard (code-wise, but of course it adds to the user complexity). See proof of concept: jorisvandenbossche@cac8fe2 (certainly missing docs and tests)

@jnothman
Copy link
Member Author

jnothman commented Nov 14, 2017 via email

@jorisvandenbossche
Copy link
Contributor

but fwiw it's not hard to change a dl to look like a table (with fixed width columns) in css

Not hard for somebody who knows css maybe :-) If that is the preferred route, I would appreciate some input on that front

@TomAugspurger
Copy link

TomAugspurger commented Nov 14, 2017 via email

@TomAugspurger
Copy link

Well, I don't see an easy way to select just the Attributes and not the Parameters section. Both are formatted as

<table class="docutils field-list" frame="void" rules="none">
<colgroup><col class="field-name">
<col class="field-body">
</colgroup><tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body">
...

And for attribute the only difference is the value in the <th class="field-name">

@jnothman
Copy link
Member Author

jnothman commented Nov 14, 2017 via email

@jorisvandenbossche
Copy link
Contributor

The problem with a css solution is that, to mimic the member listing, we also want the attribute names to be links to the separate docstring pages (and the docstring pages should be generated).
I think that is not easy to solve with css?

@TomAugspurger
Copy link

@jorisvandenbossche they're links:

screen shot 2017-11-14 at 3 25 37 pm

A simple display: inline-block doesn't quite work (everything is put on a single line), but I'm still tinkering with it.

@jorisvandenbossche
Copy link
Contributor

Ah sorry, my bad. In my test it were not links because the attribute pages didn't exist (that's a problem with numpydoc no longer generating them, but which is something we can solve in pandas by listing them all manually (as you are doing))

Anyhow, I think my proposed fix (jorisvandenbossche@cac8fe2) is rather simple if it seems difficult to get a nice way to do it with css

@jnothman
Copy link
Member Author

jnothman commented Nov 14, 2017 via email

@jorisvandenbossche
Copy link
Contributor

You're right. I was testing both numpydoc master and sphinx 1.6 at the same time (our pandas docs are currently pinned at older numpydoc and sphinx 1.5), and apparently the missing attributes were caused by the sphinx upgrade (sphinx-doc/sphinx#3235)

@jorisvandenbossche
Copy link
Contributor

I would like to come back to this, as this is one of the blockers for pandas to use upstream numpydoc.

As said above, I think adding an option to control this, is rather easy (eg jorisvandenbossche@cac8fe2). I could do a PR for this, if that would be accepted.

@TomAugspurger
Copy link

@jorisvandenbossche I can take a look as well if you have difficulty with the HTML / CSS. I don't recall how much experience you have with those.

@jnothman
Copy link
Member Author

What's the deal with the screenshot you show above? The definition list items are all over the place. I can accept a switch, but given how brittle rest can be, it becomes hard to test with more switches.

@TomAugspurger
Copy link

What's the deal with the screenshot you show above? The definition list items are all over the place.

Not sure. My comment seems to suggest I was manually adding display:inline-block to something. IIRC it was correctly formatted before I tinkered with the HTML.

@jorisvandenbossche
Copy link
Contributor

@jorisvandenbossche I can take a look as well if you have difficulty with the HTML / CSS. I don't recall how much experience you have with those.

A little bit, but nothing more than googling and trying until something 'looks' ok on my computer. While adding an option is a few lines understandable python code :-)

And apart from that, I still don't really see how it would be possible to get it fully looking the same as the Methods autosummary table listing. Of course if that is needed is debatable, but IMO giving it a different style than the methods overview will only give 'visual noise'.

The definition list items are all over the place. I can accept a switch, but given how brittle rest can be, it becomes hard to test with more switches.

It's true that the definition list is used elsewhere, but the autosummary table is also not specific to this (and in our docs also all over the place). I mean, it is not introducing new formatting code, only a switch between two already existing formatting options.

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

Successfully merging this pull request may close these issues.

8 participants