|
17 | 17 | "exceptions:": Section.Type.EXCEPTIONS,
|
18 | 18 | "return:": Section.Type.RETURN,
|
19 | 19 | "returns:": Section.Type.RETURN,
|
| 20 | + "yield:": Section.Type.YIELD, |
| 21 | + "yields:": Section.Type.YIELD, |
20 | 22 | "example:": Section.Type.EXAMPLES,
|
21 | 23 | "examples:": Section.Type.EXAMPLES,
|
22 | 24 | "attribute:": Section.Type.ATTRIBUTES,
|
@@ -46,6 +48,7 @@ def __init__(self, replace_admonitions: bool = True) -> None:
|
46 | 48 | Section.Type.EXAMPLES: self.read_examples_section,
|
47 | 49 | Section.Type.ATTRIBUTES: self.read_attributes_section,
|
48 | 50 | Section.Type.RETURN: self.read_return_section,
|
| 51 | + Section.Type.YIELD: self.read_yield_section, |
49 | 52 | }
|
50 | 53 |
|
51 | 54 | def parse_sections(self, docstring: str) -> List[Section]: # noqa: D102
|
@@ -418,6 +421,43 @@ def read_return_section(self, lines: List[str], start_index: int) -> Tuple[Optio
|
418 | 421 |
|
419 | 422 | return Section(Section.Type.RETURN, AnnotatedObject(annotation, description)), i
|
420 | 423 |
|
| 424 | + def read_yield_section(self, lines: List[str], start_index: int) -> Tuple[Optional[Section], int]: |
| 425 | + """ |
| 426 | + Parse a "yields" section. |
| 427 | +
|
| 428 | + Arguments: |
| 429 | + lines: The return block lines. |
| 430 | + start_index: The line number to start at. |
| 431 | +
|
| 432 | + Returns: |
| 433 | + A tuple containing a `Section` (or `None`) and the index at which to continue parsing. |
| 434 | + """ |
| 435 | + text, i = self.read_block(lines, start_index) |
| 436 | + |
| 437 | + # Early exit if there is no text in the yield section |
| 438 | + if not text: |
| 439 | + self.error(f"Empty yield section at line {start_index}") |
| 440 | + return None, i |
| 441 | + |
| 442 | + # First try to get the annotation and description from the docstring |
| 443 | + try: |
| 444 | + type_, text = text.split(":", 1) |
| 445 | + except ValueError: |
| 446 | + description = text |
| 447 | + annotation = self.context["annotation"] |
| 448 | + # If there was no annotation in the docstring then move to signature |
| 449 | + if annotation is empty and self.context["signature"]: |
| 450 | + annotation = self.context["signature"].return_annotation |
| 451 | + else: |
| 452 | + annotation = type_.lstrip() |
| 453 | + description = text.lstrip() |
| 454 | + |
| 455 | + # There was no type in the docstring and no annotation |
| 456 | + if annotation is empty: |
| 457 | + self.error("No yield type/annotation in docstring/signature") |
| 458 | + |
| 459 | + return Section(Section.Type.YIELD, AnnotatedObject(annotation, description)), i |
| 460 | + |
421 | 461 | def read_examples_section(self, lines: List[str], start_index: int) -> Tuple[Optional[Section], int]:
|
422 | 462 | """
|
423 | 463 | Parse an "examples" section.
|
|
0 commit comments