Skip to content

Commit

Permalink
fix: Fix retrieval of annotations from parent for Yields section in p…
Browse files Browse the repository at this point in the history
…roperties

Issue-298: #298
  • Loading branch information
pawamoy committed Aug 9, 2024
1 parent 3f6a71a commit 8a21f4d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/_griffe/docstrings/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def _read_yields_section(
else:
# try to retrieve the annotation from the docstring parent
with suppress(AttributeError, IndexError, KeyError, ValueError):
annotation = docstring.parent.returns # type: ignore[union-attr]
annotation = docstring.parent.annotation # type: ignore[union-attr]
if annotation.is_iterator:
yield_item = annotation.slice
elif annotation.is_generator:
Expand Down
4 changes: 2 additions & 2 deletions src/_griffe/docstrings/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ def _read_yields_section(
text = dedent("\n".join(item[1:]))
if annotation is None:
# try to retrieve the annotation from the docstring parent
with suppress(AttributeError, KeyError, ValueError):
annotation = docstring.parent.returns # type: ignore[union-attr]
with suppress(AttributeError, IndexError, KeyError, ValueError):
annotation = docstring.parent.annotation # type: ignore[union-attr]
if annotation.is_iterator:
yield_item = annotation.slice
elif annotation.is_generator:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_docstrings/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,30 @@ def test_extract_yielded_type_with_single_return_item(parse_google: ParserType,
assert yields[0].annotation.name == "int"


def test_yield_section_in_property(parse_google: ParserType) -> None:
"""No warnings when parsing Yields section in a property.
Parameters:
parse_google: Fixture parser.
"""
docstring = """
Summary.
Yields:
A number.
"""
sections, warnings = parse_google(
docstring,
parent=Attribute(
"prop",
annotation=parse_docstring_annotation("Iterator[int]", Docstring("d", parent=Attribute("a"))),
),
)
assert not warnings
yields = sections[1].value
assert yields[0].annotation.name == "int"


# =============================================================================================
# Receives sections
def test_parse_receives_tuple_in_generator(parse_google: ParserType) -> None:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_docstrings/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,32 @@ def test_extract_yielded_type_with_single_return_item(parse_numpy: ParserType, r
assert yields[0].annotation.name == "int"


def test_yield_section_in_property(parse_numpy: ParserType) -> None:
"""No warnings when parsing Yields section in a property.
Parameters:
parse_numpy: Fixture parser.
"""
docstring = """
Summary.
Yields
------
:
A number.
"""
sections, warnings = parse_numpy(
docstring,
parent=Attribute(
"prop",
annotation=parse_docstring_annotation("Iterator[int]", Docstring("d", parent=Attribute("a"))),
),
)
assert not warnings
yields = sections[1].value
assert yields[0].annotation.name == "int"


# =============================================================================================
# Receives sections
def test_parse_receives_tuple_in_generator(parse_numpy: ParserType) -> None:
Expand Down

0 comments on commit 8a21f4d

Please sign in to comment.