Skip to content

Commit a482f66

Browse files
mattiplarsoner
authored andcommitted
ENH: accept autoclass member options (#205)
* ENH: accept autoclass member options * BUG: options can be None (from review) * MAINT: fixes from review Co-Authored-By: mattip <[email protected]>
1 parent d49deef commit a482f66

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

numpydoc/docscrape.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import copy
1717
import sys
1818

19+
from sphinx.ext.autodoc import ALL
1920

2021
def strip_blank_lines(l):
2122
"Remove leading and trailing blank lines from a list of lines"
@@ -597,18 +598,25 @@ def __init__(self, cls, doc=None, modulename='', func_doc=FunctionDoc,
597598

598599
NumpyDocString.__init__(self, doc)
599600

600-
if config.get('show_class_members', True):
601+
_members = config.get('members', [])
602+
if _members is ALL:
603+
_members = None
604+
_exclude = config.get('exclude-members', [])
605+
606+
if config.get('show_class_members', True) and _exclude is not ALL:
601607
def splitlines_x(s):
602608
if not s:
603609
return []
604610
else:
605611
return s.splitlines()
606-
607612
for field, items in [('Methods', self.methods),
608613
('Attributes', self.properties)]:
609614
if not self[field]:
610615
doc_list = []
611616
for name in sorted(items):
617+
if (name in _exclude or
618+
(_members and name not in _members)):
619+
continue
612620
try:
613621
doc_item = pydoc.getdoc(getattr(self._cls, name))
614622
doc_list.append(

numpydoc/numpydoc.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def mangle_docstrings(app, what, name, obj, options, lines):
154154
app.config.numpydoc_show_inherited_class_members,
155155
'class_members_toctree': app.config.numpydoc_class_members_toctree}
156156

157+
cfg.update(options or {})
157158
u_NL = sixu('\n')
158159
if what == 'module':
159160
# Strip top title
@@ -199,7 +200,7 @@ def mangle_signature(app, what, name, obj, options, sig, retann):
199200

200201
if not hasattr(obj, '__doc__'):
201202
return
202-
doc = get_doc_object(obj)
203+
doc = get_doc_object(obj, config={'show_class_members': False})
203204
sig = doc['Signature'] or getattr(obj, '__text_signature__', None)
204205
if sig:
205206
sig = re.sub(sixu("^[^(]*"), sixu(""), sig)

numpydoc/tests/test_docscrape.py

+20
Original file line numberDiff line numberDiff line change
@@ -866,11 +866,13 @@ class BadSection(object):
866866
pass
867867

868868
with warnings.catch_warnings(record=True) as w:
869+
warnings.filterwarnings('always', '', UserWarning)
869870
NumpyDocString(doc_text)
870871
assert len(w) == 1
871872
assert "Unknown section Mope" == str(w[0].message)
872873

873874
with warnings.catch_warnings(record=True) as w:
875+
warnings.filterwarnings('always', '', UserWarning)
874876
SphinxClassDoc(BadSection)
875877
assert len(w) == 1
876878
assert('test_docscrape.test_unknown_section.<locals>.BadSection'
@@ -1337,6 +1339,24 @@ def test_args_and_kwargs():
13371339
Keyword arguments
13381340
""")
13391341

1342+
def test_autoclass():
1343+
cfg=dict(show_class_members=True,
1344+
show_inherited_class_members=True)
1345+
doc = SphinxClassDoc(str, '''
1346+
A top section before
1347+
1348+
.. autoclass:: str
1349+
''', config=cfg)
1350+
line_by_line_compare(str(doc), r'''
1351+
A top section before
1352+
1353+
.. autoclass:: str
1354+
1355+
.. rubric:: Methods
1356+
1357+
1358+
''')
1359+
13401360

13411361
if __name__ == "__main__":
13421362
import pytest

numpydoc/tests/test_numpydoc.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- encoding:utf-8 -*-
2+
from __future__ import division, absolute_import, print_function
3+
4+
from numpydoc.numpydoc import mangle_docstrings
5+
from sphinx.ext.autodoc import ALL
6+
7+
class MockConfig():
8+
numpydoc_use_plots = False
9+
numpydoc_use_blockquotes = True
10+
numpydoc_show_class_members = True
11+
numpydoc_show_inherited_class_members = True
12+
numpydoc_class_members_toctree = True
13+
templates_path = []
14+
numpydoc_edit_link = False
15+
numpydoc_citation_re = '[a-z0-9_.-]+'
16+
17+
class MockBuilder():
18+
config = MockConfig()
19+
20+
class MockApp():
21+
config = MockConfig()
22+
builder = MockBuilder()
23+
translator = None
24+
25+
26+
app = MockApp()
27+
app.builder.app = app
28+
29+
def test_mangle_docstrings():
30+
s ='''
31+
A top section before
32+
33+
.. autoclass:: str
34+
'''
35+
lines = s.split('\n')
36+
doc = mangle_docstrings(MockApp(), 'class', 'str', str, {}, lines)
37+
assert 'rpartition' in [x.strip() for x in lines]
38+
39+
lines = s.split('\n')
40+
doc = mangle_docstrings(MockApp(), 'class', 'str', str, {'members': ['upper']}, lines)
41+
assert 'rpartition' not in [x.strip() for x in lines]
42+
assert 'upper' in [x.strip() for x in lines]
43+
44+
lines = s.split('\n')
45+
doc = mangle_docstrings(MockApp(), 'class', 'str', str, {'exclude-members': ALL}, lines)
46+
assert 'rpartition' not in [x.strip() for x in lines]
47+
assert 'upper' not in [x.strip() for x in lines]
48+
49+
lines = s.split('\n')
50+
doc = mangle_docstrings(MockApp(), 'class', 'str', str,
51+
{'exclude-members': ['upper']}, lines)
52+
assert 'rpartition' in [x.strip() for x in lines]
53+
assert 'upper' not in [x.strip() for x in lines]
54+
55+
if __name__ == "__main__":
56+
import pytest

0 commit comments

Comments
 (0)