3030import builtins
3131
3232
33- def document_object_from_another ( target , original ):
33+ def deindent_string ( string ):
3434 """
35- Sets the docstring of the `target` function to that of the `original` function.
35+ Removes all indentation from the given string
3636
37- This may be useful for subclasses or wrappers that use the same arguments.
37+ :param string:
38+ :type string: str
3839
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
4342 """
4443
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 )
4647
4748
48- def is_documented_by (original ):
49+ # Functions that do the work
50+ def document_object_from_another (target , original ):
4951 """
5052 Sets the docstring of the `target` function to that of the `original` function.
5153
5254 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
5360 """
5461
55- def wrapper (target ):
56- document_object_from_another (target , original )
57- return target
58-
59- return wrapper
62+ target .__doc__ = original .__doc__
6063
6164
6265def append_doctring_from_another (target , original ):
@@ -75,43 +78,22 @@ def append_doctring_from_another(target, original):
7578 :type original: any
7679 """
7780
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__ )
8383
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
10385
10486
10587def make_sphinx_links (input_string , builtins_list = None ):
10688 """
10789 Make proper sphinx links out of double-backticked strings in docstring.
10890
10991 i.e. \`\`str\`\` becomes \:class\:\`~python:str\`
110-
111-
92+
93+
11294 Make sure to have `'python': ('https://docs.python.org/3/', None),` in the
11395 `intersphinx_mapping` dict of your conf.py for sphinx.
114-
96+
11597 :param input_string: The string to process
11698 :type input_string: str
11799 :param builtins_list: A list of builtins to make links for
@@ -132,6 +114,39 @@ def make_sphinx_links(input_string, builtins_list=None):
132114 return working_string
133115
134116
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+
135150def sphinxify_docstring ():
136151 """
137152 Make proper sphinx links out of double-backticked strings in docstring.
0 commit comments