Skip to content

Commit f314957

Browse files
committed
fix: Fix getting return type from parent property when parsing Sphinx docstrings
Issue-125: #125
1 parent bbb347c commit f314957

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/griffe/docstrings/sphinx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def _read_return(docstring: Docstring, offset: int, parsed_values: ParsedValues,
340340
annotation = parsed_values.return_type
341341
else:
342342
try:
343-
annotation = docstring.parent.returns # type: ignore[union-attr]
343+
annotation = docstring.parent.annotation # type: ignore[union-attr]
344344
except AttributeError:
345345
_warn(docstring, 0, f"No return type or annotation at '{parsed_directive.line}'")
346346
annotation = None

tests/test_docstrings/test_sphinx.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ def test_parse__raise_no_name__error(parse_sphinx: ParserType) -> None:
996996
assert "Failed to parse exception directive from" in warnings[0]
997997

998998

999-
def test_parse_module_attributes_section__expected_attributes_section(parse_sphinx: ParserType) -> None:
999+
def test_parse__module_attributes_section__expected_attributes_section(parse_sphinx: ParserType) -> None:
10001000
"""Parse attributes section in modules.
10011001
10021002
Parameters:
@@ -1035,3 +1035,20 @@ def test_parse_module_attributes_section__expected_attributes_section(parse_sphi
10351035
for index, expected in enumerate(expected_kwargs):
10361036
assert_attribute_equal(attr_section.value[index], DocstringAttribute(**expected)) # type: ignore[arg-type]
10371037
assert not warnings
1038+
1039+
1040+
def test_parse__properties_return_type(parse_sphinx: ParserType) -> None:
1041+
"""Parse attributes section in modules.
1042+
1043+
Parameters:
1044+
parse_sphinx: Fixture parser.
1045+
"""
1046+
docstring = """
1047+
Property that returns True for explaining the issue.
1048+
1049+
:return: True
1050+
"""
1051+
prop = Attribute("example", annotation="bool")
1052+
sections, warnings = parse_sphinx(docstring, parent=prop)
1053+
assert not warnings
1054+
assert sections[1].value[0].annotation == "bool"

0 commit comments

Comments
 (0)