Skip to content

Commit 136d4dd

Browse files
timhoffmjnothman
authored andcommitted
Make parameter a namedtuple (#176)
1 parent cb9e6b9 commit 136d4dd

File tree

2 files changed

+41
-33
lines changed

2 files changed

+41
-33
lines changed

numpydoc/docscrape.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ def __str__(self):
106106
return message
107107

108108

109+
Parameter = collections.namedtuple('Parameter', ['name', 'type', 'desc'])
110+
111+
109112
class NumpyDocString(collections.Mapping):
110113
"""Parses a numpydoc string to an abstract representation
111114
@@ -225,7 +228,7 @@ def _parse_param_list(self, content):
225228
desc = dedent_lines(desc)
226229
desc = strip_blank_lines(desc)
227230

228-
params.append((arg_name, arg_type, desc))
231+
params.append(Parameter(arg_name, arg_type, desc))
229232

230233
return params
231234

@@ -409,13 +412,13 @@ def _str_param_list(self, name):
409412
out = []
410413
if self[name]:
411414
out += self._str_header(name)
412-
for param, param_type, desc in self[name]:
413-
if param_type:
414-
out += ['%s : %s' % (param, param_type)]
415+
for param in self[name]:
416+
if param.type:
417+
out += ['%s : %s' % (param.name, param.type)]
415418
else:
416-
out += [param]
417-
if desc and ''.join(desc).strip():
418-
out += self._str_indent(desc)
419+
out += [param.name]
420+
if param.desc and ''.join(param.desc).strip():
421+
out += self._str_indent(param.desc)
419422
out += ['']
420423
return out
421424

@@ -591,7 +594,8 @@ def splitlines_x(s):
591594
for name in sorted(items):
592595
try:
593596
doc_item = pydoc.getdoc(getattr(self._cls, name))
594-
doc_list.append((name, '', splitlines_x(doc_item)))
597+
doc_list.append(
598+
Parameter(name, '', splitlines_x(doc_item)))
595599
except AttributeError:
596600
pass # method doesn't exist
597601
self[field] = doc_list

numpydoc/docscrape_sphinx.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,18 @@ def _str_returns(self, name='Returns'):
7474
if self[name]:
7575
out += self._str_field_list(name)
7676
out += ['']
77-
for param, param_type, desc in self[name]:
78-
if param_type:
79-
out += self._str_indent([typed_fmt % (param.strip(),
80-
param_type)])
77+
for param in self[name]:
78+
if param.type:
79+
out += self._str_indent([typed_fmt % (param.name.strip(),
80+
param.type)])
8181
else:
82-
out += self._str_indent([untyped_fmt % param.strip()])
83-
if desc and self.use_blockquotes:
84-
out += ['']
85-
elif not desc:
86-
desc = ['..']
87-
out += self._str_indent(desc, 8)
82+
out += self._str_indent([untyped_fmt % param.name.strip()])
83+
if not param.desc:
84+
out += self._str_indent(['..'], 8)
85+
else:
86+
if self.use_blockquotes:
87+
out += ['']
88+
out += self._str_indent(param.desc, 8)
8889
out += ['']
8990
return out
9091

@@ -200,13 +201,14 @@ def _str_param_list(self, name, fake_autosummary=False):
200201
if self[name]:
201202
out += self._str_field_list(name)
202203
out += ['']
203-
for param, param_type, desc in self[name]:
204-
display_param, desc = self._process_param(param, desc,
204+
for param in self[name]:
205+
display_param, desc = self._process_param(param.name,
206+
param.desc,
205207
fake_autosummary)
206208

207-
if param_type:
209+
if param.type:
208210
out += self._str_indent(['%s : %s' % (display_param,
209-
param_type)])
211+
param.type)])
210212
else:
211213
out += self._str_indent([display_param])
212214
if desc and self.use_blockquotes:
@@ -243,21 +245,21 @@ def _str_member_list(self, name):
243245

244246
autosum = []
245247
others = []
246-
for param, param_type, desc in self[name]:
247-
param = param.strip()
248+
for param in self[name]:
249+
param = param._replace(name=param.name.strip())
248250

249251
# Check if the referenced member can have a docstring or not
250-
param_obj = getattr(self._obj, param, None)
252+
param_obj = getattr(self._obj, param.name, None)
251253
if not (callable(param_obj)
252254
or isinstance(param_obj, property)
253255
or inspect.isdatadescriptor(param_obj)):
254256
param_obj = None
255257

256258
if param_obj and pydoc.getdoc(param_obj):
257259
# Referenced object has a docstring
258-
autosum += [" %s%s" % (prefix, param)]
260+
autosum += [" %s%s" % (prefix, param.name)]
259261
else:
260-
others.append((param, param_type, desc))
262+
others.append(param)
261263

262264
if autosum:
263265
out += ['.. autosummary::']
@@ -266,15 +268,17 @@ def _str_member_list(self, name):
266268
out += [''] + autosum
267269

268270
if others:
269-
maxlen_0 = max(3, max([len(x[0]) + 4 for x in others]))
271+
maxlen_0 = max(3, max([len(p.name) + 4 for p in others]))
270272
hdr = sixu("=") * maxlen_0 + sixu(" ") + sixu("=") * 10
271273
fmt = sixu('%%%ds %%s ') % (maxlen_0,)
272274
out += ['', '', hdr]
273-
for param, param_type, desc in others:
274-
desc = sixu(" ").join(x.strip() for x in desc).strip()
275-
if param_type:
276-
desc = "(%s) %s" % (param_type, desc)
277-
out += [fmt % ("**" + param.strip() + "**", desc)]
275+
for param in others:
276+
name = "**" + param.name.strip() + "**"
277+
desc = sixu(" ").join(x.strip()
278+
for x in param.desc).strip()
279+
if param.type:
280+
desc = "(%s) %s" % (param.type, desc)
281+
out += [fmt % (name, desc)]
278282
out += [hdr]
279283
out += ['']
280284
return out

0 commit comments

Comments
 (0)