Skip to content

Commit 096970f

Browse files
authored
feat: Add option to read return type of properties in their summary
Issue #137: #137 PR #206: #206
1 parent b0620f8 commit 096970f

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

docs/docstrings.md

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,19 @@ def foo(a, b):
122122
The parser accepts a few options:
123123

124124
- `ignore_init_summary`: Ignore the first line in `__init__` methods' docstrings.
125-
Useful when merging `__init__` docstring into class' docstrings
125+
Useful when merging `__init__` docstring into class' docstrings. Default: false.
126126
with mkdocstrings-python's [`merge_init_into_class`][merge_init] option. Default: false.
127-
- `trim_doctest_flags`: Remove the [doctest flags][] written as comments in `pycon` snippets within a docstring.
128-
These flags are used to alter the behavior of [doctest][] when testing docstrings,
129-
and should not be visible in your docs. Default: true.
130-
- `warn_unknown_params`: Warn about parameters documented in docstrings that do not appear in the signature. Default: true.
131127
- `returns_multiple_items`: Parse [Returns sections](#returns) as if they contain multiple items.
132128
It means that continuation lines must be indented. Default: true.
133129
- `returns_named_value`: Whether to parse `thing: Description` in [Returns sections](#returns) as a name and description,
134130
rather than a type and description. When true, type must be wrapped in parentheses: `(int): Description.`.
135-
When false, parentheses are optional but the items cannot be named: `int: Description`.
136-
131+
When false, parentheses are optional but the items cannot be named: `int: Description`. Default: true.
132+
- `returns_type_in_property_summary`: Whether to parse the return type of properties
133+
at the beginning of their summary: `str: Summary of the property`. Default: false.
134+
- `trim_doctest_flags`: Remove the [doctest flags][] written as comments in `pycon` snippets within a docstring.
135+
These flags are used to alter the behavior of [doctest][] when testing docstrings,
136+
and should not be visible in your docs. Default: true.
137+
- `warn_unknown_params`: Warn about parameters documented in docstrings that do not appear in the signature. Default: true.
137138

138139
#### Attributes
139140

@@ -1575,21 +1576,3 @@ Returns | ✅ | ✅ | [❌][issue-xref-sphinx-returns]
15751576
[issue-xref-sphinx-yields]: https://github.com/mkdocstrings/griffe/issues/26
15761577
[issue-xref-numpy-func-cls]: https://github.com/mkdocstrings/griffe/issues/200
15771578
[issue-xref-google-func-cls]: https://github.com/mkdocstrings/griffe/issues/199
1578-
1579-
### Parsing options
1580-
1581-
Option | Description | Google | Numpy | Sphinx
1582-
-------------------------- | ----------------------------------------- | ------ | ----- | ------
1583-
`ignore_init_summary` | Ignore `__init__` summary. | ✅ | ✅ | [][issue-ignore-init-summary-sphinx]
1584-
`trim_doctest_flags` | Trim doctest flags. | ✅ | ✅ | [][issue-trim-doctest-flags-sphinx]
1585-
`warn_unknown_params` | Warn about unknown params. | ✅ | ✅ | [][issue-warn-unknown-params-sphinx]
1586-
`allow_section_blank_line` | Allow blank line in sections. | / | ✅ | /
1587-
`returns_multiple_items` | Parse multiple items in Returns sections. | ✅ | / | /
1588-
1589-
[issue-ignore-init-summary-sphinx]: https://github.com/mkdocstrings/griffe/issues/45
1590-
[issue-trim-doctest-flags-sphinx]: https://github.com/mkdocstrings/griffe/issues/49
1591-
[issue-warn-unknown-params-sphinx]: https://github.com/mkdocstrings/griffe/issues/64
1592-
1593-
[merge_init]: https://mkdocstrings.github.io/python/usage/configuration/docstrings/#merge_init_into_class
1594-
[doctest flags]: https://docs.python.org/3/library/doctest.html#option-flags
1595-
[doctest]: https://docs.python.org/3/library/doctest.html#module-doctest

src/griffe/docstrings/google.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ def parse(
699699
returns_multiple_items: bool = True,
700700
warn_unknown_params: bool = True,
701701
returns_named_value: bool = True,
702+
returns_type_in_property_summary: bool = False,
702703
**options: Any,
703704
) -> list[DocstringSection]:
704705
"""Parse a docstring.
@@ -715,6 +716,8 @@ def parse(
715716
returns_named_value: Whether to parse `thing: Description` in returns sections as a name and description,
716717
rather than a type and description. When true, type must be wrapped in parentheses: `(int): Description.`.
717718
When false, parentheses are optional but the items cannot be named: `int: Description`.
719+
returns_type_in_property_summary: Whether to parse the return type of properties
720+
at the beginning of their summary: `str: Summary of the property`.
718721
**options: Additional parsing options.
719722
720723
Returns:
@@ -732,6 +735,7 @@ def parse(
732735
"returns_multiple_items": returns_multiple_items,
733736
"warn_unknown_params": warn_unknown_params,
734737
"returns_named_value": returns_named_value,
738+
"returns_type_in_property_summary": returns_type_in_property_summary,
735739
**options,
736740
}
737741

@@ -829,6 +833,24 @@ def parse(
829833
if current_section:
830834
sections.append(DocstringSectionText("\n".join(current_section).rstrip("\n")))
831835

836+
if (
837+
returns_type_in_property_summary
838+
and sections
839+
and docstring.parent
840+
and docstring.parent.is_attribute
841+
and "property" in docstring.parent.labels
842+
):
843+
lines = sections[0].value.lstrip().split("\n")
844+
if ":" in lines[0]:
845+
annotation, line = lines[0].split(":", 1)
846+
lines = [line, *lines[1:]]
847+
sections[0].value = "\n".join(lines)
848+
sections.append(
849+
DocstringSectionReturns(
850+
[DocstringReturn("", description="", annotation=parse_annotation(annotation, docstring))],
851+
),
852+
)
853+
832854
return sections
833855

834856

tests/test_docstrings/test_google.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,3 +1477,22 @@ def test_type_in_returns_without_parentheses(parse_google: ParserType) -> None:
14771477
assert retval.name == ""
14781478
assert retval.annotation is None
14791479
assert retval.description == "Description\non several lines."
1480+
1481+
1482+
def test_reading_property_type_in_summary(parse_google: ParserType) -> None:
1483+
"""Assert we can parse the return type of properties in their summary.
1484+
1485+
Parameters:
1486+
parse_google: Fixture parser.
1487+
"""
1488+
docstring = "str: Description of the property."
1489+
parent = Attribute("prop")
1490+
parent.labels.add("property")
1491+
sections, warnings = parse_google(docstring, returns_type_in_property_summary=True, parent=parent)
1492+
assert len(sections) == 2
1493+
assert sections[0].kind is DocstringSectionKind.text
1494+
assert sections[1].kind is DocstringSectionKind.returns
1495+
retval = sections[1].value[0]
1496+
assert retval.name == ""
1497+
assert retval.annotation.name == "str"
1498+
assert retval.description == ""

0 commit comments

Comments
 (0)