Closes #1484: add tests to confirm namedtuple subclasses are handled correctly by napoleon extension

This commit is contained in:
Rob Ruana 2015-02-09 01:28:57 -05:00
parent 4b396d4232
commit c428f9353e
2 changed files with 55 additions and 6 deletions

View File

@ -776,6 +776,8 @@ class NumpyDocstring(GoogleDocstring):
""" """
Derived from the NumpyDoc implementation of _parse_see_also. Derived from the NumpyDoc implementation of _parse_see_also.
See Also
--------
func_name : Descriptive text func_name : Descriptive text
continued text continued text
another_func_name : Descriptive text another_func_name : Descriptive text

View File

@ -10,7 +10,11 @@
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
import textwrap from collections import namedtuple
# inspect.cleandoc() implements the trim() function from PEP 257
from inspect import cleandoc
from textwrap import dedent
from unittest import TestCase from unittest import TestCase
from sphinx.ext.napoleon import Config from sphinx.ext.napoleon import Config
@ -18,10 +22,53 @@ from sphinx.ext.napoleon.docstring import GoogleDocstring, NumpyDocstring
from util import mock from util import mock
class NamedtupleSubclass(namedtuple('NamedtupleSubclass', ('attr1', 'attr2'))):
"""Sample namedtuple subclass
Attributes
----------
attr1 : Arbitrary type
Quick description of attr1
attr2 : Another arbitrary type
Quick description of attr2
"""
# To avoid creating a dict, as a namedtuple doesn't have it:
__slots__ = ()
def __new__(cls, attr1, attr2=None):
return super(NamedtupleSubclass, cls).__new__(cls, attr1, attr2)
class BaseDocstringTest(TestCase): class BaseDocstringTest(TestCase):
pass pass
class NamedtupleSubclassTest(BaseDocstringTest):
def test_attributes_docstring(self):
config = Config()
actual = str(NumpyDocstring(cleandoc(NamedtupleSubclass.__doc__),
config=config, app=None, what='class',
name='NamedtupleSubclass', obj=NamedtupleSubclass))
expected = dedent("""\
Sample namedtuple subclass
.. attribute:: attr1
*Arbitrary type*
Quick description of attr1
.. attribute:: attr2
*Another arbitrary type*
Quick description of attr2
""")
self.assertEqual(expected, actual)
class GoogleDocstringTest(BaseDocstringTest): class GoogleDocstringTest(BaseDocstringTest):
docstrings = [( docstrings = [(
"""Single line summary""", """Single line summary""",
@ -180,8 +227,8 @@ class GoogleDocstringTest(BaseDocstringTest):
def test_docstrings(self): def test_docstrings(self):
config = Config(napoleon_use_param=False, napoleon_use_rtype=False) config = Config(napoleon_use_param=False, napoleon_use_rtype=False)
for docstring, expected in self.docstrings: for docstring, expected in self.docstrings:
actual = str(GoogleDocstring(textwrap.dedent(docstring), config)) actual = str(GoogleDocstring(dedent(docstring), config))
expected = textwrap.dedent(expected) expected = dedent(expected)
self.assertEqual(expected, actual) self.assertEqual(expected, actual)
def test_parameters_with_class_reference(self): def test_parameters_with_class_reference(self):
@ -382,8 +429,8 @@ class NumpyDocstringTest(BaseDocstringTest):
def test_docstrings(self): def test_docstrings(self):
config = Config(napoleon_use_param=False, napoleon_use_rtype=False) config = Config(napoleon_use_param=False, napoleon_use_rtype=False)
for docstring, expected in self.docstrings: for docstring, expected in self.docstrings:
actual = str(NumpyDocstring(textwrap.dedent(docstring), config)) actual = str(NumpyDocstring(dedent(docstring), config))
expected = textwrap.dedent(expected) expected = dedent(expected)
self.assertEqual(expected, actual) self.assertEqual(expected, actual)
def test_parameters_with_class_reference(self): def test_parameters_with_class_reference(self):
@ -425,7 +472,7 @@ param1 : MyClass instance
self.assertEqual(expected, actual) self.assertEqual(expected, actual)
config = Config(napoleon_use_param=True) config = Config(napoleon_use_param=True)
actual = str(NumpyDocstring(textwrap.dedent(docstring), config)) actual = str(NumpyDocstring(dedent(docstring), config))
expected = """\ expected = """\
:param param1: :param param1:
:type param1: MyClass instance :type param1: MyClass instance