diff --git a/CHANGELOG.md b/CHANGELOG.md index 9377c4da..2d321091 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ - General: Compose docstrings so they parse back the same - General: Unescape names in every style, such as `\*\*kwargs` or `class\_` - General: Make each run of whitespace in a type one space +- General: Clean attribute docstrings in `parse_from_object()` as other docstrings - ReST: Stop the last field from absorbing text after the field list (thanks to @apoorvdarshan) - ReST: Raise `ParseError`, not `IndexError`, for a field with no name - ReST: Indent compact continuation lines so a leading role isn't read as a field diff --git a/docstring_parser/attrdoc.py b/docstring_parser/attrdoc.py index 473c28ed..a65837e0 100644 --- a/docstring_parser/attrdoc.py +++ b/docstring_parser/attrdoc.py @@ -9,6 +9,7 @@ import typing as T from types import ModuleType +from ._lines import cleandoc from .common import Docstring, DocstringParam, Kind @@ -120,7 +121,8 @@ def add_attribute_docstrings( if arg_name not in documented: param = DocstringParam( key="attribute", - description=description, + # Cleaned as a docstring is, without its indent. + description=cleandoc(description) if description else None, arg_name=arg_name, type_name=type_name, is_optional=default is not None, diff --git a/docstring_parser/tests/test_parse_from_object.py b/docstring_parser/tests/test_parse_from_object.py index 3e395ffe..d2debb1e 100644 --- a/docstring_parser/tests/test_parse_from_object.py +++ b/docstring_parser/tests/test_parse_from_object.py @@ -182,3 +182,19 @@ def test_from_object_with_descriptor_doc() -> None: assert docstring.short_description is None assert len(docstring.meta) == 0 + + +def test_attribute_docstring_is_cleaned() -> None: + """Test that an attribute's docstring is cleaned as a docstring is.""" + + class WithAttr: + """Class docstring.""" + + value: int = 1 + """ + Indented + attribute doc. + """ + + docstring = parse_from_object(WithAttr) + assert docstring.attrs[0].description == "Indented\n attribute doc."