diff --git a/src/griffe/docstrings/numpy.py b/src/griffe/docstrings/numpy.py index 73b46727..c0e9c214 100644 --- a/src/griffe/docstrings/numpy.py +++ b/src/griffe/docstrings/numpy.py @@ -157,7 +157,7 @@ def _read_block_items(docstring: Docstring, offset: int) -> tuple[list[list[str] return items, index - 1 -def _read_block(docstring: Docstring, offset: int) -> tuple[str, int]: +def _read_block(docstring: Docstring, offset: int) -> tuple[str, int]: # noqa: WPS231 lines = docstring.lines if offset >= len(lines): return "", offset @@ -168,8 +168,14 @@ def _read_block(docstring: Docstring, offset: int) -> tuple[str, int]: # skip first empty lines while _is_empty_line(lines[index]): index += 1 + while index < len(lines): + is_empty = _is_empty_line(lines[index]) + if is_empty and _is_dash_line(lines[index + 1]): + break # Break if a new unnamed section is reached. + + if is_empty and index < len(lines) + 1 and _is_dash_line(lines[index + 2]): + break # Break if a new named section is reached. - while index < len(lines) and not (_is_empty_line(lines[index]) and _is_dash_line(lines[index + 1])): block.append(lines[index]) index += 1 diff --git a/tests/test_docstrings/test_numpy.py b/tests/test_docstrings/test_numpy.py index 80151edd..e62b6126 100644 --- a/tests/test_docstrings/test_numpy.py +++ b/tests/test_docstrings/test_numpy.py @@ -382,6 +382,28 @@ def test_examples_section(parse_numpy): assert examples.value[3][1].startswith(">>> a = 0 # doctest: +SKIP") +def test_examples_section_when_followed_by_named_section(parse_numpy): + """Parse examples section. + + Parameters: + parse_numpy: Parse function (fixture). + """ + docstring = """ + Examples + -------- + Hello, hello. + + Parameters + ---------- + foo : int + """ + + sections, _ = parse_numpy(docstring, trim_doctest_flags=False) + assert len(sections) == 2 + assert sections[0].kind is DocstringSectionKind.examples + assert sections[1].kind is DocstringSectionKind.parameters + + # ============================================================================================= # Annotations def test_prefer_docstring_type_over_annotation(parse_numpy):