Permalink
Browse files

BUG: numpydoc: fix bugs in attribute docstring extraction + improve p…

…resentation
  • Loading branch information...
1 parent c5efee8 commit fd9064f5ee734ffb1dd1f16e7b0f6ce9cfa25df5 @pv pv committed Feb 16, 2013
Showing with 34 additions and 16 deletions.
  1. +16 −7 doc/sphinxext/numpydoc/docscrape.py
  2. +18 −9 doc/sphinxext/numpydoc/docscrape_sphinx.py
@@ -490,12 +490,19 @@ def __init__(self, cls, doc=None, modulename='', func_doc=FunctionDoc,
NumpyDocString.__init__(self, doc)
if config.get('show_class_members', True):
- if not self['Methods']:
- self['Methods'] = [(name, '', '')
- for name in sorted(self.methods)]
- if not self['Attributes']:
- self['Attributes'] = [(name, '', '')
- for name in sorted(self.properties)]
+ def splitlines_x(s):
+ if not s:
+ return []
+ else:
+ return s.splitlines()
+
+ for field, items in [('Methods', self.methods),
+ ('Attributes', self.properties)]:
+ if not self[field]:
+ self[field] = [
+ (name, '',
+ splitlines_x(pydoc.getdoc(getattr(self._cls, name))))
+ for name in sorted(items)]
@property
def methods(self):
@@ -511,4 +518,6 @@ def properties(self):
if self._cls is None:
return []
return [name for name,func in inspect.getmembers(self._cls)
- if not name.startswith('_') and func is None]
+ if not name.startswith('_') and
+ (func is None or isinstance(func, property) or
+ inspect.isgetsetdescriptor(func))]
@@ -72,7 +72,16 @@ def _str_member_list(self, name):
others = []
for param, param_type, desc in self[name]:
param = param.strip()
- if not self._obj or hasattr(self._obj, param):
+
+ # Check if the referenced member can have a docstring or not
+ param_obj = getattr(self._obj, param, None)
+ if not (callable(param_obj)
+ or isinstance(param_obj, property)
+ or inspect.isgetsetdescriptor(param_obj)):
+ param_obj = None
+
+ if param_obj and (pydoc.getdoc(param_obj) or not desc):
+ # Referenced object has a docstring
autosum += [" %s%s" % (prefix, param)]
else:
others.append((param, param_type, desc))
@@ -82,15 +91,15 @@ def _str_member_list(self, name):
out += autosum
if others:
- maxlen_0 = max([len(x[0]) for x in others])
- maxlen_1 = max([len(x[1]) for x in others])
- hdr = "="*maxlen_0 + " " + "="*maxlen_1 + " " + "="*10
- fmt = '%%%ds %%%ds ' % (maxlen_0, maxlen_1)
- n_indent = maxlen_0 + maxlen_1 + 4
- out += [hdr]
+ maxlen_0 = max(3, max([len(x[0]) for x in others]))
+ hdr = u"="*maxlen_0 + u" " + u"="*10
+ fmt = u'%%%ds %%s ' % (maxlen_0,)
+ out += ['', hdr]
for param, param_type, desc in others:
- out += [fmt % (param.strip(), param_type)]
- out += self._str_indent(desc, n_indent)
+ desc = u" ".join(x.strip() for x in desc).strip()
+ if param_type:
+ desc = "(%s) %s" % (param_type, desc)
+ out += [fmt % (param.strip(), desc)]
out += [hdr]
out += ['']
return out

0 comments on commit fd9064f

Please sign in to comment.