diff options
author | Ross Barnowski <rossbar@berkeley.edu> | 2022-09-16 18:43:28 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-16 21:43:28 -0400 |
commit | b138ea7758532a8b6950c4404570ab367c3919f4 (patch) | |
tree | 94e105c95464d511f1cd1de28b3e0c5ca786a8c5 | |
parent | 4ef89d4da0d494a5b7f6ecf1bd613599e50336bf (diff) | |
download | numpydoc-b138ea7758532a8b6950c4404570ab367c3919f4.tar.gz |
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.
-rw-r--r-- | numpydoc/docscrape.py | 10 | ||||
-rw-r--r-- | numpydoc/tests/test_docscrape.py | 15 |
2 files changed, 22 insertions, 3 deletions
diff --git a/numpydoc/docscrape.py b/numpydoc/docscrape.py index 6bdaa84..9496f9d 100644 --- a/numpydoc/docscrape.py +++ b/numpydoc/docscrape.py @@ -226,10 +226,14 @@ class NumpyDocString(Mapping): params = [] while not r.eof(): header = r.read().strip() - if " :" in header: - arg_name, arg_type = header.split(" :", maxsplit=1) - arg_name, arg_type = arg_name.strip(), arg_type.strip() + if " : " in header: + arg_name, arg_type = header.split(" : ", maxsplit=1) else: + # NOTE: param line with single element should never have a + # a " :" before the description line, so this should probably + # warn. + if header.endswith(" :"): + header = header[:-2] if single_element_is_type: arg_name, arg_type = "", header else: diff --git a/numpydoc/tests/test_docscrape.py b/numpydoc/tests/test_docscrape.py index 55ccf92..049d2a2 100644 --- a/numpydoc/tests/test_docscrape.py +++ b/numpydoc/tests/test_docscrape.py @@ -980,6 +980,21 @@ doc8 = NumpyDocString( ) +def test_returns_with_roles_no_names(): + """Make sure colons that are part of sphinx roles are not misinterpreted + as type separator in returns section. See gh-428.""" + docstring = NumpyDocString( + """ + Returns + ------- + str or :class:`NumpyDocString` + """ + ) + expected = "str or :class:`NumpyDocString`" # not "str or : class:... + assert docstring["Returns"][0].type == expected + assert expected in str(docstring) + + def test_trailing_colon(): assert doc8["Parameters"][0].name == "data" |