Skip to content

Commit 676a8d4

Browse files
Carreaularsoner
andauthored
Fix param parsing. (#286)
* 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. * test fixture * make doc a fixture * Update numpydoc/tests/test_docscrape.py Co-authored-by: Eric Larson <[email protected]> * Update numpydoc/tests/test_docscrape.py Co-authored-by: Eric Larson <[email protected]> Co-authored-by: Eric Larson <[email protected]>
1 parent 7bee33a commit 676a8d4

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
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: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@
133133
:refguide: random;distributions, random;gauss
134134
135135
'''
136-
doc = NumpyDocString(doc_txt)
136+
137+
@pytest.fixture(params=['','\n '], ids=["flush", "newline_indented"])
138+
def doc(request):
139+
return NumpyDocString(request.param+doc_txt)
140+
137141

138142
doc_yields_txt = """
139143
Test generator
@@ -169,21 +173,21 @@
169173
doc_sent = NumpyDocString(doc_sent_txt)
170174

171175

172-
def test_signature():
176+
def test_signature(doc):
173177
assert doc['Signature'].startswith('numpy.multivariate_normal(')
174178
assert doc['Signature'].endswith('spam=None)')
175179

176180

177-
def test_summary():
181+
def test_summary(doc):
178182
assert doc['Summary'][0].startswith('Draw values')
179183
assert doc['Summary'][-1].endswith('covariance.')
180184

181185

182-
def test_extended_summary():
186+
def test_extended_summary(doc):
183187
assert doc['Extended Summary'][0].startswith('The multivariate normal')
184188

185189

186-
def test_parameters():
190+
def test_parameters(doc):
187191
assert len(doc['Parameters']) == 4
188192
names = [n for n, _, _ in doc['Parameters']]
189193
assert all(a == b for a, b in zip(names, ['mean', 'cov', 'shape']))
@@ -205,15 +209,16 @@ def test_parameters():
205209
assert desc[0].startswith('The type and size')
206210

207211

208-
def test_other_parameters():
212+
def test_other_parameters(doc):
209213
assert len(doc['Other Parameters']) == 1
210214
assert [n for n, _, _ in doc['Other Parameters']] == ['spam']
211215
arg, arg_type, desc = doc['Other Parameters'][0]
212216
assert arg_type == 'parrot'
213217
assert desc[0].startswith('A parrot off its mortal coil')
214218

215219

216-
def test_returns():
220+
221+
def test_returns(doc):
217222
assert len(doc['Returns']) == 3
218223
arg, arg_type, desc = doc['Returns'][0]
219224
assert arg == 'out'
@@ -342,23 +347,23 @@ def dummy_func(arg):
342347
or 'function dummy_func' in str(e))
343348

344349

345-
def test_notes():
350+
def test_notes(doc):
346351
assert doc['Notes'][0].startswith('Instead')
347352
assert doc['Notes'][-1].endswith('definite.')
348353
assert len(doc['Notes']) == 17
349354

350355

351-
def test_references():
356+
def test_references(doc):
352357
assert doc['References'][0].startswith('..')
353358
assert doc['References'][-1].endswith('2001.')
354359

355360

356-
def test_examples():
361+
def test_examples(doc):
357362
assert doc['Examples'][0].startswith('>>>')
358363
assert doc['Examples'][-1].endswith('True]')
359364

360365

361-
def test_index():
366+
def test_index(doc):
362367
assert doc['index']['default'] == 'random'
363368
assert len(doc['index']) == 2
364369
assert len(doc['index']['refguide']) == 2
@@ -382,7 +387,7 @@ def line_by_line_compare(a, b, n_lines=None):
382387
assert aa == bb
383388

384389

385-
def test_str():
390+
def test_str(doc):
386391
# doc_txt has the order of Notes and See Also sections flipped.
387392
# This should be handled automatically, and so, one thing this test does
388393
# is to make sure that See Also precedes Notes in the output.
@@ -921,6 +926,21 @@ class BadSection:
921926
def test_empty_first_line():
922927
assert doc7['Summary'][0].startswith('Doc starts')
923928

929+
doc8 = NumpyDocString("""
930+
931+
Parameters with 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'
943+
924944

925945
def test_no_summary():
926946
str(SphinxDocString("""

0 commit comments

Comments
 (0)