Skip to content

Commit 51982b2

Browse files
committed
Merge pull request #22 from espdev/show-inherited-class-members
ENH: Add a config option for showing/hiding the inherited class members
2 parents 7c3a1dc + 1eded92 commit 51982b2

File tree

4 files changed

+79
-14
lines changed

4 files changed

+79
-14
lines changed

README.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,20 @@ The following options can be set in conf.py:
4545

4646
Whether to show all members of a class in the Methods and Attributes
4747
sections automatically.
48+
``True`` by default.
49+
50+
- numpydoc_show_inherited_class_members: bool
51+
52+
Whether to show all inherited members of a class in the Methods and Attributes
53+
sections automatically. If it's false, inherited members won't shown.
54+
``True`` by default.
4855

4956
- numpydoc_class_members_toctree: bool
5057

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

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

numpydoc/docscrape.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ def __init__(self, cls, doc=None, modulename='', func_doc=FunctionDoc,
482482
raise ValueError("Expected a class or None, but got %r" % cls)
483483
self._cls = cls
484484

485+
self.show_inherited_members = config.get('show_inherited_class_members',
486+
True)
487+
485488
if modulename and not modulename.endswith('.'):
486489
modulename += '.'
487490
self._mod = modulename
@@ -505,27 +508,36 @@ def splitlines_x(s):
505508
if not self[field]:
506509
doc_list = []
507510
for name in sorted(items):
508-
try:
511+
try:
509512
doc_item = pydoc.getdoc(getattr(self._cls, name))
510513
doc_list.append((name, '', splitlines_x(doc_item)))
511-
except AttributeError:
512-
pass # method doesn't exist
514+
except AttributeError:
515+
pass # method doesn't exist
513516
self[field] = doc_list
514517

515518
@property
516519
def methods(self):
517520
if self._cls is None:
518521
return []
519-
return [name for name,func in inspect.getmembers(self._cls)
522+
return [name for name, func in inspect.getmembers(self._cls)
520523
if ((not name.startswith('_')
521524
or name in self.extra_public_methods)
522-
and isinstance(func, collections.Callable))]
525+
and isinstance(func, collections.Callable)
526+
and self._is_show_member(name))]
523527

524528
@property
525529
def properties(self):
526530
if self._cls is None:
527531
return []
528-
return [name for name,func in inspect.getmembers(self._cls)
529-
if not name.startswith('_') and
530-
(func is None or isinstance(func, property) or
531-
inspect.isgetsetdescriptor(func))]
532+
return [name for name, func in inspect.getmembers(self._cls)
533+
if (not name.startswith('_') and
534+
(func is None or isinstance(func, property) or
535+
inspect.isgetsetdescriptor(func))
536+
and self._is_show_member(name))]
537+
538+
def _is_show_member(self, name):
539+
if self.show_inherited_members:
540+
return True # show all class members
541+
if name not in self._cls.__dict__:
542+
return False # class member is inherited, we do not show it
543+
return True

numpydoc/numpydoc.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"""
1818
from __future__ import division, absolute_import, print_function
1919

20-
import os, sys, re, pydoc
20+
import sys
21+
import re
22+
import pydoc
2123
import sphinx
2224
import inspect
2325
import collections
@@ -37,10 +39,12 @@
3739
def mangle_docstrings(app, what, name, obj, options, lines,
3840
reference_offset=[0]):
3941

40-
cfg = dict(use_plots=app.config.numpydoc_use_plots,
41-
show_class_members=app.config.numpydoc_show_class_members,
42-
class_members_toctree=app.config.numpydoc_class_members_toctree,
43-
)
42+
cfg = dict(
43+
use_plots=app.config.numpydoc_use_plots,
44+
show_class_members=app.config.numpydoc_show_class_members,
45+
show_inherited_class_members=app.config.numpydoc_show_inherited_class_members,
46+
class_members_toctree=app.config.numpydoc_class_members_toctree,
47+
)
4448

4549
if what == 'module':
4650
# Strip top title
@@ -116,6 +120,7 @@ def setup(app, get_doc_object_=get_doc_object):
116120
app.add_config_value('numpydoc_edit_link', None, False)
117121
app.add_config_value('numpydoc_use_plots', None, False)
118122
app.add_config_value('numpydoc_show_class_members', True, True)
123+
app.add_config_value('numpydoc_show_inherited_class_members', True, True)
119124
app.add_config_value('numpydoc_class_members_toctree', True, True)
120125

121126
# Extra mangling domains

numpydoc/tests/test_docscrape.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,46 @@ class Ignorable(object):
646646
else:
647647
assert 'Spammity index' in str(doc), str(doc)
648648

649+
class SubDummy(Dummy):
650+
"""
651+
Subclass of Dummy class.
652+
653+
"""
654+
def ham(self, c, d):
655+
"""Cheese\n\nNo cheese.\nOverloaded Dummy.ham"""
656+
pass
657+
658+
def bar(self, a, b):
659+
"""Bar\n\nNo bar"""
660+
pass
661+
662+
for cls in (ClassDoc, SphinxClassDoc):
663+
doc = cls(SubDummy, config=dict(show_class_members=True,
664+
show_inherited_class_members=False))
665+
assert 'Methods' in str(doc), (cls, str(doc))
666+
assert 'spam' not in str(doc), (cls, str(doc))
667+
assert 'ham' in str(doc), (cls, str(doc))
668+
assert 'bar' in str(doc), (cls, str(doc))
669+
assert 'spammity' not in str(doc), (cls, str(doc))
670+
671+
if cls is SphinxClassDoc:
672+
assert '.. autosummary::' in str(doc), str(doc)
673+
else:
674+
assert 'Spammity index' not in str(doc), str(doc)
675+
676+
doc = cls(SubDummy, config=dict(show_class_members=True,
677+
show_inherited_class_members=True))
678+
assert 'Methods' in str(doc), (cls, str(doc))
679+
assert 'spam' in str(doc), (cls, str(doc))
680+
assert 'ham' in str(doc), (cls, str(doc))
681+
assert 'bar' in str(doc), (cls, str(doc))
682+
assert 'spammity' in str(doc), (cls, str(doc))
683+
684+
if cls is SphinxClassDoc:
685+
assert '.. autosummary::' in str(doc), str(doc)
686+
else:
687+
assert 'Spammity index' in str(doc), str(doc)
688+
649689
def test_duplicate_signature():
650690
# Duplicate function signatures occur e.g. in ufuncs, when the
651691
# automatic mechanism adds one, and a more detailed comes from the

0 commit comments

Comments
 (0)