/
_style_store.py
514 lines (381 loc) · 14.3 KB
/
_style_store.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
from __future__ import absolute_import
from ._doc_parse_tools import (
merge_google_napoleon_docs,
merge_numpy_docs,
merge_numpy_napoleon_docs,
merge_rest_docs,
)
""" Docstring inheritance-style implementations.
To implement your own inheritance file, simply write a function that fits the template:
def your_style(prnt_doc, child_doc):
''' Merges parent and child docstrings
Parameters
----------
prnt_cls_doc: Optional[str]
child_doc: Optional[str]
Returns
------
Optional[str]
The merged docstring that will be utilized.'''
return final_docstring
and log this using `custom_inherit.add_style(your_style)`. To permanently save your function,
define your function within custom_inherit/_style_store.py, and log it in custom_inherit.style_store.__all__.
Your style will then be available as 'your_style' (i.e. whatever you named the function).
"""
# All built-in styles must be logged in the __all__ field.
__all__ = [
"parent",
"numpy",
"reST",
"google",
"numpy_napoleon",
"google_with_merge",
"numpy_napoleon_with_merge",
"numpy_with_merge",
]
def parent(prnt_doc, child_doc):
"""Wherever the docstring for a child-class' attribute (or for the class itself) is
`None`, inherit the corresponding docstring from the parent.
*NOTE* As of Python 3.5, this is the default behavior of the built-in function
inspect.getdoc, and thus this style is deprecated Python 3.(>=5)."""
return child_doc if child_doc is not None else prnt_doc
def numpy(prnt_doc, child_doc):
"""Merges numpy-styled docstrings from the parent and child.
Specifically, any docstring section that appears in the parent's docstring that
is not present in the child's is inherited. Otherwise, the child's docstring
section is utilized. An exception to this is if the parent docstring contains a
"Raises" section, but the child's attribute's docstring contains a "Returns" or
"Yields" section instead. In this instance, the "Raises" section will not appear in the
inherited docstring.
Any whitespace that can be uniformly removed from a docstring's second line and onwards is
removed. Sections in the resulting docstring will be separated by a single blank line.
For details on the numpy docstring style, see:
https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt
Example:
- parent's docstring:
''' Parent's line
Parameters
----------
x: int
description of x
y: Union[None, int]
description of y
Raises
------
NotImplemented Error'''
- child's docstring:
''' Child's line
Returns
-------
int
Notes
-----
notes blah blah'''
- docstring that is ultimately inherited:
''' Child's line
Parameters
----------
x: int
description of x
y: Union[None, int]
description of y
Returns
-------
int
Notes
-----
notes blah blah'''"""
return merge_numpy_docs(prnt_doc, child_doc)
def reST(prnt_doc, child_doc):
"""Merge two reST-style docstrings into a single docstring.
Given the reST-style docstrings from a parent and child's attributes, merge the docstring
sections such that the child's section is used, wherever present, otherwise the parent's
section is used.
Sections are delimited by any type of reST section title. For more details, see:
http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#sections
Any whitespace that can be uniformly removed from a docstring's second line and onwards is
removed. Sections in the resulting docstring will be separated by a single blank line.
Example:
parent - ''' Header1
-------
parent's content for Header 1
- indented material
Header2
~~~~~~~
content for Header2'''
child - ''' Front-matter
~~~~~~~~~
NewHeader
~~~~~~~~~
content for NewHeader
Header2
+++++++
child's content for Header2'''
merged - ''' Front-matter
Header1
-------
content for Header 1
- indented material
Header2
+++++++
child's content for Header2
~~~~~~~~~
NewHeader
~~~~~~~~~
content for NewHeader '''
"""
return merge_rest_docs(prnt_doc, child_doc)
def numpy_napoleon(prnt_doc, child_doc):
"""Behaves identically to the 'numpy' style, but abides by the docstring sections
specified by the "Napoleon" standard.
For more info regarding the Napoleon standard, see:
http://sphinxcontrib-napoleon.readthedocs.io/en/latest/index.html#docstring-sections
Example:
- parent's docstring:
''' Parent's line
Keyword Arguments
-----------------
x: int
description of x
y: Union[None, int]
description of y
Raises
------
NotImplemented Error'''
- child's docstring:
''' Child's line
Returns
-------
int
Notes
-----
notes blah blah'''
- docstring that is ultimately inherited:
''' Child's line
Keyword Arguments
-----------------
x: int
description of x
y: Union[None, int]
description of y
Returns
-------
int
Notes
-----
notes blah blah'''
"""
return merge_numpy_napoleon_docs(prnt_doc, child_doc)
def google(prnt_doc, child_doc):
"""Merges google-styled docstrings from the parent and child, abiding to the docstring sections
specified by the "Napoleon" standard.
Specifically, any docstring section that appears in the parent's docstring that
is not present in the child's is inherited. Otherwise, the child's docstring
section is utilized. An exception to this is if the parent docstring contains a
"Raises" section, but the child's attribute's docstring contains a "Returns" or
"Yields" section instead. In this instance, the "Raises" section will not appear in the
inherited docstring.
Any whitespace that can be uniformly removed from a docstring's second line and onwards is
removed. Sections in the resulting docstring will be separated by a single blank line.
For more info regarding the Napoleon standard, see:
http://sphinxcontrib-napoleon.readthedocs.io/en/latest/index.html#docstring-sections
For details on the google docstring style, see:
http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html#example-google
Example:
- parent's docstring:
''' Parent's line
Args:
x: int
description of x
y: Union[None, int]
description of y
Raises:
NotImplemented Error'''
- child's docstring:
''' Child's line
Returns:
int
Notes:
notes blah blah'''
- docstring that is ultimately inherited:
''' Child's line
Parameters:
x: int
description of x
y: Union[None, int]
description of y
Returns:
int
Notes:
notes blah blah'''
"""
return merge_google_napoleon_docs(prnt_doc, child_doc)
def google_with_merge(prnt_doc, child_doc):
"""Behaves identically to the 'google' style, but also merges sections that
overlap, instead of only keeping the child's section. All sections are
concerned except sections "Short Summary", "Example" and "Examples" (or
coresponding aliases) for which the 'google' style applies.
Example: - parent's docstring:
''' Parent's line
Args:
x: int
description of x
y: Union[None, int]
description of y
Raises:
NotImplemented Error
Example:
>>> parent_func(x=3, y=None)
NotImplementedError:'''
- child's docstring:
''' Child's line
Args:
z: Union[None, int]
description of z
Returns:
int
Example:
>>> child_func(x=3, y=None, z=4)
7
Notes:
notes blah blah'''
- docstring that is ultimately inherited:
''' Child's line
Parameters:
x: int
description of x
y: Union[None, int]
description of y
z: Union[None, int]
description of z
Returns:
int
Example:
>>> child_func(x=3, y=None, z=4)
7
Notes:
notes blah blah'''
"""
return merge_google_napoleon_docs(prnt_doc, child_doc, merge_within_sections=True)
def numpy_napoleon_with_merge(prnt_doc, child_doc):
"""
Behaves identically to the 'numpy_napoleon' style, but also merges sections
that overlap, instead of only keeping the child's section. All sections are
concerned except sections "Short Summary", "Example" and "Examples" (or
coresponding aliases) for which the 'numpy_napoleon' style behaviour
applies.
Example: - parent's docstring:
''' Parent's line
Keyword Arguments
-----------------
x: int
description of x
y: Union[None, int]
description of y
Raises
------
NotImplemented Error
Example
-------
>>> parent_func(x=3, y=None)
NotImplementedError:'''
- child's docstring:
''' Child's line
Keyword Arguments
-----------------
z: Union[None, int]
description of z
Returns
-------
int
Notes
-----
notes blah blah
Example
-------
>>> child_func(x=3, y=None, z=4)
7'''
- docstring that is ultimately inherited:
''' Child's line
Keyword Arguments
-----------------
x: int
description of x
y: Union[None, int]
description of y
z: Union[None, int]
description of z
Returns
-------
int
Notes
-----
notes blah blah
Example
-------
>>> child_func(x=3, y=None, z=4)
7'''
"""
return merge_numpy_napoleon_docs(prnt_doc, child_doc, merge_within_sections=True)
def numpy_with_merge(prnt_doc, child_doc):
"""
Behaves identically to the 'numpy' style, but also merges sections that
overlap, instead of only keeping the child's section. All sections are
concerned except sections "Short Summary", "Extended Summary", "Deprecation
Warning" and "Examples" for which the 'numpy' style behaviour applies.
Example:
- parent's docstring:
''' Parent's line
Parameters
----------
x: int
parent's description of x
y: Union[None, int]
parent's description of y
Raises
------
NotImplemented Error
Example
-------
>>> parent_func(x=3, y=None)
NotImplementedError:'''
- child's docstring:
''' Child's line
Parameters
----------
y: int
childs's description of y
z: Union[None, int]
child's description of z
Returns
-------
int
Notes
-----
notes blah blah
Example
-------
>>> child_func(x=3, y=None, z=4)
7'''
- docstring that is ultimately inherited:
''' Child's line
Parameters
----------
x: int
parent's description of x
y: int
childs's description of y
z: Union[None, int]
child's description of z
Returns
-------
int
Notes
-----
notes blah blah
Example
-------
>>> child_func(x=3, y=None, z=4)
7'''
"""
return merge_numpy_docs(prnt_doc, child_doc, merge_within_sections=True)