Skip to content

Commit d9c8d67

Browse files
authored
ENH: Add support for dict show_inherited_class_members (#415)
* ENH: Add support for dict show_inherited_class_members * STY: Black * TST: Add test
1 parent c796da6 commit d9c8d67

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

doc/install.rst

+10-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,18 @@ numpydoc_show_class_members : bool
2626
Whether to show all members of a class in the Methods and Attributes
2727
sections automatically.
2828
``True`` by default.
29-
numpydoc_show_inherited_class_members : bool
29+
numpydoc_show_inherited_class_members : bool | dict
3030
Whether to show all inherited members of a class in the Methods and Attributes
3131
sections automatically. If it's false, inherited members won't shown.
32-
``True`` by default.
32+
``True`` by default. It can also be a dict mapping names of classes to
33+
boolean values (missing keys are treated as ``True``).
34+
For example, ``defaultdict(lambda: False, {'mymod.MyClass': True})``
35+
would only show inherited class members for ``MyClass``, whereas
36+
``{'mymod.MyClass': False}`` would show inherited class members for all
37+
classes except ``MyClass``. Note that disabling this for a limited set of
38+
classes might simultaneously require the use of a separate, custom
39+
autosummary class template with ``:no-inherited-members:`` in the
40+
``autoclass`` directive options.
3341
numpydoc_class_members_toctree : bool
3442
Whether to create a Sphinx table of contents for the lists of class
3543
methods and attributes. If a table of contents is made, Sphinx expects

numpydoc/numpydoc.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,18 @@ def clean_backrefs(app, doc, docname):
162162
def mangle_docstrings(app, what, name, obj, options, lines):
163163
if DEDUPLICATION_TAG in lines:
164164
return
165+
show_inherited_class_members = app.config.numpydoc_show_inherited_class_members
166+
if isinstance(show_inherited_class_members, dict):
167+
try:
168+
show_inherited_class_members = show_inherited_class_members[name]
169+
except KeyError:
170+
show_inherited_class_members = True
165171

166172
cfg = {
167173
"use_plots": app.config.numpydoc_use_plots,
168174
"use_blockquotes": app.config.numpydoc_use_blockquotes,
169175
"show_class_members": app.config.numpydoc_show_class_members,
170-
"show_inherited_class_members": app.config.numpydoc_show_inherited_class_members,
176+
"show_inherited_class_members": show_inherited_class_members,
171177
"class_members_toctree": app.config.numpydoc_class_members_toctree,
172178
"attributes_as_param_list": app.config.numpydoc_attributes_as_param_list,
173179
"xref_param_type": app.config.numpydoc_xref_param_type,
@@ -270,7 +276,9 @@ def setup(app, get_doc_object_=get_doc_object):
270276
app.add_config_value("numpydoc_use_plots", None, False)
271277
app.add_config_value("numpydoc_use_blockquotes", None, False)
272278
app.add_config_value("numpydoc_show_class_members", True, True)
273-
app.add_config_value("numpydoc_show_inherited_class_members", True, True)
279+
app.add_config_value(
280+
"numpydoc_show_inherited_class_members", True, True, types=(bool, dict)
281+
)
274282
app.add_config_value("numpydoc_class_members_toctree", True, True)
275283
app.add_config_value("numpydoc_citation_re", "[a-z0-9_.-]+", True)
276284
app.add_config_value("numpydoc_attributes_as_param_list", True, True)

numpydoc/tests/test_numpydoc.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pytest
2+
from collections import defaultdict
23
from io import StringIO
4+
from pathlib import PosixPath
35
from copy import deepcopy
46
from numpydoc.numpydoc import mangle_docstrings, _clean_text_signature, update_config
57
from numpydoc.xref import DEFAULT_LINKS
@@ -41,7 +43,7 @@ def __init__(self):
4143
self.warningiserror = False
4244

4345

44-
def test_mangle_docstrings():
46+
def test_mangle_docstrings_basic():
4547
s = """
4648
A top section before
4749
@@ -69,6 +71,35 @@ def test_mangle_docstrings():
6971
assert "upper" not in [x.strip() for x in lines]
7072

7173

74+
def test_mangle_docstrings_inherited_class_members():
75+
# if subclass docs are rendered, this PosixPath should have Path.samefile
76+
p = """
77+
A top section before
78+
79+
.. autoclass:: pathlib.PosixPath
80+
"""
81+
lines = p.split("\n")
82+
app = MockApp()
83+
mangle_docstrings(app, "class", "pathlib.PosixPath", PosixPath, {}, lines)
84+
lines = [x.strip() for x in lines]
85+
assert "samefile" in lines
86+
app.config.numpydoc_show_inherited_class_members = False
87+
lines = p.split("\n")
88+
mangle_docstrings(app, "class", "pathlib.PosixPath", PosixPath, {}, lines)
89+
lines = [x.strip() for x in lines]
90+
assert "samefile" not in lines
91+
app.config.numpydoc_show_inherited_class_members = dict()
92+
lines = p.split("\n")
93+
mangle_docstrings(app, "class", "pathlib.PosixPath", PosixPath, {}, lines)
94+
lines = [x.strip() for x in lines]
95+
assert "samefile" in lines
96+
app.config.numpydoc_show_inherited_class_members = defaultdict(lambda: False)
97+
lines = p.split("\n")
98+
mangle_docstrings(app, "class", "pathlib.PosixPath", PosixPath, {}, lines)
99+
lines = [x.strip() for x in lines]
100+
assert "samefile" not in lines
101+
102+
72103
def test_clean_text_signature():
73104
assert _clean_text_signature(None) is None
74105
assert _clean_text_signature("func($self)") == "func()"

0 commit comments

Comments
 (0)