Skip to content

Commit 75b4024

Browse files
committed
fix: Fix reading Google-style return section
1 parent 14b18be commit 75b4024

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

Diff for: src/pytkdocs/parsers/docstrings/google.py

+19-18
Original file line numberDiff line numberDiff line change
@@ -393,28 +393,29 @@ def read_return_section(self, lines: List[str], start_index: int) -> Tuple[Optio
393393
A tuple containing a `Section` (or `None`) and the index at which to continue parsing.
394394
"""
395395
text, i = self.read_block(lines, start_index)
396-
annotation = self.context["annotation"]
397-
description = ""
398396

399-
# First try to get the annotation and description from the docstring
400-
if text:
401-
try:
402-
type_, text = text.split(":", 1)
403-
except ValueError:
404-
self.error("No type in return description")
405-
else:
406-
annotation = type_.lstrip()
407-
description = text.lstrip()
408-
409-
# If there was no annotation in the docstring then move to signature
410-
if annotation is empty and self.context["signature"]:
411-
annotation = self.context["signature"].return_annotation
412-
413-
# Early exit if there was no annotation in the docstring or the signature
414-
if annotation is empty and not text:
397+
# Early exit if there is no text in the return section
398+
if not text:
415399
self.error(f"Empty return section at line {start_index}")
416400
return None, i
417401

402+
# First try to get the annotation and description from the docstring
403+
try:
404+
type_, text = text.split(":", 1)
405+
except ValueError:
406+
description = text
407+
annotation = self.context["annotation"]
408+
# If there was no annotation in the docstring then move to signature
409+
if annotation is empty and self.context["signature"]:
410+
annotation = self.context["signature"].return_annotation
411+
else:
412+
annotation = type_.lstrip()
413+
description = text.lstrip()
414+
415+
# There was no type in the docstring and no annotation
416+
if annotation is empty:
417+
self.error("No return type/annotation in docstring/signature")
418+
418419
return Section(Section.Type.RETURN, AnnotatedObject(annotation, description)), i
419420

420421
def read_examples_section(self, lines: List[str], start_index: int) -> Tuple[Optional[Section], int]:

Diff for: tests/test_parsers/test_docstrings/test_google.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_property_docstring():
7676
prop = class_.attributes[0]
7777
sections, errors = prop.docstring_sections, prop.docstring_errors
7878
assert len(sections) == 2
79-
assert len(errors) == 1
79+
assert not errors
8080

8181

8282
def test_function_without_annotations():
@@ -101,7 +101,7 @@ def f(x, y, *, z):
101101
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
102102
assert len(sections) == 4
103103
assert len(errors) == 1
104-
assert "No type in return" in errors[0]
104+
assert "No return type/annotation in" in errors[0]
105105

106106

107107
def test_function_with_annotations():
@@ -125,7 +125,7 @@ def f(x: int, y: int, *, z: int) -> int:
125125

126126
sections, errors = parse(inspect.getdoc(f), inspect.signature(f))
127127
assert len(sections) == 4
128-
assert len(errors) == 1
128+
assert not errors
129129

130130

131131
def test_function_with_examples():

Diff for: tests/test_parsers/test_docstrings/test_numpy.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class DummyObject:
1414
def parse(docstring, signature=None, return_type=inspect.Signature.empty):
1515
"""Helper to parse a doctring."""
1616
return Numpy().parse(
17-
dedent(docstring).strip(), {"obj": DummyObject(), "signature": signature, "type": return_type},
17+
dedent(docstring).strip(),
18+
{"obj": DummyObject(), "signature": signature, "type": return_type},
1819
)
1920

2021

@@ -79,7 +80,7 @@ def test_property_docstring():
7980
prop = class_.attributes[0]
8081
sections, errors = prop.docstring_sections, prop.docstring_errors
8182
assert len(sections) == 2
82-
assert len(errors) == 1
83+
assert not errors
8384

8485

8586
def test_function_without_annotations():

0 commit comments

Comments
 (0)