Skip to content

Commit 5e08541

Browse files
committed
Restrict in-section merging to Parameters, Methods, Attributes, ...
1 parent e2b838f commit 5e08541

File tree

6 files changed

+199
-34
lines changed

6 files changed

+199
-34
lines changed

src/custom_inherit/_doc_parse_tools/napoleon_parse_tools.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ def parse_napoleon_doc(doc, style):
4040
"Attributes",
4141
"Methods",
4242
"Warning",
43-
"Notes",
4443
"Parameters",
4544
"Other Parameters",
4645
"Keyword Arguments",
4746
"Returns",
4847
"Yields",
4948
"Raises",
49+
"Notes",
5050
"Warns",
5151
"See Also",
5252
"References",
@@ -113,9 +113,10 @@ def merge_section(key, prnt_sec, child_sec, style, merge_within_sections=False):
113113

114114
napoleon_sections_that_cant_merge = [
115115
"Short Summary",
116-
"Example",
117116
"Examples",
117+
"Notes",
118118
]
119+
napoleon_sections_that_cant_merge.extend([ALIASES[section] for section in napoleon_sections_that_cant_merge if section in ALIASES])
119120

120121
if not prnt_sec and not child_sec:
121122
return None
@@ -132,14 +133,6 @@ def merge_section(key, prnt_sec, child_sec, style, merge_within_sections=False):
132133

133134
if key in section_items.SECTION_NAMES:
134135
body = section_items.merge(prnt_sec, child_sec, merge_within_sections, style)
135-
136-
elif merge_within_sections and key not in napoleon_sections_that_cant_merge:
137-
if child_sec is None:
138-
body = prnt_sec
139-
elif prnt_sec is None:
140-
body = child_sec
141-
else:
142-
body = '\n'.join((prnt_sec, child_sec))
143136
else:
144137
body = prnt_sec if child_sec is None else child_sec
145138

src/custom_inherit/_doc_parse_tools/numpy_parse_tools.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,6 @@ def merge_section(key, prnt_sec, child_sec, merge_within_sections=False):
8484
Optional[str]
8585
The output docstring section."""
8686

87-
doc_sections_that_cant_merge = [
88-
"Short Summary",
89-
"Deprecation Warning",
90-
"Extended Summary",
91-
"Examples"
92-
]
93-
9487
if not prnt_sec and not child_sec:
9588
return None
9689

@@ -101,14 +94,6 @@ def merge_section(key, prnt_sec, child_sec, merge_within_sections=False):
10194

10295
if key in section_items.SECTION_NAMES:
10396
body = section_items.merge(prnt_sec, child_sec, merge_within_sections, "numpy")
104-
105-
elif merge_within_sections and key not in doc_sections_that_cant_merge:
106-
if child_sec is None:
107-
body = prnt_sec
108-
elif prnt_sec is None:
109-
body = child_sec
110-
else:
111-
body = '\n'.join((prnt_sec, child_sec))
11297
else:
11398
body = prnt_sec if child_sec is None else child_sec
11499

src/custom_inherit/_doc_parse_tools/section_items.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ def indent(text, padding):
2121
"google": " " * 4,
2222
}
2323

24-
SECTION_NAMES = ("Attributes", "Parameters")
24+
SECTION_NAMES = {"Attributes", "Parameters", "Methods", "Other Parameters", "Args",
25+
"Arguments"
26+
"Keyword Args", "Keyword Arguments"}
2527

2628

2729
def _render(body, style):

src/custom_inherit/_metaclass_base.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
class DocInheritorBase(type):
18-
""" A metaclass that merges the respective docstrings of a parent class and of its child, along with their
18+
"""A metaclass that merges the respective docstrings of a parent class and of its child, along with their
1919
properties, methods (including classmethod, staticmethod, decorated methods).
2020
2121
This merge-style must be implemented via the static methods `class_doc_inherit`
@@ -45,9 +45,10 @@ def __new__(mcs, class_name, class_bases, class_dict):
4545
(FunctionType, MethodType, classmethod, staticmethod, property),
4646
)
4747
if (
48-
(attr.startswith("__") and attr.endswith("__") and not mcs.include_special_methods) or
49-
not is_doc_type
50-
):
48+
attr.startswith("__")
49+
and attr.endswith("__")
50+
and not mcs.include_special_methods
51+
) or not is_doc_type:
5152
continue
5253

5354
is_static_or_class = isinstance(attribute, (staticmethod, classmethod))
@@ -90,7 +91,7 @@ def __new__(mcs, class_name, class_bases, class_dict):
9091

9192
@staticmethod
9293
def class_doc_inherit(prnt_cls_doc, child_doc):
93-
""" Merge the docstrings of a parent class and its child.
94+
"""Merge the docstrings of a parent class and its child.
9495
9596
Parameters
9697
----------
@@ -104,7 +105,7 @@ def class_doc_inherit(prnt_cls_doc, child_doc):
104105

105106
@staticmethod
106107
def attr_doc_inherit(prnt_attr_doc, child_doc):
107-
""" Merge the docstrings of method or property from parent class and the corresponding
108+
"""Merge the docstrings of method or property from parent class and the corresponding
108109
attribute of its child.
109110
110111
Parameters

tests/default_styles_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ def child():
434434
"multi-line\n\nMethods:\n parent methods\n\nWarning:\n warnings\n\nParameters:\n "
435435
"parent's Parameters\n alias for Parameters - child's section\n\nOther Parameters:\n "
436436
"other\n\nKeyword Arguments:\n parent's section\n alias for Keyword Arguments - child's section"
437-
"\n\nReturns:\n return\n\nYields:\n alias of Yields - parent's\n yield\n\nRaises:\n "
437+
"\n\nReturns:\n return\n\nYields:\n yield\n\nRaises:\n "
438438
"raise\n\nNotes:\n note\n\nWarns:\n warns\n\nSee Also:\n see\n\nReferences:\n "
439439
"ref\n\nTodo:\n todo\n\nExamples:\n child's example"
440440
)
@@ -629,7 +629,7 @@ def child():
629629
"\nParameters\n----------\nparent's Parameters\nalias for Parameters - child's section\n"
630630
"\nOther Parameters\n----------------\nother\n\nKeyword Arguments\n-----------------\n"
631631
"parent's section\nalias for Keyword Arguments - child's section\n\nReturns\n-------\n"
632-
"return\n\nYields\n------\nalias of Yields - parent's\nyield\n\nRaises\n------\nraise\n"
632+
"return\n\nYields\n------\nyield\n\nRaises\n------\nraise\n"
633633
"\nNotes\n-----\nnote\n\nWarns\n-----\nwarns\n\nSee Also\n--------\nsee\n\nReferences\n"
634634
"----------\nref\n\nTodo\n----\ntodo\n\nExamples\n--------\nchild's example"
635635
)

tests/inheritance_test.py

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
import pytest
2+
3+
from custom_inherit import DocInheritMeta
4+
5+
6+
@pytest.mark.parametrize("style", ["numpy_with_merge", "numpy_napoleon_with_merge"])
7+
def test_inheritance_numpy_with_merge_styles(style):
8+
class Parent(metaclass=DocInheritMeta(style=style)):
9+
"""Parent.
10+
11+
Notes
12+
-----
13+
Blah
14+
"""
15+
16+
def meth(self, x, y=None):
17+
"""
18+
Raises
19+
------
20+
NotImplementedError"""
21+
raise NotImplementedError
22+
23+
class Child1(Parent):
24+
"""Child1.
25+
26+
Methods
27+
-------
28+
meth: does something
29+
"""
30+
31+
def meth(self, x, y=None):
32+
"""Method description.
33+
34+
Parameters
35+
----------
36+
x: int
37+
y: float"""
38+
return 0
39+
40+
class Child2(Parent):
41+
"""Child 2.
42+
43+
Methods
44+
-------
45+
blah : does not much
46+
"""
47+
48+
def blah(self):
49+
"""Method blah2."""
50+
pass
51+
52+
class GrandChild(Child1, Child2):
53+
"""Grand Child."""
54+
55+
def meth(self, x, y=None):
56+
"""
57+
Parameters
58+
----------
59+
x: int
60+
y: float"""
61+
return 0
62+
63+
pass
64+
65+
"""
66+
Grand Child.
67+
68+
Methods
69+
-------
70+
blah : does not much
71+
meth: does something
72+
73+
Notes
74+
-----
75+
Blah"""
76+
assert (
77+
GrandChild.__doc__
78+
== "Grand Child.\n\nMethods\n-------\nblah : does not much\nmeth: does something\n\nNotes\n-----\nBlah"
79+
)
80+
81+
"""
82+
Method description.
83+
84+
Parameters
85+
----------
86+
x: int
87+
y: float
88+
89+
Raises
90+
------
91+
NotImplementedError"""
92+
assert (
93+
GrandChild.meth.__doc__
94+
== "Method description.\n\nParameters\n----------\nx: int\ny: float\n\nRaises\n------\nNotImplementedError"
95+
)
96+
97+
98+
def test_inheritance_google_with_merge_style():
99+
class A(metaclass=DocInheritMeta(style="google_with_merge")):
100+
"""Testing A.
101+
102+
Args:
103+
a :
104+
parameter a.
105+
b :
106+
parameter b.
107+
c :
108+
parameter c.
109+
110+
Returns:
111+
None
112+
113+
Note:
114+
Hello
115+
"""
116+
117+
pass
118+
119+
class B(A):
120+
"""Testing B.
121+
122+
Parameters:
123+
d :
124+
parameter d.
125+
e :
126+
parameter e.
127+
f :
128+
parameter f.
129+
130+
Return:
131+
None
132+
"""
133+
134+
pass
135+
136+
class C(B):
137+
"""Testing C.
138+
139+
Args:
140+
g :
141+
parameter g.
142+
h :
143+
parameter h.
144+
i :
145+
parameter i.
146+
147+
Note:
148+
None
149+
"""
150+
151+
pass
152+
153+
"""
154+
Testing C.
155+
156+
Parameters:
157+
a :
158+
parameter a.
159+
b :
160+
parameter b.
161+
c :
162+
parameter c.
163+
d :
164+
parameter d.
165+
e :
166+
parameter e.
167+
f :
168+
parameter f.
169+
g :
170+
parameter g.
171+
h :
172+
parameter h.
173+
i :
174+
parameter i.
175+
176+
Returns:
177+
None
178+
179+
Notes:
180+
None"""
181+
assert (
182+
C.__doc__
183+
== "Testing C.\n\nParameters:\n a :\n parameter a.\n b :\n parameter b.\n c :\n parameter c.\n d :\n parameter d.\n e :\n parameter e.\n f :\n parameter f.\n g :\n parameter g.\n h :\n parameter h.\n i :\n parameter i.\n\nReturns:\n None\n\nNotes:\n None"
184+
)

0 commit comments

Comments
 (0)