From 0dc8e57c50b2245687e13a8be8de7b93bdff9bde Mon Sep 17 00:00:00 2001 From: Wei Yen Date: Wed, 22 Feb 2017 06:56:15 +1100 Subject: [PATCH] Implement `trim_docstring` Thin wrapper around `inspect.clean_doc` --- graphene/utils/tests/test_trim_docstring.py | 21 +++++++++++++++++++++ graphene/utils/trim_docstring.py | 9 +++++++++ 2 files changed, 30 insertions(+) create mode 100644 graphene/utils/tests/test_trim_docstring.py create mode 100644 graphene/utils/trim_docstring.py 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