Skip to content

Commit c817114

Browse files
authored
Merge pull request #55 from jaredkhan/fix-cleandoc
fix #54: Use inspect.cleandoc for stripping docstring whitespace
2 parents e652a22 + 8009940 commit c817114

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Diff for: src/pytkdocs/loader.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import inspect
1010
import pkgutil
1111
import re
12-
import textwrap
1312
from functools import lru_cache
1413
from itertools import chain
1514
from pathlib import Path
@@ -333,7 +332,7 @@ def get_class_documentation(self, node: ObjectNode, select_members=None) -> Clas
333332
The documented class object.
334333
"""
335334
class_ = node.obj
336-
docstring = textwrap.dedent(class_.__doc__ or "")
335+
docstring = inspect.cleandoc(class_.__doc__ or "")
337336
root_object = Class(name=node.name, path=node.dotted_path, file_path=node.file_path, docstring=docstring)
338337

339338
# Even if we don't select members, we want to correctly parse the docstring

Diff for: tests/fixtures/first_line_class_docstring.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class TheClass:
2+
"""The first line of the docstring.
3+
4+
A bit more of the docstring.
5+
"""
6+
7+
pass

Diff for: tests/test_loader.py

+7
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ def test_loading_class():
124124
assert obj.docstring == "The class docstring."
125125

126126

127+
def test_loading_class_with_multiline_docstring_starting_on_first_line():
128+
"""Handle classes with multiline docstrings where the first line is next to the triple-quotes."""
129+
loader = Loader()
130+
obj = loader.get_object_documentation("tests.fixtures.first_line_class_docstring.TheClass")
131+
assert obj.docstring == """The first line of the docstring.\n\nA bit more of the docstring."""
132+
133+
127134
def test_loading_dataclass():
128135
"""Handle dataclasses."""
129136
loader = Loader()

0 commit comments

Comments
 (0)