Skip to content

Commit 849f686

Browse files
committed
Use inspect.cleandoc rather than textwrap.dedent when appending docstrings.
1 parent e4c74d1 commit 849f686

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

domdf_python_tools/doctools.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# stdlib
3030
import builtins
3131
import platform
32-
from textwrap import dedent
32+
from inspect import cleandoc
3333
from types import MethodType
3434
from typing import Any, Callable, Dict, Optional, Sequence, Type, TypeVar, Union
3535

@@ -102,13 +102,13 @@ def append_doctring_from_another(target: Union[Type, Callable], original: Union[
102102
original_doc = original.__doc__
103103

104104
if isinstance(original_doc, str) and isinstance(target_doc, str):
105-
deindented_target_doc = dedent(target_doc)
106-
deindented_original_doc = dedent(original_doc)
105+
deindented_target_doc = cleandoc(target_doc)
106+
deindented_original_doc = cleandoc(original_doc)
107107

108108
target.__doc__ = deindented_target_doc + "\n" + deindented_original_doc
109109

110110
elif not isinstance(target_doc, str) and isinstance(original_doc, str):
111-
target.__doc__ = dedent(original_doc)
111+
target.__doc__ = cleandoc(original_doc)
112112

113113

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

tests/test_doctools.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ def opening_hours(self):
124124

125125
@doctools.append_docstring_from(Cafe.set_opening_hours)
126126
def set_opening_hours(self, opens_at, closes_at):
127-
"""
128-
I will not buy this record, it is scratched.
127+
"""I will not buy this record, it is scratched.
129128
"""
130129

131130
self._opens_at = opens_at
@@ -208,13 +207,13 @@ def test_decorators():
208207
assert SpamCafe.opening_hours.__doc__ == Cafe.opening_hours.__doc__
209208

210209
# set_opening_hours and ceil should have extra text at the beginning
211-
assert SpamCafe.set_opening_hours.__doc__.startswith("\nI will not buy this record, it is scratched.")
212-
assert doctools.deindent_string(SpamCafe.set_opening_hours.__doc__, ).endswith(
210+
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(
213212
doctools.deindent_string(Cafe.set_opening_hours.__doc__)
214213
)
215214
# Dedented both strings to be sure of equivalence
216215
assert SpamCafe.ceil.__doc__.startswith(
217-
"\nI don't know why the cafe has a ceil function, but we'd better document it properly.",
216+
"I don't know why the cafe has a ceil function, but we'd better document it properly.",
218217
)
219218
assert doctools.deindent_string(SpamCafe.ceil.__doc__).endswith(doctools.deindent_string(math.ceil.__doc__))
220219
# Dedented both strings to be sure of equivalence
@@ -224,9 +223,9 @@ def test_decorators():
224223
assert undocumented_function.__name__ == "undocumented_function"
225224
assert undocumented_function.__annotations__ == {'a': float, 'b': float, 'c': float, 'd': int, "return": float}
226225
assert partially_documented_function.__doc__.startswith(
227-
"\nThis function works like ``documented_function`` except it returns the result telepathically.",
226+
"This function works like ``documented_function`` except it returns the result telepathically.",
228227
)
229-
assert doctools.deindent_string(partially_documented_function.__doc__, ).endswith(
228+
assert (doctools.deindent_string(partially_documented_function.__doc__) + "\n").endswith(
230229
doctools.deindent_string(documented_function.__doc__)
231230
)
232231
# Dedented both strings to be sure of equivalence

0 commit comments

Comments
 (0)