30
30
import builtins
31
31
32
32
33
- def document_object_from_another ( target , original ):
33
+ def deindent_string ( string ):
34
34
"""
35
- Sets the docstring of the `target` function to that of the `original` function.
35
+ Removes all indentation from the given string
36
36
37
- This may be useful for subclasses or wrappers that use the same arguments.
37
+ :param string:
38
+ :type string: str
38
39
39
- :param target: The object to set the docstring for
40
- :type target: any
41
- :param original: The object to copy the docstring from
42
- :type original: any
40
+ :return:
41
+ :rtype: str
43
42
"""
44
43
45
- target .__doc__ = original .__doc__
44
+ split_string = string .split ("\n " )
45
+ deindented_string = [line .lstrip ("\t " ) for line in split_string ]
46
+ return "\n " .join (deindented_string )
46
47
47
48
48
- def is_documented_by (original ):
49
+ # Functions that do the work
50
+ def document_object_from_another (target , original ):
49
51
"""
50
52
Sets the docstring of the `target` function to that of the `original` function.
51
53
52
54
This may be useful for subclasses or wrappers that use the same arguments.
55
+
56
+ :param target: The object to set the docstring for
57
+ :type target: any
58
+ :param original: The object to copy the docstring from
59
+ :type original: any
53
60
"""
54
61
55
- def wrapper (target ):
56
- document_object_from_another (target , original )
57
- return target
58
-
59
- return wrapper
62
+ target .__doc__ = original .__doc__
60
63
61
64
62
65
def append_doctring_from_another (target , original ):
@@ -75,43 +78,22 @@ def append_doctring_from_another(target, original):
75
78
:type original: any
76
79
"""
77
80
78
- split_target_doc = target .__doc__ .split ("\n " )
79
- deindented_target_doc = [line .lstrip ("\t " ) for line in split_target_doc ]
80
-
81
- split_original_doc = original .__doc__ .split ("\n " )
82
- deindented_original_doc = [line .lstrip ("\t " ) for line in split_original_doc ]
81
+ deindented_target_doc = deindent_string (target .__doc__ )
82
+ deindented_original_doc = deindent_string (original .__doc__ )
83
83
84
- target .__doc__ = f"\n " .join (deindented_target_doc + deindented_original_doc )
85
-
86
-
87
- def append_docstring_from (original ):
88
- """
89
- Appends the docstring from the `original` function to the `target` function.
90
-
91
- This may be useful for subclasses or wrappers that use the same arguments.
92
-
93
- Any indentation in either docstring is removed to
94
- ensure consistent indentation between the two docstrings.
95
- Bear this in mind if additional indentation is used in the docstring.
96
- """
97
-
98
- def wrapper (target ):
99
- append_doctring_from_another (target , original )
100
- return target
101
-
102
- return wrapper
84
+ target .__doc__ = deindented_target_doc + "\n " + deindented_original_doc
103
85
104
86
105
87
def make_sphinx_links (input_string , builtins_list = None ):
106
88
"""
107
89
Make proper sphinx links out of double-backticked strings in docstring.
108
90
109
91
i.e. \`\`str\`\` becomes \:class\:\`~python:str\`
110
-
111
-
92
+
93
+
112
94
Make sure to have `'python': ('https://docs.python.org/3/', None),` in the
113
95
`intersphinx_mapping` dict of your conf.py for sphinx.
114
-
96
+
115
97
:param input_string: The string to process
116
98
:type input_string: str
117
99
:param builtins_list: A list of builtins to make links for
@@ -132,6 +114,39 @@ def make_sphinx_links(input_string, builtins_list=None):
132
114
return working_string
133
115
134
116
117
+ # Decorators that call the above functions
118
+ def is_documented_by (original ):
119
+ """
120
+ Sets the docstring of the `target` function to that of the `original` function.
121
+
122
+ This may be useful for subclasses or wrappers that use the same arguments.
123
+ """
124
+
125
+ def wrapper (target ):
126
+ document_object_from_another (target , original )
127
+ return target
128
+
129
+ return wrapper
130
+
131
+
132
+ def append_docstring_from (original ):
133
+ """
134
+ Appends the docstring from the `original` function to the `target` function.
135
+
136
+ This may be useful for subclasses or wrappers that use the same arguments.
137
+
138
+ Any indentation in either docstring is removed to
139
+ ensure consistent indentation between the two docstrings.
140
+ Bear this in mind if additional indentation is used in the docstring.
141
+ """
142
+
143
+ def wrapper (target ):
144
+ append_doctring_from_another (target , original )
145
+ return target
146
+
147
+ return wrapper
148
+
149
+
135
150
def sphinxify_docstring ():
136
151
"""
137
152
Make proper sphinx links out of double-backticked strings in docstring.
0 commit comments