Skip to content

Commit 5f2d997

Browse files
committed
fix: Fix parsing of choices in Numpy parameters
Issue #212: #212
1 parent a68c533 commit 5f2d997

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/griffe/docstrings/numpy.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ def _read_parameters(
252252
choices = match.group("choices")
253253
default = None
254254
if choices:
255-
choices = choices.split(", ", 1)
256-
default = choices[0]
255+
annotation = choices
256+
default = choices.split(", ", 1)[0]
257257
elif annotation:
258258
match = re.match(r"^(?P<annotation>.+),\s+default(?: |: |=)(?P<default>.+)$", annotation)
259259
if match:

tests/test_docstrings/test_numpy.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ def test_ignore_init_summary(parse_numpy: ParserType, docstring: str) -> None:
10901090
],
10911091
)
10921092
def test_trim_doctest_flags_basic_example(parse_numpy: ParserType, docstring: str) -> None:
1093-
"""Correctly parse_numpy simple example docstrings when `trim_doctest_flags` option is turned on.
1093+
"""Correctly parse simple example docstrings when `trim_doctest_flags` option is turned on.
10941094
10951095
Parameters:
10961096
parse_numpy: Fixture parser.
@@ -1108,7 +1108,7 @@ def test_trim_doctest_flags_basic_example(parse_numpy: ParserType, docstring: st
11081108

11091109

11101110
def test_trim_doctest_flags_multi_example(parse_numpy: ParserType) -> None:
1111-
"""Correctly parse_numpy multiline example docstrings when `trim_doctest_flags` option is turned on.
1111+
"""Correctly parse multiline example docstrings when `trim_doctest_flags` option is turned on.
11121112
11131113
Parameters:
11141114
parse_numpy: Fixture parser.
@@ -1142,3 +1142,20 @@ def test_trim_doctest_flags_multi_example(parse_numpy: ParserType) -> None:
11421142
example_str = sections[0].value[3][1]
11431143
assert "<BLANKLINE>" not in example_str
11441144
assert "\n>>> print(list(range(1, 100)))\n" in example_str
1145+
1146+
1147+
def test_parsing_choices(parse_numpy: ParserType) -> None:
1148+
"""Correctly parse choices.
1149+
1150+
Parameters:
1151+
parse_numpy: Fixture parser.
1152+
"""
1153+
docstring = r"""
1154+
Parameters
1155+
--------
1156+
order : {'C', 'F', 'A'}
1157+
Description of `order`.
1158+
"""
1159+
sections, warnings = parse_numpy(docstring, trim_doctest_flags=True)
1160+
assert sections[0].value[0].annotation == "'C', 'F', 'A'"
1161+
assert not warnings

0 commit comments

Comments
 (0)