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

ENH: accept autoclass member options #205

Merged
merged 3 commits into from Apr 9, 2019
Merged

ENH: accept autoclass member options #205

merged 3 commits into from Apr 9, 2019

Conversation

mattip
Copy link
Member

@mattip mattip commented Apr 7, 2019

numpydoc adds member docstrings in calls to get_doc_object, in both mangle_docstringsand mangle_signature. A feature of numpydoc is that class methods get their own html page and the method in the class table is a link to that page. However, the automatic toctree generation in an autoclass directive does not add the directory generated to the toctree, resulting in references to unknown document warnings for each method. The only solution I could find was to use :exclude-members: on the autoclass directive. I modified the logic inside get_doc_object to handle both the :members: and :exclude-members: options. Tests added. Here is an example

Some text

.. autoclass:: str
    :exclude_members:

Changing case
-------------------
.. autosummary::
    :toctree: generated/
    str.upper
    str.lower

Checking content
-----------------------

.. autosummary:
    :toctree: generated/
    str.isalnum
    str.isalpha
    str.isdecimal
    str.isdigit

and so on

@@ -132,6 +132,7 @@ def mangle_docstrings(app, what, name, obj, options, lines):
app.config.numpydoc_show_inherited_class_members,
'class_members_toctree': app.config.numpydoc_class_members_toctree}

cfg.update(options)
Copy link
Member Author

Choose a reason for hiding this comment

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

Pass the options on to get_doc_object

Copy link
Collaborator

Choose a reason for hiding this comment

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

To build the SciPy doc I had to change this to cfg.update(options or {}) because in scipyoptdoc we pass None for options

Copy link
Member Author

Choose a reason for hiding this comment

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

adopting

@@ -177,7 +178,7 @@ def mangle_signature(app, what, name, obj, options, sig, retann):

if not hasattr(obj, '__doc__'):
return
doc = get_doc_object(obj)
doc = get_doc_object(obj, config={'show_class_members': False})
Copy link
Member Author

Choose a reason for hiding this comment

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

We just want the signature at this point, not all the side effects of parsing all the members of obj

@@ -796,11 +796,13 @@ class BadSection(object):
pass

with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', UserWarning)
Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure how test runs avoid the need for a filter to work properly, this is more in line with what numpy does.

@mattip
Copy link
Member Author

mattip commented Apr 8, 2019

xref #184. Not strictly connected since this issue is connected with autoclass not autosummary, but the mangle_signature change may be relevant.

Copy link
Collaborator

@larsoner larsoner left a comment

Choose a reason for hiding this comment

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

I can confirm that this does not break my MNE build, which uses methods documented on the same page as the class itself, or my SciPy build (with the suggested change), which uses methods documented on their own page.

I started looking into whether or not this PR would allow me to (re-)simplify the SciPy doc build by undoing some changes I made to SciPy recently to work around the sorts of things mentioned in this PR (I think?). But if I remove the templates I had added for attribute.rst and method.rst in order to place :orphan: tags at the top, and revert the tweaks I had made to class.rst, I see the old warnings back again:

/home/larsoner/python/scipy/scipy/stats/_distn_infrastructure.py:docstring of scipy.stats.rv_histogram.var:1: WARNING: duplicate object description of scipy.stats.rv_histogram.var, other instance in /home/larsoner/python/scipy/doc/source/generated/scipy.stats.rv_histogram.rst, use :noindex: for one of them
...
/home/larsoner/python/scipy/doc/source/generated/scipy.LowLevelCallable.function.rst: WARNING: document isn't included in any toctree

Would you expect that these tweaks should no longer be necessary after this PR?

@@ -132,6 +132,7 @@ def mangle_docstrings(app, what, name, obj, options, lines):
app.config.numpydoc_show_inherited_class_members,
'class_members_toctree': app.config.numpydoc_class_members_toctree}

cfg.update(options)
Copy link
Collaborator

Choose a reason for hiding this comment

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

To build the SciPy doc I had to change this to cfg.update(options or {}) because in scipyoptdoc we pass None for options

@mattip
Copy link
Member Author

mattip commented Apr 8, 2019

Would you expect that these tweaks should no longer be necessary after this PR?

I think the "document isn't included in any toctree" is from numpydoc creating a document for methods but not linking it into a toctree, so the :orphan: is exactly the needed fix for this problem.

I don't see why /python/scipy/scipy/stats/_distn_infrastructure.py is linking in the docstring for rv_histogram.var.

@larsoner
Copy link
Collaborator

larsoner commented Apr 8, 2019

I don't see why /python/scipy/scipy/stats/_distn_infrastructure.py is linking in the docstring for rv_histogram.var.

This is just a peculiarity of how SciPy generates docs, as stats distribution docs are generated on the fly. When I revert my class.rst workarounds, the problem is also in more standard places like:

/home/larsoner/python/scipy/scipy/__init__.py:docstring of scipy.LowLevelCallable.count:1: WARNING: duplicate object description of scipy.LowLevelCallable.count, other instance in /home/larsoner/python/scipy/doc/source/generated/scipy.LowLevelCallable.rst, use :noindex: for one of them

But this existed before this PR, too, so I'm not too surprised it continues here.

In any case, it looks like this does not simplify anything at the SciPy end. But it looks like it isn't really designed to, doesn't break the builds, and otherwise looks reasonable to me.

numpydoc/tests/test_numpydoc.py Outdated Show resolved Hide resolved
@@ -132,6 +132,7 @@ def mangle_docstrings(app, what, name, obj, options, lines):
app.config.numpydoc_show_inherited_class_members,
'class_members_toctree': app.config.numpydoc_class_members_toctree}

cfg.update(options or {})
Copy link
Member

Choose a reason for hiding this comment

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

Is this an appropriate thing to do with already-supported options?

Copy link
Member Author

Choose a reason for hiding this comment

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

At some point numpydoc should re-use the sphinx versions of the options (numpdoc_show_inherited_class_members => 'inherited_members') rather than duplicate them, so I would claim it is not only appropriate but desired

assert 'upper' not in [x.strip() for x in lines]

lines = s.split('\n')
doc = mangle_docstrings(MockApp(), 'class', 'str', str, {'exclude-members': 'upper'}, lines)
Copy link
Member

Choose a reason for hiding this comment

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

is upper here intentionally a string and not a list of strings? are both meant to be supported?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not really, but it works because 'upper' in 'upper' => True. fixing

Co-Authored-By: mattip <matti.picus@gmail.com>
@larsoner larsoner merged commit a482f66 into numpy:master Apr 9, 2019
@larsoner
Copy link
Collaborator

larsoner commented Apr 9, 2019

Thanks @mattip !

@mattip
Copy link
Member Author

mattip commented Apr 9, 2019

Thanks for the quick review

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.

None yet

3 participants