Skip to content

Commit

Permalink
Merge pull request scikit-learn#22 from espdev/show-inherited-class-m…
Browse files Browse the repository at this point in the history
…embers

ENH: Add a config option for showing/hiding the inherited class members
  • Loading branch information
pv committed Sep 7, 2014
2 parents 7c3a1dc + 1eded92 commit 51982b2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,20 @@ The following options can be set in conf.py:

Whether to show all members of a class in the Methods and Attributes
sections automatically.
``True`` by default.

- numpydoc_show_inherited_class_members: bool

Whether to show all inherited members of a class in the Methods and Attributes
sections automatically. If it's false, inherited members won't shown.
``True`` by default.

- numpydoc_class_members_toctree: bool

Whether to create a Sphinx table of contents for the lists of class
methods and attributes. If a table of contents is made, Sphinx expects
each entry to have a separate page.
``True`` by default.

- numpydoc_edit_link: bool (DEPRECATED -- edit your HTML template instead)

Expand Down
30 changes: 21 additions & 9 deletions numpydoc/docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ def __init__(self, cls, doc=None, modulename='', func_doc=FunctionDoc,
raise ValueError("Expected a class or None, but got %r" % cls)
self._cls = cls

self.show_inherited_members = config.get('show_inherited_class_members',
True)

if modulename and not modulename.endswith('.'):
modulename += '.'
self._mod = modulename
Expand All @@ -505,27 +508,36 @@ def splitlines_x(s):
if not self[field]:
doc_list = []
for name in sorted(items):
try:
try:
doc_item = pydoc.getdoc(getattr(self._cls, name))
doc_list.append((name, '', splitlines_x(doc_item)))
except AttributeError:
pass # method doesn't exist
except AttributeError:
pass # method doesn't exist
self[field] = doc_list

@property
def methods(self):
if self._cls is None:
return []
return [name for name,func in inspect.getmembers(self._cls)
return [name for name, func in inspect.getmembers(self._cls)
if ((not name.startswith('_')
or name in self.extra_public_methods)
and isinstance(func, collections.Callable))]
and isinstance(func, collections.Callable)
and self._is_show_member(name))]

@property
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 or isinstance(func, property) or
inspect.isgetsetdescriptor(func))]
return [name for name, func in inspect.getmembers(self._cls)
if (not name.startswith('_') and
(func is None or isinstance(func, property) or
inspect.isgetsetdescriptor(func))
and self._is_show_member(name))]

def _is_show_member(self, name):
if self.show_inherited_members:
return True # show all class members
if name not in self._cls.__dict__:
return False # class member is inherited, we do not show it
return True
15 changes: 10 additions & 5 deletions numpydoc/numpydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"""
from __future__ import division, absolute_import, print_function

import os, sys, re, pydoc
import sys
import re
import pydoc
import sphinx
import inspect
import collections
Expand All @@ -37,10 +39,12 @@
def mangle_docstrings(app, what, name, obj, options, lines,
reference_offset=[0]):

cfg = dict(use_plots=app.config.numpydoc_use_plots,
show_class_members=app.config.numpydoc_show_class_members,
class_members_toctree=app.config.numpydoc_class_members_toctree,
)
cfg = dict(
use_plots=app.config.numpydoc_use_plots,
show_class_members=app.config.numpydoc_show_class_members,
show_inherited_class_members=app.config.numpydoc_show_inherited_class_members,
class_members_toctree=app.config.numpydoc_class_members_toctree,
)

if what == 'module':
# Strip top title
Expand Down Expand Up @@ -116,6 +120,7 @@ def setup(app, get_doc_object_=get_doc_object):
app.add_config_value('numpydoc_edit_link', None, False)
app.add_config_value('numpydoc_use_plots', None, False)
app.add_config_value('numpydoc_show_class_members', True, True)
app.add_config_value('numpydoc_show_inherited_class_members', True, True)
app.add_config_value('numpydoc_class_members_toctree', True, True)

# Extra mangling domains
Expand Down
40 changes: 40 additions & 0 deletions numpydoc/tests/test_docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,46 @@ class Ignorable(object):
else:
assert 'Spammity index' in str(doc), str(doc)

class SubDummy(Dummy):
"""
Subclass of Dummy class.
"""
def ham(self, c, d):
"""Cheese\n\nNo cheese.\nOverloaded Dummy.ham"""
pass

def bar(self, a, b):
"""Bar\n\nNo bar"""
pass

for cls in (ClassDoc, SphinxClassDoc):
doc = cls(SubDummy, config=dict(show_class_members=True,
show_inherited_class_members=False))
assert 'Methods' in str(doc), (cls, str(doc))
assert 'spam' not in str(doc), (cls, str(doc))
assert 'ham' in str(doc), (cls, str(doc))
assert 'bar' in str(doc), (cls, str(doc))
assert 'spammity' not in str(doc), (cls, str(doc))

if cls is SphinxClassDoc:
assert '.. autosummary::' in str(doc), str(doc)
else:
assert 'Spammity index' not in str(doc), str(doc)

doc = cls(SubDummy, config=dict(show_class_members=True,
show_inherited_class_members=True))
assert 'Methods' in str(doc), (cls, str(doc))
assert 'spam' in str(doc), (cls, str(doc))
assert 'ham' in str(doc), (cls, str(doc))
assert 'bar' in str(doc), (cls, str(doc))
assert 'spammity' in str(doc), (cls, str(doc))

if cls is SphinxClassDoc:
assert '.. autosummary::' in str(doc), str(doc)
else:
assert 'Spammity index' in str(doc), str(doc)

def test_duplicate_signature():
# Duplicate function signatures occur e.g. in ufuncs, when the
# automatic mechanism adds one, and a more detailed comes from the
Expand Down

0 comments on commit 51982b2

Please sign in to comment.