|
11 | 11 | Attribute,
|
12 | 12 | Class,
|
13 | 13 | Docstring,
|
| 14 | + DocstringReceive, |
14 | 15 | DocstringReturn,
|
15 | 16 | DocstringSectionKind,
|
| 17 | + DocstringYield, |
16 | 18 | ExprName,
|
17 | 19 | Function,
|
18 | 20 | Module,
|
@@ -1407,6 +1409,148 @@ def test_parse_returns_multiple_items(
|
1407 | 1409 | assert annotated.description == expected_.description
|
1408 | 1410 |
|
1409 | 1411 |
|
| 1412 | +@pytest.mark.parametrize( |
| 1413 | + ("returns_multiple_items", "return_annotation", "expected"), |
| 1414 | + [ |
| 1415 | + ( |
| 1416 | + False, |
| 1417 | + None, |
| 1418 | + [DocstringYield("", description="XXXXXXX\n YYYYYYY\nZZZZZZZ", annotation=None)], |
| 1419 | + ), |
| 1420 | + ( |
| 1421 | + False, |
| 1422 | + "Iterator[tuple[int, int]]", |
| 1423 | + [DocstringYield("", description="XXXXXXX\n YYYYYYY\nZZZZZZZ", annotation="tuple[int, int]")], |
| 1424 | + ), |
| 1425 | + ( |
| 1426 | + True, |
| 1427 | + None, |
| 1428 | + [ |
| 1429 | + DocstringYield("", description="XXXXXXX\nYYYYYYY", annotation=None), |
| 1430 | + DocstringYield("", description="ZZZZZZZ", annotation=None), |
| 1431 | + ], |
| 1432 | + ), |
| 1433 | + ( |
| 1434 | + True, |
| 1435 | + "Iterator[tuple[int,int]]", |
| 1436 | + [ |
| 1437 | + DocstringYield("", description="XXXXXXX\nYYYYYYY", annotation="int"), |
| 1438 | + DocstringYield("", description="ZZZZZZZ", annotation="int"), |
| 1439 | + ], |
| 1440 | + ), |
| 1441 | + ], |
| 1442 | +) |
| 1443 | +def test_parse_yields_multiple_items( |
| 1444 | + parse_google: ParserType, |
| 1445 | + returns_multiple_items: bool, |
| 1446 | + return_annotation: str, |
| 1447 | + expected: list[DocstringYield], |
| 1448 | +) -> None: |
| 1449 | + """Parse Returns section with and without multiple items. |
| 1450 | +
|
| 1451 | + Parameters: |
| 1452 | + parse_google: Fixture parser. |
| 1453 | + returns_multiple_items: Whether the `Returns` and `Yields` sections have multiple items. |
| 1454 | + return_annotation: The return annotation of the function to parse. Usually an `Iterator`. |
| 1455 | + expected: The expected value of the parsed Yields section. |
| 1456 | + """ |
| 1457 | + parent = ( |
| 1458 | + Function("func", returns=parse_docstring_annotation(return_annotation, Docstring("d", parent=Function("f")))) |
| 1459 | + if return_annotation is not None |
| 1460 | + else None |
| 1461 | + ) |
| 1462 | + docstring = """ |
| 1463 | + Yields: |
| 1464 | + XXXXXXX |
| 1465 | + YYYYYYY |
| 1466 | + ZZZZZZZ |
| 1467 | + """ |
| 1468 | + sections, _ = parse_google( |
| 1469 | + docstring, |
| 1470 | + returns_multiple_items=returns_multiple_items, |
| 1471 | + parent=parent, |
| 1472 | + ) |
| 1473 | + |
| 1474 | + assert len(sections) == 1 |
| 1475 | + assert len(sections[0].value) == len(expected) |
| 1476 | + |
| 1477 | + for annotated, expected_ in zip(sections[0].value, expected): |
| 1478 | + assert annotated.name == expected_.name |
| 1479 | + assert str(annotated.annotation) == str(expected_.annotation) |
| 1480 | + assert annotated.description == expected_.description |
| 1481 | + |
| 1482 | + |
| 1483 | +@pytest.mark.parametrize( |
| 1484 | + ("receives_multiple_items", "return_annotation", "expected"), |
| 1485 | + [ |
| 1486 | + ( |
| 1487 | + False, |
| 1488 | + None, |
| 1489 | + [DocstringReceive("", description="XXXXXXX\n YYYYYYY\nZZZZZZZ", annotation=None)], |
| 1490 | + ), |
| 1491 | + ( |
| 1492 | + False, |
| 1493 | + "Generator[..., tuple[int, int], ...]", |
| 1494 | + [DocstringReceive("", description="XXXXXXX\n YYYYYYY\nZZZZZZZ", annotation="tuple[int, int]")], |
| 1495 | + ), |
| 1496 | + ( |
| 1497 | + True, |
| 1498 | + None, |
| 1499 | + [ |
| 1500 | + DocstringReceive("", description="XXXXXXX\nYYYYYYY", annotation=None), |
| 1501 | + DocstringReceive("", description="ZZZZZZZ", annotation=None), |
| 1502 | + ], |
| 1503 | + ), |
| 1504 | + ( |
| 1505 | + True, |
| 1506 | + "Generator[..., tuple[int, int], ...]", |
| 1507 | + [ |
| 1508 | + DocstringReceive("", description="XXXXXXX\nYYYYYYY", annotation="int"), |
| 1509 | + DocstringReceive("", description="ZZZZZZZ", annotation="int"), |
| 1510 | + ], |
| 1511 | + ), |
| 1512 | + ], |
| 1513 | +) |
| 1514 | +def test_parse_receives_multiple_items( |
| 1515 | + parse_google: ParserType, |
| 1516 | + receives_multiple_items: bool, |
| 1517 | + return_annotation: str, |
| 1518 | + expected: list[DocstringReceive], |
| 1519 | +) -> None: |
| 1520 | + """Parse Returns section with and without multiple items. |
| 1521 | +
|
| 1522 | + Parameters: |
| 1523 | + parse_google: Fixture parser. |
| 1524 | + receives_multiple_items: Whether the `Receives` section has multiple items. |
| 1525 | + return_annotation: The return annotation of the function to parse. Usually a `Generator`. |
| 1526 | + expected: The expected value of the parsed Receives section. |
| 1527 | + """ |
| 1528 | + parent = ( |
| 1529 | + Function("func", returns=parse_docstring_annotation(return_annotation, Docstring("d", parent=Function("f")))) |
| 1530 | + if return_annotation is not None |
| 1531 | + else None |
| 1532 | + ) |
| 1533 | + docstring = """ |
| 1534 | + Receives: |
| 1535 | + XXXXXXX |
| 1536 | + YYYYYYY |
| 1537 | + ZZZZZZZ |
| 1538 | + """ |
| 1539 | + sections, _ = parse_google( |
| 1540 | + docstring, |
| 1541 | + receives_multiple_items=receives_multiple_items, |
| 1542 | + parent=parent, |
| 1543 | + ) |
| 1544 | + |
| 1545 | + assert len(sections) == 1 |
| 1546 | + assert len(sections[0].value) == len(expected) |
| 1547 | + |
| 1548 | + for annotated, expected_ in zip(sections[0].value, expected): |
| 1549 | + assert annotated.name == expected_.name |
| 1550 | + assert str(annotated.annotation) == str(expected_.annotation) |
| 1551 | + assert annotated.description == expected_.description |
| 1552 | + |
| 1553 | + |
1410 | 1554 | def test_avoid_false_positive_sections(parse_google: ParserType) -> None:
|
1411 | 1555 | """Avoid false positive when parsing sections.
|
1412 | 1556 |
|
@@ -1490,6 +1634,80 @@ def test_type_in_returns_without_parentheses(parse_google: ParserType) -> None:
|
1490 | 1634 | assert retval.description == "Description\non several lines."
|
1491 | 1635 |
|
1492 | 1636 |
|
| 1637 | +def test_type_in_yields_without_parentheses(parse_google: ParserType) -> None: |
| 1638 | + """Assert we can parse the return type without parentheses. |
| 1639 | +
|
| 1640 | + Parameters: |
| 1641 | + parse_google: Fixture parser. |
| 1642 | + """ |
| 1643 | + docstring = """ |
| 1644 | + Summary. |
| 1645 | +
|
| 1646 | + Yields: |
| 1647 | + int: Description |
| 1648 | + on several lines. |
| 1649 | + """ |
| 1650 | + sections, warnings = parse_google(docstring, returns_named_value=False) |
| 1651 | + assert len(sections) == 2 |
| 1652 | + assert not warnings |
| 1653 | + retval = sections[1].value[0] |
| 1654 | + assert retval.name == "" |
| 1655 | + assert retval.annotation == "int" |
| 1656 | + assert retval.description == "Description\non several lines." |
| 1657 | + |
| 1658 | + docstring = """ |
| 1659 | + Summary. |
| 1660 | +
|
| 1661 | + Yields: |
| 1662 | + Description |
| 1663 | + on several lines. |
| 1664 | + """ |
| 1665 | + sections, warnings = parse_google(docstring, returns_named_value=False) |
| 1666 | + assert len(sections) == 2 |
| 1667 | + assert len(warnings) == 1 |
| 1668 | + retval = sections[1].value[0] |
| 1669 | + assert retval.name == "" |
| 1670 | + assert retval.annotation is None |
| 1671 | + assert retval.description == "Description\non several lines." |
| 1672 | + |
| 1673 | + |
| 1674 | +def test_type_in_receives_without_parentheses(parse_google: ParserType) -> None: |
| 1675 | + """Assert we can parse the return type without parentheses. |
| 1676 | +
|
| 1677 | + Parameters: |
| 1678 | + parse_google: Fixture parser. |
| 1679 | + """ |
| 1680 | + docstring = """ |
| 1681 | + Summary. |
| 1682 | +
|
| 1683 | + Receives: |
| 1684 | + int: Description |
| 1685 | + on several lines. |
| 1686 | + """ |
| 1687 | + sections, warnings = parse_google(docstring, receives_named_value=False) |
| 1688 | + assert len(sections) == 2 |
| 1689 | + assert not warnings |
| 1690 | + retval = sections[1].value[0] |
| 1691 | + assert retval.name == "" |
| 1692 | + assert retval.annotation == "int" |
| 1693 | + assert retval.description == "Description\non several lines." |
| 1694 | + |
| 1695 | + docstring = """ |
| 1696 | + Summary. |
| 1697 | +
|
| 1698 | + Receives: |
| 1699 | + Description |
| 1700 | + on several lines. |
| 1701 | + """ |
| 1702 | + sections, warnings = parse_google(docstring, receives_named_value=False) |
| 1703 | + assert len(sections) == 2 |
| 1704 | + assert len(warnings) == 1 |
| 1705 | + retval = sections[1].value[0] |
| 1706 | + assert retval.name == "" |
| 1707 | + assert retval.annotation is None |
| 1708 | + assert retval.description == "Description\non several lines." |
| 1709 | + |
| 1710 | + |
1493 | 1711 | def test_reading_property_type_in_summary(parse_google: ParserType) -> None:
|
1494 | 1712 | """Assert we can parse the return type of properties in their summary.
|
1495 | 1713 |
|
|
0 commit comments