Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow named sections after numpy examples #54

Merged
merged 2 commits into from
Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 22 additions & 0 deletions tests/test_docstrings/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down