From 705edff6c208281bdab387a464799de613b087b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Wed, 15 Feb 2023 21:23:50 +0100 Subject: [PATCH] fix: Fix parsing empty lines with indentation in Google docstrings Issue #129: https://github.com/mkdocstrings/griffe/issues/129 --- src/griffe/docstrings/google.py | 8 ++++---- tests/test_docstrings/test_google.py | 12 ++++++++++++ tests/test_docstrings/test_numpy.py | 12 ++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/griffe/docstrings/google.py b/src/griffe/docstrings/google.py index 31a85a37..7d0e20ef 100644 --- a/src/griffe/docstrings/google.py +++ b/src/griffe/docstrings/google.py @@ -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 diff --git a/tests/test_docstrings/test_google.py b/tests/test_docstrings/test_google.py index 7be2cad1..0fbee4fe 100644 --- a/tests/test_docstrings/test_google.py +++ b/tests/test_docstrings/test_google.py @@ -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): diff --git a/tests/test_docstrings/test_numpy.py b/tests/test_docstrings/test_numpy.py index b66da91a..dbc25aa8 100644 --- a/tests/test_docstrings/test_numpy.py +++ b/tests/test_docstrings/test_numpy.py @@ -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):