diff options
| author | Rob Ruana <rob@robruana.com> | 2015-02-09 01:28:57 -0500 |
|---|---|---|
| committer | Rob Ruana <rob@robruana.com> | 2015-02-09 01:28:57 -0500 |
| commit | c428f9353e8d1665fee7c0e820b16bb090f0be09 (patch) | |
| tree | bc6b8285486125547bbd6761d12884ed53445322 /tests/test_ext_napoleon_docstring.py | |
| parent | 4b396d423209dc8fde3393ff3cbab3167cdf57f2 (diff) | |
| download | sphinx-git-c428f9353e8d1665fee7c0e820b16bb090f0be09.tar.gz | |
Closes #1484: add tests to confirm namedtuple subclasses are handled correctly by napoleon extension
Diffstat (limited to 'tests/test_ext_napoleon_docstring.py')
| -rw-r--r-- | tests/test_ext_napoleon_docstring.py | 59 |
1 files changed, 53 insertions, 6 deletions
diff --git a/tests/test_ext_napoleon_docstring.py b/tests/test_ext_napoleon_docstring.py index 84e55840a..6e19b3279 100644 --- a/tests/test_ext_napoleon_docstring.py +++ b/tests/test_ext_napoleon_docstring.py @@ -10,7 +10,11 @@ :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 sphinx.ext.napoleon import Config @@ -18,10 +22,53 @@ from sphinx.ext.napoleon.docstring import GoogleDocstring, NumpyDocstring 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): 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): docstrings = [( """Single line summary""", @@ -180,8 +227,8 @@ class GoogleDocstringTest(BaseDocstringTest): def test_docstrings(self): config = Config(napoleon_use_param=False, napoleon_use_rtype=False) for docstring, expected in self.docstrings: - actual = str(GoogleDocstring(textwrap.dedent(docstring), config)) - expected = textwrap.dedent(expected) + actual = str(GoogleDocstring(dedent(docstring), config)) + expected = dedent(expected) self.assertEqual(expected, actual) def test_parameters_with_class_reference(self): @@ -382,8 +429,8 @@ class NumpyDocstringTest(BaseDocstringTest): def test_docstrings(self): config = Config(napoleon_use_param=False, napoleon_use_rtype=False) for docstring, expected in self.docstrings: - actual = str(NumpyDocstring(textwrap.dedent(docstring), config)) - expected = textwrap.dedent(expected) + actual = str(NumpyDocstring(dedent(docstring), config)) + expected = dedent(expected) self.assertEqual(expected, actual) def test_parameters_with_class_reference(self): @@ -425,7 +472,7 @@ param1 : MyClass instance self.assertEqual(expected, actual) config = Config(napoleon_use_param=True) - actual = str(NumpyDocstring(textwrap.dedent(docstring), config)) + actual = str(NumpyDocstring(dedent(docstring), config)) expected = """\ :param param1: :type param1: MyClass instance |
