|
8 | 8 | import pytest |
9 | 9 |
|
10 | 10 | from griffe.dataclasses import Attribute, Class, Docstring, Function, Module, Parameter, Parameters |
11 | | -from griffe.docstrings.dataclasses import DocstringSectionKind |
| 11 | +from griffe.docstrings.dataclasses import DocstringReturn, DocstringSectionKind |
12 | 12 | from griffe.docstrings.utils import parse_annotation |
13 | 13 | from griffe.expressions import ExprName |
14 | 14 |
|
@@ -1359,3 +1359,74 @@ def test_single_line_with_trailing_whitespace(parse_google: ParserType) -> None: |
1359 | 1359 | assert len(sections) == 1 |
1360 | 1360 | assert sections[0].kind is DocstringSectionKind.text |
1361 | 1361 | assert not warnings |
| 1362 | + |
| 1363 | + |
| 1364 | +@pytest.mark.parametrize( |
| 1365 | + ("returns_multiple_items", "return_annotation", "expected"), |
| 1366 | + [ |
| 1367 | + ( |
| 1368 | + False, |
| 1369 | + None, |
| 1370 | + [DocstringReturn("", description="XXXXXXX\n YYYYYYY\nZZZZZZZ", annotation=None)], |
| 1371 | + ), |
| 1372 | + ( |
| 1373 | + False, |
| 1374 | + "tuple[int, int]", |
| 1375 | + [DocstringReturn("", description="XXXXXXX\n YYYYYYY\nZZZZZZZ", annotation="tuple[int, int]")], |
| 1376 | + ), |
| 1377 | + ( |
| 1378 | + True, |
| 1379 | + None, |
| 1380 | + [ |
| 1381 | + DocstringReturn("", description="XXXXXXX\nYYYYYYY", annotation=None), |
| 1382 | + DocstringReturn("", description="ZZZZZZZ", annotation=None), |
| 1383 | + ], |
| 1384 | + ), |
| 1385 | + ( |
| 1386 | + True, |
| 1387 | + "tuple[int,int]", |
| 1388 | + [ |
| 1389 | + DocstringReturn("", description="XXXXXXX\nYYYYYYY", annotation="int"), |
| 1390 | + DocstringReturn("", description="ZZZZZZZ", annotation="int"), |
| 1391 | + ], |
| 1392 | + ), |
| 1393 | + ], |
| 1394 | +) |
| 1395 | +def test_parse_returns_multiple_items( |
| 1396 | + parse_google: ParserType, |
| 1397 | + returns_multiple_items: bool, |
| 1398 | + return_annotation: str, |
| 1399 | + expected: list[DocstringReturn], |
| 1400 | +) -> None: |
| 1401 | + """Parse Returns section with and without multiple items. |
| 1402 | +
|
| 1403 | + Parameters: |
| 1404 | + parse_google: Fixture parser. |
| 1405 | + returns_multiple_items: Whether the `Returns` section has multiple items. |
| 1406 | + return_annotation: The return annotation of the function to parse. |
| 1407 | + expected: The expected value of the parsed Returns section. |
| 1408 | + """ |
| 1409 | + parent = ( |
| 1410 | + Function("func", returns=parse_annotation(return_annotation, Docstring("d", parent=Function("f")))) |
| 1411 | + if return_annotation is not None |
| 1412 | + else None |
| 1413 | + ) |
| 1414 | + docstring = """ |
| 1415 | + Returns: |
| 1416 | + XXXXXXX |
| 1417 | + YYYYYYY |
| 1418 | + ZZZZZZZ |
| 1419 | + """ |
| 1420 | + sections, _ = parse_google( |
| 1421 | + docstring, |
| 1422 | + returns_multiple_items=returns_multiple_items, |
| 1423 | + parent=parent, |
| 1424 | + ) |
| 1425 | + |
| 1426 | + assert len(sections) == 1 |
| 1427 | + assert len(sections[0].value) == len(expected) |
| 1428 | + |
| 1429 | + for annotated, expected_ in zip(sections[0].value, expected): |
| 1430 | + assert annotated.name == expected_.name |
| 1431 | + assert str(annotated.annotation) == str(expected_.annotation) |
| 1432 | + assert annotated.description == expected_.description |
0 commit comments