diff --git a/src/labthings/utilities.py b/src/labthings/utilities.py index 9a4407c..f41a0cd 100644 --- a/src/labthings/utilities.py +++ b/src/labthings/utilities.py @@ -1,4 +1,5 @@ import copy +import inspect import operator import os import re @@ -152,12 +153,12 @@ def get_docstring(obj: Any, remove_newlines=True) -> str: """ ds = obj.__doc__ - if ds: + if not ds: + return "" + if remove_newlines: stripped = [line.strip() for line in ds.splitlines() if line] - if not remove_newlines: - return "\n".join(stripped) return " ".join(stripped).replace("\n", " ").replace("\r", "") - return "" + return inspect.cleandoc(ds) # Strip spurious indentation/newlines def get_summary(obj: Any) -> str: diff --git a/tests/test_utilities.py b/tests/test_utilities.py index fecd685..eab062a 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -30,6 +30,10 @@ def test_get_docstring(example_class): utilities.get_docstring(example_class) == "First line of class docstring. Second line of class docstring. " ) + assert ( + utilities.get_docstring(example_class, remove_newlines=False) + == "First line of class docstring.\nSecond line of class docstring." + ) assert utilities.get_docstring(example_class.class_method) == ( "First line of class method docstring. Second line of class method docstring. "