Skip to content

Commit 4ebd89a

Browse files
committed
Use StringList for append_docstring_from_another to keep output consistent.
1 parent 849f686 commit 4ebd89a

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

domdf_python_tools/doctools.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from typing import Any, Callable, Dict, Optional, Sequence, Type, TypeVar, Union
3535

3636
# this package
37+
from domdf_python_tools.stringlist import StringList
3738
from domdf_python_tools.typing import MethodDescriptorType, MethodWrapperType, WrapperDescriptorType
3839

3940
__all__ = [
@@ -102,13 +103,16 @@ def append_doctring_from_another(target: Union[Type, Callable], original: Union[
102103
original_doc = original.__doc__
103104

104105
if isinstance(original_doc, str) and isinstance(target_doc, str):
105-
deindented_target_doc = cleandoc(target_doc)
106-
deindented_original_doc = cleandoc(original_doc)
107-
108-
target.__doc__ = deindented_target_doc + "\n" + deindented_original_doc
106+
docstring = StringList(cleandoc(target_doc))
107+
docstring.blankline(ensure_single=True)
108+
docstring.append(cleandoc(original_doc))
109+
docstring.blankline(ensure_single=True)
110+
target.__doc__ = str(docstring)
109111

110112
elif not isinstance(target_doc, str) and isinstance(original_doc, str):
111-
target.__doc__ = cleandoc(original_doc)
113+
docstring = StringList(cleandoc(original_doc))
114+
docstring.blankline(ensure_single=True)
115+
target.__doc__ = str(docstring)
112116

113117

114118
def make_sphinx_links(input_string: str, builtins_list: Optional[Sequence[str]] = None) -> str:

tests/test_doctools.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ def test_decorators():
208208

209209
# set_opening_hours and ceil should have extra text at the beginning
210210
assert SpamCafe.set_opening_hours.__doc__.startswith("I will not buy this record, it is scratched.")
211-
assert (doctools.deindent_string(SpamCafe.set_opening_hours.__doc__) + "\n").endswith(
212-
doctools.deindent_string(Cafe.set_opening_hours.__doc__)
213-
)
211+
assert (doctools.deindent_string(SpamCafe.set_opening_hours.__doc__
212+
)).endswith(doctools.deindent_string(Cafe.set_opening_hours.__doc__))
214213
# Dedented both strings to be sure of equivalence
215214
assert SpamCafe.ceil.__doc__.startswith(
216215
"I don't know why the cafe has a ceil function, but we'd better document it properly.",
217216
)
218-
assert doctools.deindent_string(SpamCafe.ceil.__doc__).endswith(doctools.deindent_string(math.ceil.__doc__))
217+
assert doctools.deindent_string(SpamCafe.ceil.__doc__
218+
).endswith(doctools.deindent_string(math.ceil.__doc__) + "\n")
219219
# Dedented both strings to be sure of equivalence
220220

221221
# Functions
@@ -225,9 +225,8 @@ def test_decorators():
225225
assert partially_documented_function.__doc__.startswith(
226226
"This function works like ``documented_function`` except it returns the result telepathically.",
227227
)
228-
assert (doctools.deindent_string(partially_documented_function.__doc__) + "\n").endswith(
229-
doctools.deindent_string(documented_function.__doc__)
230-
)
228+
assert (doctools.deindent_string(partially_documented_function.__doc__
229+
)).endswith(doctools.deindent_string(documented_function.__doc__))
231230
# Dedented both strings to be sure of equivalence
232231
assert DummyClass.function_in_class_with_same_args.__doc__ == documented_function.__doc__
233232
assert DummyClass.function_in_class_with_same_args.__name__ == "function_in_class_with_same_args"
@@ -261,10 +260,10 @@ def funD():
261260
assert funC.__doc__ == "World"
262261

263262
doctools.append_doctring_from_another(funB, funC)
264-
assert funB.__doc__ == "Hello\nWorld"
263+
assert funB.__doc__ == "Hello\n\nWorld\n"
265264

266265
doctools.append_doctring_from_another(funD, funB)
267-
assert funD.__doc__ == "Hello\nWorld"
266+
assert funD.__doc__ == "Hello\n\nWorld\n"
268267

269268

270269
def test_still_callable():

0 commit comments

Comments
 (0)