Implement trim_docstring

Thin wrapper around `inspect.clean_doc`
This commit is contained in:
Wei Yen 2017-02-22 06:56:15 +11:00
parent d1d87221d5
commit 0dc8e57c50
2 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,21 @@
from ..trim_docstring import trim_docstring
def test_trim_docstring():
class WellDocumentedObject(object):
"""
This object is very well-documented. It has multiple lines in its
description.
Multiple paragraphs too
"""
pass
assert (trim_docstring(WellDocumentedObject.__doc__) ==
"This object is very well-documented. It has multiple lines in its\n"
"description.\n\nMultiple paragraphs too")
class UndocumentedObject(object):
pass
assert trim_docstring(UndocumentedObject.__doc__) is None

View File

@ -0,0 +1,9 @@
import inspect
def trim_docstring(docstring):
# Cleans up whitespaces from an indented docstring
#
# See https://www.python.org/dev/peps/pep-0257/
# and https://docs.python.org/2/library/inspect.html#inspect.cleandoc
return inspect.cleandoc(docstring) if docstring else None