Skip to content

Commit b138ea7

Browse files
authored
BUG: Fix returns parsing no name (#429)
* TST: Add test case. * MAINT: Adjust logic for parameter lines with : char. Adjust logic in parameter line splitting to avoid bug where Returns objects containing sphinx roles are improperly parsed.
1 parent 4ef89d4 commit b138ea7

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

numpydoc/docscrape.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,14 @@ def _parse_param_list(self, content, single_element_is_type=False):
226226
params = []
227227
while not r.eof():
228228
header = r.read().strip()
229-
if " :" in header:
230-
arg_name, arg_type = header.split(" :", maxsplit=1)
231-
arg_name, arg_type = arg_name.strip(), arg_type.strip()
229+
if " : " in header:
230+
arg_name, arg_type = header.split(" : ", maxsplit=1)
232231
else:
232+
# NOTE: param line with single element should never have a
233+
# a " :" before the description line, so this should probably
234+
# warn.
235+
if header.endswith(" :"):
236+
header = header[:-2]
233237
if single_element_is_type:
234238
arg_name, arg_type = "", header
235239
else:

numpydoc/tests/test_docscrape.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,21 @@ def test_empty_first_line():
980980
)
981981

982982

983+
def test_returns_with_roles_no_names():
984+
"""Make sure colons that are part of sphinx roles are not misinterpreted
985+
as type separator in returns section. See gh-428."""
986+
docstring = NumpyDocString(
987+
"""
988+
Returns
989+
-------
990+
str or :class:`NumpyDocString`
991+
"""
992+
)
993+
expected = "str or :class:`NumpyDocString`" # not "str or : class:...
994+
assert docstring["Returns"][0].type == expected
995+
assert expected in str(docstring)
996+
997+
983998
def test_trailing_colon():
984999
assert doc8["Parameters"][0].name == "data"
9851000

0 commit comments

Comments
 (0)