Skip to content

Commit

Permalink
fix: Fix parsing of Numpy docstring with an Examples section at the end
Browse files Browse the repository at this point in the history
Issue #97: #97
  • Loading branch information
pawamoy committed Sep 22, 2022
1 parent 9e5df0a commit 3114727
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/griffe/docstrings/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ def _read_block(docstring: Docstring, offset: int) -> tuple[str, int]: # noqa:
new_offset += 1
while new_offset < len(lines):
is_empty = _is_empty_line(lines[new_offset])
if is_empty and _is_dash_line(lines[new_offset + 1]):
if is_empty and new_offset < len(lines) - 1 and _is_dash_line(lines[new_offset + 1]):
break # Break if a new unnamed section is reached.

if is_empty and new_offset < len(lines) + 1 and _is_dash_line(lines[new_offset + 2]):
if is_empty and new_offset < len(lines) - 2 and _is_dash_line(lines[new_offset + 2]):
break # Break if a new named section is reached.

block.append(lines[new_offset])
Expand Down
26 changes: 25 additions & 1 deletion tests/test_docstrings/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ def test_examples_section(parse_numpy):


def test_examples_section_when_followed_by_named_section(parse_numpy):
"""Parse examples section.
"""Parse examples section followed by another section.
Parameters:
parse_numpy: Parse function (fixture).
Expand All @@ -498,6 +498,30 @@ def test_examples_section_when_followed_by_named_section(parse_numpy):
assert sections[1].kind is DocstringSectionKind.parameters


def test_examples_section_as_last(parse_numpy):
"""Parse examples section being last in the docstring.
Parameters:
parse_numpy: Parse function (fixture).
"""
docstring = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
Examples
--------
```python
>>> LoremIpsum.from_string("consectetur")
<foofoo: Ipsum.Lorem>
```
"""

sections, _ = parse_numpy(docstring)
assert len(sections) == 2
assert sections[0].kind is DocstringSectionKind.text
assert sections[1].kind is DocstringSectionKind.examples


# =============================================================================================
# Attributes sections
def test_retrieve_attributes_annotation_from_parent(parse_numpy):
Expand Down

0 comments on commit 3114727

Please sign in to comment.