diff --git a/graphene/utils/tests/test_trim_docstring.py b/graphene/utils/tests/test_trim_docstring.py new file mode 100644 index 00000000..3aab5f11 --- /dev/null +++ b/graphene/utils/tests/test_trim_docstring.py @@ -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 diff --git a/graphene/utils/trim_docstring.py b/graphene/utils/trim_docstring.py new file mode 100644 index 00000000..a23c7e7d --- /dev/null +++ b/graphene/utils/trim_docstring.py @@ -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