Skip to content

Commit

Permalink
fix: Fix parsing empty lines with indentation in Google docstrings
Browse files Browse the repository at this point in the history
Issue #129: #129
  • Loading branch information
pawamoy committed Feb 15, 2023
1 parent 1ff0918 commit 705edff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/griffe/docstrings/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ def _read_block_items(docstring: Docstring, offset: int) -> ItemsBlock: # noqa:
f"should be {indent} * 2 = {indent*2} spaces, not {cont_indent}",
)

elif _is_empty_line(line):
# empty line: preserve it in the current item
current_item[1].append("")

elif line.startswith(indent * " "):
# indent equal to initial one: new item
items.append(current_item)
current_item = (new_offset, [line[indent:]])

elif _is_empty_line(line):
# empty line: preserve it in the current item
current_item[1].append("")

else:
# indent lower than initial one: end of section
break
Expand Down
12 changes: 12 additions & 0 deletions tests/test_docstrings/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ def test_different_indentation(parse_google):
assert "should be 5 * 2 = 10 spaces, not 6" in warnings[0]


def test_empty_indented_lines_in_section_with_items(parse_google):
"""In sections with items, don't treat lines with just indentation as items.
Parameters:
parse_google: Fixture parser.
"""
docstring = "Returns:\n only_item: Description.\n \n \n\nSomething."
sections, _ = parse_google(docstring)
assert len(sections) == 2
assert len(sections[0].value) == 1


# =============================================================================================
# Annotations (general)
def test_parse_without_parent(parse_google):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_docstrings/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ def test_indented_code_block(parse_numpy):
assert not warnings


def test_empty_indented_lines_in_section_with_items(parse_numpy):
"""In sections with items, don't treat lines with just indentation as items.
Parameters:
parse_numpy: Fixture parser.
"""
docstring = "Returns\n-------\nonly_item : type\n Description.\n \n \n\nSomething."
sections, _ = parse_numpy(docstring)
assert len(sections) == 2
assert len(sections[0].value) == 1


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

0 comments on commit 705edff

Please # to comment.