Skip to content

Commit

Permalink
Fix param parsing.
Browse files Browse the repository at this point in the history
Closes numpy#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.
  • Loading branch information
Carreau committed Jul 18, 2020
1 parent b65bff5 commit a838455
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 4 additions & 2 deletions numpydoc/docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,14 @@ def _read_sections(self):
yield name, self._strip(data[2:])

def _parse_param_list(self, content, single_element_is_type=False):
content = dedent_lines(content)
r = Reader(content)
params = []
while not r.eof():
header = r.read().strip()
if ' : ' in header:
arg_name, arg_type = header.split(' : ', maxsplit=1)
if ' :' in header:
arg_name, arg_type = header.split(' :', maxsplit=1)
arg_name, arg_type = arg_name.strip(), arg_type.strip()
else:
if single_element_is_type:
arg_name, arg_type = '', header
Expand Down
21 changes: 20 additions & 1 deletion numpydoc/tests/test_docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,12 @@ def test_extended_summary():
assert doc['Extended Summary'][0].startswith('The multivariate normal')


def test_parameters():
@pytest.mark.parametrize('sig_on_first_line', (True, False))
def test_parameters(sig_on_first_line):
if sig_on_first_line:
doc = NumpyDocString(doc_txt.lstrip())
else:
doc = NumpyDocString(doc_txt)
assert len(doc['Parameters']) == 4
names = [n for n, _, _ in doc['Parameters']]
assert all(a == b for a, b in zip(names, ['mean', 'cov', 'shape']))
Expand Down Expand Up @@ -921,6 +926,20 @@ class BadSection:
def test_empty_first_line():
assert doc7['Summary'][0].startswith('Doc starts')

doc8 = NumpyDocString("""
Parameters wit colon and no types:
Parameters
----------
data :
some stuff, technically invalid
""")


def test_trailing_colon():
assert doc8['Parameters'][0].name == 'data'

def test_no_summary():
str(SphinxDocString("""
Expand Down

0 comments on commit a838455

Please sign in to comment.