Skip to content

Commit 67c2edc

Browse files
committed
Fix param parsing.
Closes #285 This fixes two tings: - When first sentence of the docstring is onteh first line, Parameters is not properly parse, which for example mis parsed numpy.array docstring. - many project have paremeters description list with ` :` afer the name, even if no type is present. If there is no space after the `:` the parameter name includes the ` :` which is most likely wrong.
1 parent 4e86e81 commit 67c2edc

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

numpydoc/docscrape.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,14 @@ def _read_sections(self):
219219
yield name, self._strip(data[2:])
220220

221221
def _parse_param_list(self, content, single_element_is_type=False):
222+
content = dedent_lines(content)
222223
r = Reader(content)
223224
params = []
224225
while not r.eof():
225226
header = r.read().strip()
226-
if ' : ' in header:
227-
arg_name, arg_type = header.split(' : ', maxsplit=1)
227+
if ' :' in header:
228+
arg_name, arg_type = header.split(' :', maxsplit=1)
229+
arg_name, arg_type = arg_name.strip(), arg_type.strip()
228230
else:
229231
if single_element_is_type:
230232
arg_name, arg_type = '', header

numpydoc/tests/test_docscrape.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,12 @@ def test_extended_summary():
183183
assert doc['Extended Summary'][0].startswith('The multivariate normal')
184184

185185

186-
def test_parameters():
186+
@pytest.mark.parametrize('sig_on_first_line', (True, False))
187+
def test_parameters(sig_on_first_line):
188+
if sig_on_first_line:
189+
doc = NumpyDocString(doc_txt.lstrip())
190+
else:
191+
doc = NumpyDocString(doc_txt)
187192
assert len(doc['Parameters']) == 4
188193
names = [n for n, _, _ in doc['Parameters']]
189194
assert all(a == b for a, b in zip(names, ['mean', 'cov', 'shape']))
@@ -921,6 +926,20 @@ class BadSection:
921926
def test_empty_first_line():
922927
assert doc7['Summary'][0].startswith('Doc starts')
923928

929+
doc8 = NumpyDocString("""
930+
931+
Parameters wit colon and no types:
932+
933+
Parameters
934+
----------
935+
936+
data :
937+
some stuff, technically invalid
938+
""")
939+
940+
941+
def test_trailing_colon():
942+
assert doc8['Parameters'][0].name == 'data'
924943

925944
def test_no_summary():
926945
str(SphinxDocString("""

0 commit comments

Comments
 (0)