Skip to content

Make parameter a namedtuple #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions numpydoc/docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ def __str__(self):
return message


Parameter = collections.namedtuple('Parameter', ['name', 'type', 'desc'])


class NumpyDocString(collections.Mapping):
"""Parses a numpydoc string to an abstract representation

Expand Down Expand Up @@ -225,7 +228,7 @@ def _parse_param_list(self, content):
desc = dedent_lines(desc)
desc = strip_blank_lines(desc)

params.append((arg_name, arg_type, desc))
params.append(Parameter(arg_name, arg_type, desc))

return params

Expand Down Expand Up @@ -409,13 +412,13 @@ def _str_param_list(self, name):
out = []
if self[name]:
out += self._str_header(name)
for param, param_type, desc in self[name]:
if param_type:
out += ['%s : %s' % (param, param_type)]
for param in self[name]:
if param.type:
out += ['%s : %s' % (param.name, param.type)]
else:
out += [param]
if desc and ''.join(desc).strip():
out += self._str_indent(desc)
out += [param.name]
if param.desc and ''.join(param.desc).strip():
out += self._str_indent(param.desc)
out += ['']
return out

Expand Down Expand Up @@ -591,7 +594,8 @@ def splitlines_x(s):
for name in sorted(items):
try:
doc_item = pydoc.getdoc(getattr(self._cls, name))
doc_list.append((name, '', splitlines_x(doc_item)))
doc_list.append(
Parameter(name, '', splitlines_x(doc_item)))
except AttributeError:
pass # method doesn't exist
self[field] = doc_list
Expand Down
54 changes: 29 additions & 25 deletions numpydoc/docscrape_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,18 @@ def _str_returns(self, name='Returns'):
if self[name]:
out += self._str_field_list(name)
out += ['']
for param, param_type, desc in self[name]:
if param_type:
out += self._str_indent([typed_fmt % (param.strip(),
param_type)])
for param in self[name]:
if param.type:
out += self._str_indent([typed_fmt % (param.name.strip(),
param.type)])
else:
out += self._str_indent([untyped_fmt % param.strip()])
if desc and self.use_blockquotes:
out += ['']
elif not desc:
desc = ['..']
out += self._str_indent(desc, 8)
out += self._str_indent([untyped_fmt % param.name.strip()])
if not param.desc:
out += self._str_indent(['..'], 8)
else:
if self.use_blockquotes:
out += ['']
out += self._str_indent(param.desc, 8)
out += ['']
return out

Expand Down Expand Up @@ -200,13 +201,14 @@ def _str_param_list(self, name, fake_autosummary=False):
if self[name]:
out += self._str_field_list(name)
out += ['']
for param, param_type, desc in self[name]:
display_param, desc = self._process_param(param, desc,
for param in self[name]:
display_param, desc = self._process_param(param.name,
param.desc,
fake_autosummary)

if param_type:
if param.type:
out += self._str_indent(['%s : %s' % (display_param,
param_type)])
param.type)])
else:
out += self._str_indent([display_param])
if desc and self.use_blockquotes:
Expand Down Expand Up @@ -243,21 +245,21 @@ def _str_member_list(self, name):

autosum = []
others = []
for param, param_type, desc in self[name]:
param = param.strip()
for param in self[name]:
param = param._replace(name=param.name.strip())

# Check if the referenced member can have a docstring or not
param_obj = getattr(self._obj, param, None)
param_obj = getattr(self._obj, param.name, None)
if not (callable(param_obj)
or isinstance(param_obj, property)
or inspect.isdatadescriptor(param_obj)):
param_obj = None

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

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

if others:
maxlen_0 = max(3, max([len(x[0]) + 4 for x in others]))
maxlen_0 = max(3, max([len(p.name) + 4 for p in others]))
hdr = sixu("=") * maxlen_0 + sixu(" ") + sixu("=") * 10
fmt = sixu('%%%ds %%s ') % (maxlen_0,)
out += ['', '', hdr]
for param, param_type, desc in others:
desc = sixu(" ").join(x.strip() for x in desc).strip()
if param_type:
desc = "(%s) %s" % (param_type, desc)
out += [fmt % ("**" + param.strip() + "**", desc)]
for param in others:
name = "**" + param.name.strip() + "**"
desc = sixu(" ").join(x.strip()
for x in param.desc).strip()
if param.type:
desc = "(%s) %s" % (param.type, desc)
out += [fmt % (name, desc)]
out += [hdr]
out += ['']
return out
Expand Down