Skip to content

Commit bdec37d

Browse files
machowpawamoy
andauthored
feat: Numpy parser: handle return section items with just type, or no name and no type
Issue #173: #173 PR #174: #174 Co-authored-by: Timothée Mazzucotelli <pawamoy@pm.me>
1 parent 88cd603 commit bdec37d

3 files changed

Lines changed: 34 additions & 6 deletions

File tree

src/griffe/docstrings/numpy.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ def _read_block(docstring: Docstring, *, offset: int) -> tuple[str, int]:
211211
| # or
212212
(?P<name>{_RE_NAME})\s*:\s* # just name
213213
| # or
214-
(?P<type>{_RE_TYPE})\s* # just type
214+
\s*:\s*$ # no name, no type
215+
| # or
216+
(?::\s*)?(?P<type>{_RE_TYPE})\s* # just type
215217
)
216218
""",
217219
re.IGNORECASE | re.VERBOSE,

tests/test_docstrings/helpers.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,10 @@ def assert_element_equal(actual: DocstringElement, expected: DocstringElement) -
9292
actual: The actual element.
9393
expected: The expected element.
9494
"""
95-
assert actual.annotation == expected.annotation
96-
assert actual.description == expected.description
95+
assert isinstance(actual, type(expected))
96+
97+
for k in actual.as_dict():
98+
src = getattr(actual, k)
99+
dst = getattr(expected, k)
100+
101+
assert src == dst, f"attribute {k!r}, {src!r} != {dst!r}"

tests/test_docstrings/test_numpy.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,12 @@ def test_returns_section(parse_numpy: ParserType) -> None:
367367
flag : bool
368368
Some kind
369369
of flag.
370+
x :
371+
Name only
372+
:
373+
No name or annotation
374+
: int
375+
Only annotation
370376
"""
371377

372378
sections, _ = parse_numpy(docstring)
@@ -377,7 +383,22 @@ def test_returns_section(parse_numpy: ParserType) -> None:
377383
)
378384
assert_element_equal(
379385
sections[0].value[1],
380-
DocstringReturn(name="", annotation="bool", description="Some kind\nof flag."),
386+
DocstringReturn(name="flag", annotation="bool", description="Some kind\nof flag."),
387+
)
388+
389+
assert_element_equal(
390+
sections[0].value[2],
391+
DocstringReturn(name="x", annotation=None, description="Name only"),
392+
)
393+
394+
assert_element_equal(
395+
sections[0].value[3],
396+
DocstringReturn(name="", annotation=None, description="No name or annotation"),
397+
)
398+
399+
assert_element_equal(
400+
sections[0].value[4],
401+
DocstringReturn(name="", annotation="int", description="Only annotation"),
381402
)
382403

383404

@@ -405,7 +426,7 @@ def test_yields_section(parse_numpy: ParserType) -> None:
405426
)
406427
assert_element_equal(
407428
sections[0].value[1],
408-
DocstringYield(name="", annotation="bool", description="Some kind\nof flag."),
429+
DocstringYield(name="flag", annotation="bool", description="Some kind\nof flag."),
409430
)
410431

411432

@@ -433,7 +454,7 @@ def test_receives_section(parse_numpy: ParserType) -> None:
433454
)
434455
assert_element_equal(
435456
sections[0].value[1],
436-
DocstringReceive(name="", annotation="bool", description="Some kind\nof flag."),
457+
DocstringReceive(name="flag", annotation="bool", description="Some kind\nof flag."),
437458
)
438459

439460

0 commit comments

Comments
 (0)