Skip to content

Commit

Permalink
fix: Fix parsing of choices in Numpy parameters
Browse files Browse the repository at this point in the history
Issue #212: #212
  • Loading branch information
pawamoy committed Sep 28, 2023
1 parent a68c533 commit 5f2d997
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/griffe/docstrings/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ def _read_parameters(
choices = match.group("choices")
default = None
if choices:
choices = choices.split(", ", 1)
default = choices[0]
annotation = choices
default = choices.split(", ", 1)[0]
elif annotation:
match = re.match(r"^(?P<annotation>.+),\s+default(?: |: |=)(?P<default>.+)$", annotation)
if match:
Expand Down
21 changes: 19 additions & 2 deletions tests/test_docstrings/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ def test_ignore_init_summary(parse_numpy: ParserType, docstring: str) -> None:
],
)
def test_trim_doctest_flags_basic_example(parse_numpy: ParserType, docstring: str) -> None:
"""Correctly parse_numpy simple example docstrings when `trim_doctest_flags` option is turned on.
"""Correctly parse simple example docstrings when `trim_doctest_flags` option is turned on.
Parameters:
parse_numpy: Fixture parser.
Expand All @@ -1108,7 +1108,7 @@ def test_trim_doctest_flags_basic_example(parse_numpy: ParserType, docstring: st


def test_trim_doctest_flags_multi_example(parse_numpy: ParserType) -> None:
"""Correctly parse_numpy multiline example docstrings when `trim_doctest_flags` option is turned on.
"""Correctly parse multiline example docstrings when `trim_doctest_flags` option is turned on.
Parameters:
parse_numpy: Fixture parser.
Expand Down Expand Up @@ -1142,3 +1142,20 @@ def test_trim_doctest_flags_multi_example(parse_numpy: ParserType) -> None:
example_str = sections[0].value[3][1]
assert "<BLANKLINE>" not in example_str
assert "\n>>> print(list(range(1, 100)))\n" in example_str


def test_parsing_choices(parse_numpy: ParserType) -> None:
"""Correctly parse choices.
Parameters:
parse_numpy: Fixture parser.
"""
docstring = r"""
Parameters
--------
order : {'C', 'F', 'A'}
Description of `order`.
"""
sections, warnings = parse_numpy(docstring, trim_doctest_flags=True)
assert sections[0].value[0].annotation == "'C', 'F', 'A'"
assert not warnings

0 comments on commit 5f2d997

Please sign in to comment.