Skip to content

Commit

Permalink
feat: Numpy parser: handle return section items with just type, or no…
Browse files Browse the repository at this point in the history
… name and no type

Issue #173: #173
PR #174: #174
Co-authored-by: Timothée Mazzucotelli <[email protected]>
  • Loading branch information
machow authored Jul 13, 2023
1 parent 88cd603 commit bdec37d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/griffe/docstrings/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ def _read_block(docstring: Docstring, *, offset: int) -> tuple[str, int]:
| # or
(?P<name>{_RE_NAME})\s*:\s* # just name
| # or
(?P<type>{_RE_TYPE})\s* # just type
\s*:\s*$ # no name, no type
| # or
(?::\s*)?(?P<type>{_RE_TYPE})\s* # just type
)
""",
re.IGNORECASE | re.VERBOSE,
Expand Down
9 changes: 7 additions & 2 deletions tests/test_docstrings/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,10 @@ def assert_element_equal(actual: DocstringElement, expected: DocstringElement) -
actual: The actual element.
expected: The expected element.
"""
assert actual.annotation == expected.annotation
assert actual.description == expected.description
assert isinstance(actual, type(expected))

for k in actual.as_dict():
src = getattr(actual, k)
dst = getattr(expected, k)

assert src == dst, f"attribute {k!r}, {src!r} != {dst!r}"
27 changes: 24 additions & 3 deletions tests/test_docstrings/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@ def test_returns_section(parse_numpy: ParserType) -> None:
flag : bool
Some kind
of flag.
x :
Name only
:
No name or annotation
: int
Only annotation
"""

sections, _ = parse_numpy(docstring)
Expand All @@ -377,7 +383,22 @@ def test_returns_section(parse_numpy: ParserType) -> None:
)
assert_element_equal(
sections[0].value[1],
DocstringReturn(name="", annotation="bool", description="Some kind\nof flag."),
DocstringReturn(name="flag", annotation="bool", description="Some kind\nof flag."),
)

assert_element_equal(
sections[0].value[2],
DocstringReturn(name="x", annotation=None, description="Name only"),
)

assert_element_equal(
sections[0].value[3],
DocstringReturn(name="", annotation=None, description="No name or annotation"),
)

assert_element_equal(
sections[0].value[4],
DocstringReturn(name="", annotation="int", description="Only annotation"),
)


Expand Down Expand Up @@ -405,7 +426,7 @@ def test_yields_section(parse_numpy: ParserType) -> None:
)
assert_element_equal(
sections[0].value[1],
DocstringYield(name="", annotation="bool", description="Some kind\nof flag."),
DocstringYield(name="flag", annotation="bool", description="Some kind\nof flag."),
)


Expand Down Expand Up @@ -433,7 +454,7 @@ def test_receives_section(parse_numpy: ParserType) -> None:
)
assert_element_equal(
sections[0].value[1],
DocstringReceive(name="", annotation="bool", description="Some kind\nof flag."),
DocstringReceive(name="flag", annotation="bool", description="Some kind\nof flag."),
)


Expand Down

0 comments on commit bdec37d

Please sign in to comment.