Skip to content

Commit

Permalink
Merge pull request #55 from jaredkhan/fix-cleandoc
Browse files Browse the repository at this point in the history
fix #54: Use inspect.cleandoc for stripping docstring whitespace
  • Loading branch information
pawamoy authored Jun 27, 2020
2 parents e652a22 + 8009940 commit c817114
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/pytkdocs/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import inspect
import pkgutil
import re
import textwrap
from functools import lru_cache
from itertools import chain
from pathlib import Path
Expand Down Expand Up @@ -333,7 +332,7 @@ def get_class_documentation(self, node: ObjectNode, select_members=None) -> Clas
The documented class object.
"""
class_ = node.obj
docstring = textwrap.dedent(class_.__doc__ or "")
docstring = inspect.cleandoc(class_.__doc__ or "")
root_object = Class(name=node.name, path=node.dotted_path, file_path=node.file_path, docstring=docstring)

# Even if we don't select members, we want to correctly parse the docstring
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/first_line_class_docstring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class TheClass:
"""The first line of the docstring.
A bit more of the docstring.
"""

pass
7 changes: 7 additions & 0 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ def test_loading_class():
assert obj.docstring == "The class docstring."


def test_loading_class_with_multiline_docstring_starting_on_first_line():
"""Handle classes with multiline docstrings where the first line is next to the triple-quotes."""
loader = Loader()
obj = loader.get_object_documentation("tests.fixtures.first_line_class_docstring.TheClass")
assert obj.docstring == """The first line of the docstring.\n\nA bit more of the docstring."""


def test_loading_dataclass():
"""Handle dataclasses."""
loader = Loader()
Expand Down

0 comments on commit c817114

Please sign in to comment.