Skip to content

Commit

Permalink
fix: Allow named sections after numpydoc examples
Browse files Browse the repository at this point in the history
  • Loading branch information
FasterSpeeding committed Feb 24, 2022
1 parent 46eddac commit c70699c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/griffe/docstrings/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
18 changes: 18 additions & 0 deletions tests/test_docstrings/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,24 @@ def test_examples_section():
assert examples.value[3][1].startswith(">>> a = 0 # doctest: +SKIP")


def test_examples_section_when_followed_by_named_section():
"""Parse examples section."""
docstring = """
Examples
--------
Hello, hello.
Parameters
----------
foo : int
"""

sections, _ = parse(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():
Expand Down

0 comments on commit c70699c

Please sign in to comment.