Skip to content

ENH: accept autoclass member options #205

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

Merged
merged 3 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions numpydoc/docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import copy
import sys

from sphinx.ext.autodoc import ALL

def strip_blank_lines(l):
"Remove leading and trailing blank lines from a list of lines"
Expand Down Expand Up @@ -593,18 +594,25 @@ def __init__(self, cls, doc=None, modulename='', func_doc=FunctionDoc,

NumpyDocString.__init__(self, doc)

if config.get('show_class_members', True):
_members = config.get('members', [])
if _members is ALL:
_members = None
_exclude = config.get('exclude-members', [])

if config.get('show_class_members', True) and _exclude is not ALL:
def splitlines_x(s):
if not s:
return []
else:
return s.splitlines()

for field, items in [('Methods', self.methods),
('Attributes', self.properties)]:
if not self[field]:
doc_list = []
for name in sorted(items):
if (name in _exclude or
(_members and name not in _members)):
continue
try:
doc_item = pydoc.getdoc(getattr(self._cls, name))
doc_list.append(
Expand Down
3 changes: 2 additions & 1 deletion numpydoc/numpydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

u_NL = sixu('\n')
if what == 'module':
# Strip top title
Expand Down Expand Up @@ -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

sig = doc['Signature'] or getattr(obj, '__text_signature__', None)
if sig:
sig = re.sub(sixu("^[^(]*"), sixu(""), sig)
Expand Down
20 changes: 20 additions & 0 deletions numpydoc/tests/test_docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

NumpyDocString(doc_text)
assert len(w) == 1
assert "Unknown section Mope" == str(w[0].message)

with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', UserWarning)
SphinxClassDoc(BadSection)
assert len(w) == 1
assert('test_docscrape.test_unknown_section.<locals>.BadSection'
Expand Down Expand Up @@ -1267,6 +1269,24 @@ def test_args_and_kwargs():
Keyword arguments
""")

def test_autoclass():
cfg=dict(show_class_members=True,
show_inherited_class_members=True)
doc = SphinxClassDoc(str, '''
A top section before

.. autoclass:: str
''', config=cfg)
line_by_line_compare(str(doc), r'''
A top section before

.. autoclass:: str

.. rubric:: Methods


''')


if __name__ == "__main__":
import pytest
Expand Down
55 changes: 55 additions & 0 deletions numpydoc/tests/test_numpydoc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- encoding:utf-8 -*-
from __future__ import division, absolute_import, print_function

from numpydoc.numpydoc import mangle_docstrings
from sphinx.ext.autodoc import ALL

class MockConfig():
numpydoc_use_plots = False
numpydoc_use_blockquotes = True
numpydoc_show_class_members = True
numpydoc_show_inherited_class_members = True
numpydoc_class_members_toctree = True
templates_path = []
numpydoc_edit_link = False
numpydoc_citation_re = '[a-z0-9_.-]+'

class MockBuilder():
config = MockConfig()

class MockApp():
config = MockConfig()
builder = MockBuilder()
translator = None


app = MockApp()
app.builder.app = app

def test_mangle_docstrings():
s ='''
A top section before

.. autoclass:: str
'''
lines = s.split('\n')
doc = mangle_docstrings(MockApp(), 'class', 'str', str, {}, lines)
assert 'rpartition' in [x.strip() for x in lines]

lines = s.split('\n')
doc = mangle_docstrings(MockApp(), 'class', 'str', str, {'members':['upper']}, lines)
assert 'rpartition' not in [x.strip() for x in lines]
assert 'upper' in [x.strip() for x in lines]

lines = s.split('\n')
doc = mangle_docstrings(MockApp(), 'class', 'str', str, {'exclude-members': ALL}, lines)
assert 'rpartition' not in [x.strip() for x in lines]
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

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

if __name__ == "__main__":
import pytest