summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Bussonnier <bussonniermatthias@gmail.com>2020-08-10 10:03:12 -0700
committerGitHub <noreply@github.com>2020-08-10 13:03:12 -0400
commit676a8d4c1627e92052c2f3a5e2bde558e9a59f96 (patch)
tree17a7714773b1505eb35543f6b7afaef9a2da667f
parent7bee33a6841666c8410366053213ce61e8652bd2 (diff)
downloadnumpydoc-676a8d4c1627e92052c2f3a5e2bde558e9a59f96.tar.gz
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 <larson.eric.d@gmail.com> * Update numpydoc/tests/test_docscrape.py Co-authored-by: Eric Larson <larson.eric.d@gmail.com> Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
-rw-r--r--numpydoc/docscrape.py6
-rw-r--r--numpydoc/tests/test_docscrape.py44
2 files changed, 36 insertions, 14 deletions
diff --git a/numpydoc/docscrape.py b/numpydoc/docscrape.py
index d79992c..2b992d8 100644
--- a/numpydoc/docscrape.py
+++ b/numpydoc/docscrape.py
@@ -219,12 +219,14 @@ class NumpyDocString(Mapping):
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
diff --git a/numpydoc/tests/test_docscrape.py b/numpydoc/tests/test_docscrape.py
index 2ab0218..a0b6d2e 100644
--- a/numpydoc/tests/test_docscrape.py
+++ b/numpydoc/tests/test_docscrape.py
@@ -133,7 +133,11 @@ doc_txt = '''\
:refguide: random;distributions, random;gauss
'''
-doc = NumpyDocString(doc_txt)
+
+@pytest.fixture(params=['','\n '], ids=["flush", "newline_indented"])
+def doc(request):
+ return NumpyDocString(request.param+doc_txt)
+
doc_yields_txt = """
Test generator
@@ -169,21 +173,21 @@ c : int
doc_sent = NumpyDocString(doc_sent_txt)
-def test_signature():
+def test_signature(doc):
assert doc['Signature'].startswith('numpy.multivariate_normal(')
assert doc['Signature'].endswith('spam=None)')
-def test_summary():
+def test_summary(doc):
assert doc['Summary'][0].startswith('Draw values')
assert doc['Summary'][-1].endswith('covariance.')
-def test_extended_summary():
+def test_extended_summary(doc):
assert doc['Extended Summary'][0].startswith('The multivariate normal')
-def test_parameters():
+def test_parameters(doc):
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']))
@@ -205,7 +209,7 @@ def test_parameters():
assert desc[0].startswith('The type and size')
-def test_other_parameters():
+def test_other_parameters(doc):
assert len(doc['Other Parameters']) == 1
assert [n for n, _, _ in doc['Other Parameters']] == ['spam']
arg, arg_type, desc = doc['Other Parameters'][0]
@@ -213,7 +217,8 @@ def test_other_parameters():
assert desc[0].startswith('A parrot off its mortal coil')
-def test_returns():
+
+def test_returns(doc):
assert len(doc['Returns']) == 3
arg, arg_type, desc = doc['Returns'][0]
assert arg == 'out'
@@ -342,23 +347,23 @@ That should break...
or 'function dummy_func' in str(e))
-def test_notes():
+def test_notes(doc):
assert doc['Notes'][0].startswith('Instead')
assert doc['Notes'][-1].endswith('definite.')
assert len(doc['Notes']) == 17
-def test_references():
+def test_references(doc):
assert doc['References'][0].startswith('..')
assert doc['References'][-1].endswith('2001.')
-def test_examples():
+def test_examples(doc):
assert doc['Examples'][0].startswith('>>>')
assert doc['Examples'][-1].endswith('True]')
-def test_index():
+def test_index(doc):
assert doc['index']['default'] == 'random'
assert len(doc['index']) == 2
assert len(doc['index']['refguide']) == 2
@@ -382,7 +387,7 @@ def line_by_line_compare(a, b, n_lines=None):
assert aa == bb
-def test_str():
+def test_str(doc):
# doc_txt has the order of Notes and See Also sections flipped.
# This should be handled automatically, and so, one thing this test does
# is to make sure that See Also precedes Notes in the output.
@@ -921,6 +926,21 @@ doc7 = NumpyDocString("""
def test_empty_first_line():
assert doc7['Summary'][0].startswith('Doc starts')
+doc8 = NumpyDocString("""
+
+ Parameters with 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("""