Skip to content

Only print the index if it is necessary. #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions numpydoc/docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,20 @@ def _str_see_also(self, func_role):
def _str_index(self):
idx = self['index']
out = []
out += ['.. index:: %s' % idx.get('default', '')]
output_index = False
default_index = idx.get('default', '')
if default_index:
output_index = True
out += ['.. index:: %s' % default_index]
for section, references in idx.items():
if section == 'default':
continue
output_index = True
out += [' :%s: %s' % (section, ', '.join(references))]
return out
if output_index:
return out
else:
return ''

def __str__(self, func_role=''):
out = []
Expand Down
16 changes: 16 additions & 0 deletions numpydoc/tests/test_docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,22 @@ def test_yield_str():
.. index:: """)


def test_no_index_in_str():
assert "index" not in str(NumpyDocString("""Test idx

"""))

assert "index" in str(NumpyDocString("""Test idx

.. index :: random
"""))

assert "index" in str(NumpyDocString("""Test idx

.. index ::
foo
"""))

def test_sphinx_str():
sphinx_doc = SphinxDocString(doc_txt)
line_by_line_compare(str(sphinx_doc),
Expand Down