Skip to content

Commit a1e7eba

Browse files
committed
Some cleaning and docs
1 parent f09abc8 commit a1e7eba

File tree

1 file changed

+75
-41
lines changed

1 file changed

+75
-41
lines changed

numpydoc/docscrape_sphinx.py

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

82+
def _process_param(self, param, desc, autosum):
83+
"""Determine how to display a parameter
84+
85+
Emulates autosummary behavior if autosum is not None.
86+
87+
Parameters
88+
----------
89+
param : str
90+
The name of the parameter
91+
desc : list of str
92+
The parameter description as given in the docstring
93+
autosum : list or None
94+
If a list, autosummary-style behaviour will apply for params
95+
that are attributes of the class and have a docstring.
96+
Names for autosummary generation will be appended to this list.
97+
98+
If None, autosummary is disabled.
99+
100+
Returns
101+
-------
102+
display_param : str
103+
The marked up parameter name for display. This may include a link
104+
to the corresponding attribute's own documentation.
105+
desc : list of str
106+
A list of description lines. This may be identical to the input
107+
``desc``, if ``autosum is None`` or ``param`` is not a class
108+
attribute, or it will be a summary of the class attribute's
109+
docstring.
110+
"""
111+
param = param.strip()
112+
display_param = '**%s**' % param
113+
114+
if autosum is None:
115+
return display_param, desc
116+
117+
param_obj = getattr(self._obj, param, None)
118+
if not (callable(param_obj)
119+
or isinstance(param_obj, property)
120+
or inspect.isgetsetdescriptor(param_obj)):
121+
param_obj = None
122+
obj_doc = pydoc.getdoc(param_obj)
123+
124+
if not (param_obj and obj_doc):
125+
return display_param, desc
126+
127+
prefix = getattr(self, '_name', '')
128+
if prefix:
129+
autosum_prefix = '~%s.' % prefix
130+
link_prefix = '%s.' % prefix
131+
else:
132+
autosum_prefix = ''
133+
link_prefix = ''
134+
135+
# Referenced object has a docstring
136+
autosum.append(" %s%s" % (autosum_prefix, param))
137+
display_param = ':obj:`%s <%s%s>`' % (param,
138+
link_prefix,
139+
param)
140+
if obj_doc:
141+
# Overwrite desc. Take summary logic of autosummary
142+
desc = re.split('\n\s*\n', obj_doc.strip(), 1)[0]
143+
# XXX: Should this have DOTALL?
144+
# It does not in autosummary
145+
m = re.search(r"^([A-Z].*?\.)(?:\s|$)",
146+
' '.join(desc.split()))
147+
if m:
148+
desc = m.group(1).strip()
149+
else:
150+
desc = desc.partition('\n')[0]
151+
desc = desc.split('\n')
152+
return display_param, desc
153+
82154
def _str_param_list(self, name, fake_autosummary=False):
83155
out = []
84156
if self[name]:
85157
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 = ''
93158
autosum = []
159+
else:
160+
autosum = None
94161

95162
out += self._str_field_list(name)
96163
out += ['']
97164
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:
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|$)",
126-
' '.join(desc.split()))
127-
if m:
128-
desc = m.group(1).strip()
129-
else:
130-
desc = desc.partition('\n')[0]
131-
desc = desc.split('\n')
165+
display_param, desc = self._process_param(param, desc, autosum)
132166

133167
if param_type:
134168
out += self._str_indent(['%s : %s' % (display_param,

0 commit comments

Comments
 (0)