Skip to content

Commit 5f641a1

Browse files
committed
Format attributes like parameters
- Provide links to autogen when possible - Use fake (commented) autosummary nodes (as already used in scipy docs)
1 parent 057ef57 commit 5f641a1

File tree

2 files changed

+73
-13
lines changed

2 files changed

+73
-13
lines changed

numpydoc/docscrape_sphinx.py

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,74 @@ def _str_returns(self, name='Returns'):
7979
out += ['']
8080
return out
8181

82-
def _str_param_list(self, name):
82+
def _str_param_list(self, name, fake_autosummary=False):
8383
out = []
8484
if self[name]:
85+
if fake_autosummary:
86+
prefix = getattr(self, '_name', '')
87+
if prefix:
88+
autosum_prefix = '~%s.' % prefix
89+
link_prefix = '%s.' % prefix
90+
else:
91+
autosum_prefix = ''
92+
link_prefix = ''
93+
autosum = []
94+
8595
out += self._str_field_list(name)
8696
out += ['']
8797
for param, param_type, desc in self[name]:
98+
param = param.strip()
99+
100+
display_param = '**%s**' % param
101+
102+
if fake_autosummary:
103+
param_obj = getattr(self._obj, param, None)
104+
if not (callable(param_obj)
105+
or isinstance(param_obj, property)
106+
or inspect.isgetsetdescriptor(param_obj)):
107+
param_obj = None
108+
obj_doc = pydoc.getdoc(param_obj)
109+
110+
if param_obj and (obj_doc or not desc):
111+
# Referenced object has a docstring
112+
autosum += [" %s%s" % (autosum_prefix, param)]
113+
# TODO: add signature to display as in autosummary
114+
# Difficult because autosummary's processing
115+
# involves sphinx's plugin mechanism, for
116+
# directives only
117+
display_param = ':obj:`%s <%s%s>`' % (param,
118+
link_prefix,
119+
param)
120+
if obj_doc:
121+
# Overwrite desc. Take summary logic of autosummary
122+
desc = re.split('\n\s*\n', obj_doc.strip(), 1)[0]
123+
# XXX: Should this have DOTALL?
124+
# It does not in autosummary
125+
m = re.search(r"^([A-Z].*?\.)(?:\s|$)", desc)
126+
if m:
127+
desc = m.group(1).strip()
128+
else:
129+
desc = desc.partition('\n')[0]
130+
desc = desc.split('\n')
131+
88132
if param_type:
89-
out += self._str_indent(['**%s** : %s' % (param.strip(),
90-
param_type)])
133+
out += self._str_indent(['%s : %s' % (display_param,
134+
param_type)])
91135
else:
92-
out += self._str_indent(['**%s**' % param.strip()])
136+
out += self._str_indent([display_param])
93137
if desc:
94138
out += ['']
95139
out += self._str_indent(desc, 8)
96140
out += ['']
141+
142+
if fake_autosummary and autosum:
143+
if self.class_members_toctree:
144+
autosum.insert(0, ' :toctree:')
145+
autosum.insert(0, '.. autosummary::')
146+
out += ['..', ' HACK to make autogen generate docs:']
147+
out += self._str_indent(autosum, 4)
148+
out += ['']
149+
97150
return out
98151

99152
@property
@@ -250,7 +303,8 @@ def __str__(self, indent=0, func_role="obj"):
250303
'notes': self._str_section('Notes'),
251304
'references': self._str_references(),
252305
'examples': self._str_examples(),
253-
'attributes': self._str_member_list('Attributes'),
306+
'attributes': self._str_param_list('Attributes',
307+
fake_autosummary=True),
254308
'methods': self._str_member_list('Methods'),
255309
}
256310
ns = dict((k, '\n'.join(v)) for k, v in ns.items())

numpydoc/tests/test_docscrape.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from nose.tools import (assert_equal, assert_raises, assert_list_equal,
1818
assert_true)
1919

20+
assert_list_equal.__self__.maxDiff = None
21+
2022
if sys.version_info[0] >= 3:
2123
sixu = lambda s: s
2224
else:
@@ -975,17 +977,21 @@ def x(self):
975977
976978
For usage examples, see `ode`.
977979
978-
.. rubric:: Attributes
980+
:Attributes:
979981
980-
.. autosummary::
981-
:toctree:
982+
**t** : float
983+
Current time.
984+
**y** : ndarray
985+
Current variable values.
986+
:obj:`x <x>` : float
987+
Test attribute
982988
983-
x
989+
..
990+
HACK to make autogen generate docs:
991+
.. autosummary::
992+
:toctree:
984993
985-
===== ==========
986-
**t** (float) Current time.
987-
**y** (ndarray) Current variable values.
988-
===== ==========
994+
x
989995
990996
.. rubric:: Methods
991997

0 commit comments

Comments
 (0)