Skip to content

Commit c2e8b8f

Browse files
authored
Merge pull request #161 from jorisvandenbossche/attributes_as_param_list
Add option to use member listing for attributes
2 parents 54e3cb0 + d4d3445 commit c2e8b8f

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

doc/install.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ numpydoc_use_blockquotes : bool
4242
Until version 0.8, parameter definitions were shown as blockquotes, rather
4343
than in a definition list. If your styling requires blockquotes, switch
4444
this config option to True. This option will be removed in version 0.10.
45+
numpydoc_attributes_as_param_list : bool
46+
Whether to format the Attributes section of a class page in the same way
47+
as the Parameter section. If it's False, the Attributes section will be
48+
formatted as the Methods section using an autosummary table.
49+
``True`` by default.
4550
numpydoc_edit_link : bool
4651
.. deprecated:: edit your HTML template instead
4752

numpydoc/docscrape_sphinx.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def load_config(self, config):
3636
self.use_plots = config.get('use_plots', False)
3737
self.use_blockquotes = config.get('use_blockquotes', False)
3838
self.class_members_toctree = config.get('class_members_toctree', True)
39+
self.attributes_as_param_list = config.get('attributes_as_param_list', True)
3940
self.template = config.get('template', None)
4041
if self.template is None:
4142
template_dirs = [os.path.join(os.path.dirname(__file__), 'templates')]
@@ -384,8 +385,10 @@ def __str__(self, indent=0, func_role="obj"):
384385
'notes': self._str_section('Notes'),
385386
'references': self._str_references(),
386387
'examples': self._str_examples(),
387-
'attributes': self._str_param_list('Attributes',
388-
fake_autosummary=True),
388+
'attributes':
389+
self._str_param_list('Attributes', fake_autosummary=True)
390+
if self.attributes_as_param_list
391+
else self._str_member_list('Attributes'),
389392
'methods': self._str_member_list('Methods'),
390393
}
391394
ns = dict((k, '\n'.join(v)) for k, v in ns.items())

numpydoc/numpydoc.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ def mangle_docstrings(app, what, name, obj, options, lines):
152152
'show_class_members': app.config.numpydoc_show_class_members,
153153
'show_inherited_class_members':
154154
app.config.numpydoc_show_inherited_class_members,
155-
'class_members_toctree': app.config.numpydoc_class_members_toctree}
155+
'class_members_toctree': app.config.numpydoc_class_members_toctree,
156+
'attributes_as_param_list':
157+
app.config.numpydoc_attributes_as_param_list}
156158

157159
cfg.update(options or {})
158160
u_NL = sixu('\n')
@@ -227,6 +229,7 @@ def setup(app, get_doc_object_=get_doc_object):
227229
app.add_config_value('numpydoc_show_inherited_class_members', True, True)
228230
app.add_config_value('numpydoc_class_members_toctree', True, True)
229231
app.add_config_value('numpydoc_citation_re', '[a-z0-9_.-]+', True)
232+
app.add_config_value('numpydoc_attributes_as_param_list', True, True)
230233

231234
# Extra mangling domains
232235
app.add_domain(NumpyPythonDomain)

numpydoc/tests/test_docscrape.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,43 @@ def no_period(self):
12951295
""")
12961296

12971297

1298+
def test_class_attributes_as_member_list():
1299+
1300+
class Foo:
1301+
"""
1302+
Class docstring.
1303+
1304+
Attributes
1305+
----------
1306+
an_attribute
1307+
Another description that is not used.
1308+
1309+
"""
1310+
@property
1311+
def an_attribute(self):
1312+
"""Test attribute"""
1313+
return None
1314+
1315+
attr_doc = """:Attributes:
1316+
1317+
:obj:`an_attribute <an_attribute>`
1318+
Test attribute"""
1319+
1320+
assert attr_doc in str(SphinxClassDoc(Foo))
1321+
assert "Another description" not in str(SphinxClassDoc(Foo))
1322+
1323+
attr_doc2 = """.. rubric:: Attributes
1324+
1325+
.. autosummary::
1326+
:toctree:
1327+
1328+
an_attribute"""
1329+
1330+
cfg = dict(attributes_as_param_list=False)
1331+
assert attr_doc2 in str(SphinxClassDoc(Foo, config=cfg))
1332+
assert "Another description" not in str(SphinxClassDoc(Foo, config=cfg))
1333+
1334+
12981335
def test_templated_sections():
12991336
doc = SphinxClassDoc(None, class_doc_txt,
13001337
config={'template': jinja2.Template('{{examples}}\n{{parameters}}')})

numpydoc/tests/test_numpydoc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class MockConfig():
1313
templates_path = []
1414
numpydoc_edit_link = False
1515
numpydoc_citation_re = '[a-z0-9_.-]+'
16+
numpydoc_attributes_as_param_list = True
1617

1718
class MockBuilder():
1819
config = MockConfig()

0 commit comments

Comments
 (0)