Skip to content

Commit 7ca8e3c

Browse files
hmaarrfkjnothman
authored andcommitted
ENH Only print the index if it is necessary. (#187)
1 parent 2cdd4d9 commit 7ca8e3c

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

numpydoc/docscrape.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,20 @@ def _str_see_also(self, func_role):
464464
def _str_index(self):
465465
idx = self['index']
466466
out = []
467-
out += ['.. index:: %s' % idx.get('default', '')]
467+
output_index = False
468+
default_index = idx.get('default', '')
469+
if default_index:
470+
output_index = True
471+
out += ['.. index:: %s' % default_index]
468472
for section, references in idx.items():
469473
if section == 'default':
470474
continue
475+
output_index = True
471476
out += [' :%s: %s' % (section, ', '.join(references))]
472-
return out
477+
if output_index:
478+
return out
479+
else:
480+
return ''
473481

474482
def __str__(self, func_role=''):
475483
out = []

numpydoc/tests/test_docscrape.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,22 @@ def test_yield_str():
468468
.. index:: """)
469469

470470

471+
def test_no_index_in_str():
472+
assert "index" not in str(NumpyDocString("""Test idx
473+
474+
"""))
475+
476+
assert "index" in str(NumpyDocString("""Test idx
477+
478+
.. index :: random
479+
"""))
480+
481+
assert "index" in str(NumpyDocString("""Test idx
482+
483+
.. index ::
484+
foo
485+
"""))
486+
471487
def test_sphinx_str():
472488
sphinx_doc = SphinxDocString(doc_txt)
473489
line_by_line_compare(str(sphinx_doc),

0 commit comments

Comments
 (0)