Skip to content

autosummary: Add an option to skip inherited members #4029

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion sphinx/ext/autosummary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,8 @@ def process_generate_options(app):

generate_autosummary_docs(genfiles, builder=app.builder,
warn=logger.warning, info=logger.info,
suffix=suffix, base_path=app.srcdir)
suffix=suffix, base_path=app.srcdir,
inherited_members=app.config.autosummary_inherited_members)


def setup(app):
Expand All @@ -638,4 +639,5 @@ def setup(app):
app.connect('doctree-read', process_autosummary_toc)
app.connect('builder-inited', process_generate_options)
app.add_config_value('autosummary_generate', [], True, [bool])
app.add_config_value('autosummary_inherited_members', 'all', 'env', [str])
return {'version': sphinx.__display_version__, 'parallel_read_safe': True}
34 changes: 31 additions & 3 deletions sphinx/ext/autosummary/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,38 @@ def _underline(title, line='='):

# -- Generating output ---------------------------------------------------------

def _should_skip_inherited_member(obj, membername, inherited_members):
"""Determine if a member should be omitted from documentation according to
the inherited_members config value.
"""

assert inherited_members in ('all', 'overridden', 'none'), inherited_members
if inherited_members == 'all':
# Document all inherited members
return False

method = getattr(obj, membername)
base_methods = [
getattr(base, membername) for base in obj.__bases__ if hasattr(base, membername)]

if inherited_members == 'overridden':
# Document only overridden members
return any(_ is method for _ in base_methods)
elif inherited_members == 'none':
# Document only new members
return len(base_methods) > 0
else:
assert False


def generate_autosummary_docs(sources, output_dir=None, suffix='.rst',
warn=_simple_warn, info=_simple_info,
base_path=None, builder=None, template_dir=None,
imported_members=False):
imported_members=False, inherited_members=None):
# type: (List[unicode], unicode, unicode, Callable, Callable, unicode, Builder, unicode, bool) -> None # NOQA
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is the cause of your Travis CI build failure. I imagine you need to add unicode to the first tuple, like so:

# type: (List[unicode], unicode, unicode, Callable, Callable, unicode, Builder, unicode, bool, unicode)


if inherited_members not in ('all', 'overridden', 'none'):
raise ValueError('Invalid value for inherited_members: %s' % inherited_members)
showed_sources = list(sorted(sources))
if len(showed_sources) > 20:
showed_sources = showed_sources[:10] + ['...'] + showed_sources[-10:]
Expand Down Expand Up @@ -196,7 +222,8 @@ def get_members(obj, typ, include_public=[], imported=False):
documenter = get_documenter(value, obj)
if documenter.objtype == typ:
if typ == 'method':
items.append(name)
if not _should_skip_inherited_member(obj, name, inherited_members):
items.append(name)
elif imported or getattr(value, '__module__', None) == obj.__name__:
# skip imported members if expected
items.append(name)
Expand Down Expand Up @@ -246,7 +273,8 @@ def get_members(obj, typ, include_public=[], imported=False):
generate_autosummary_docs(new_files, output_dir=output_dir,
suffix=suffix, warn=warn, info=info,
base_path=base_path, builder=builder,
template_dir=template_dir)
template_dir=template_dir,
inherited_members=inherited_members)


# -- Finding documented entries in files ---------------------------------------
Expand Down