Skip to content

Commit

Permalink
Allow named sections after numpy examples
Browse files Browse the repository at this point in the history
  • Loading branch information
FasterSpeeding committed Feb 24, 2022
1 parent 46eddac commit 8674ec9
Show file tree
Hide file tree
Showing 2 changed files with 44 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
36 changes: 36 additions & 0 deletions tests/test_docstrings/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,42 @@ 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.
>>> 42 * 1
42
```python
>>> print("Hello again.")
```
>>> a = 2 # doctest: +SKIP
>>> b = a + 3
>>> print(b)
5
Bye.
Parameters
----------
Not in the section.
"""

sections, _ = parse(docstring, trim_doctest_flags=False)
assert len(sections) == 2
examples = sections[0]
assert len(examples.value) == 5
assert examples.value[0] == (DocstringSectionKind.text, "Hello, hello.")
assert examples.value[1] == (DocstringSectionKind.examples, ">>> 42 * 1\n42")
assert examples.value[3][1].startswith(">>> a = 2 # doctest: +SKIP")


# =============================================================================================
# Annotations
def test_prefer_docstring_type_over_annotation():
Expand Down

0 comments on commit 8674ec9

Please sign in to comment.