From dd5f2b4235eb214fd4bf9663570a9516658c9a93 Mon Sep 17 00:00:00 2001 From: Mark Harfouche Date: Sat, 11 Aug 2018 23:41:33 -0400 Subject: [PATCH 1/2] Only print the index if it is necessary. --- numpydoc/docscrape.py | 12 ++++++++++-- numpydoc/tests/test_docscrape.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/numpydoc/docscrape.py b/numpydoc/docscrape.py index c5d9b0ec..36ae55d9 100644 --- a/numpydoc/docscrape.py +++ b/numpydoc/docscrape.py @@ -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 len(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 = [] diff --git a/numpydoc/tests/test_docscrape.py b/numpydoc/tests/test_docscrape.py index 902c3f1d..d5882080 100644 --- a/numpydoc/tests/test_docscrape.py +++ b/numpydoc/tests/test_docscrape.py @@ -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), From d4483f7b2094e5f01678b02a4607a196e6b32cde Mon Sep 17 00:00:00 2001 From: Mark Harfouche Date: Sat, 11 Aug 2018 23:49:43 -0400 Subject: [PATCH 2/2] don't use len to check for 0 length strings. --- numpydoc/docscrape.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpydoc/docscrape.py b/numpydoc/docscrape.py index 36ae55d9..9448829a 100644 --- a/numpydoc/docscrape.py +++ b/numpydoc/docscrape.py @@ -461,7 +461,7 @@ def _str_index(self): out = [] output_index = False default_index = idx.get('default', '') - if len(default_index): + if default_index: output_index = True out += ['.. index:: %s' % default_index] for section, references in idx.items():