Fixes #5426: [Napoleon] Better handling of inline attributes (#5470)

* Fixes #5426: [Napoleon] Better handling of inline attributes

* Removes uneeded NOQA comments

* Fixes imports
This commit is contained in:
Rob Ruana
2018-09-23 09:26:17 -04:00
committed by GitHub
parent bd1e33643c
commit e547a6a0d2
4 changed files with 38 additions and 8 deletions

View File

@@ -409,10 +409,10 @@ sure that "sphinx.ext.napoleon" is enabled in `conf.py`::
.. attribute:: attr1
*int*
Description of `attr1`
:type: int
.. confval:: napoleon_use_param
True to use a ``:param:`` role for each function parameter. False to

View File

@@ -9,7 +9,7 @@
:license: BSD, see LICENSE for details.
"""
import sphinx
from sphinx import __display_version__ as __version__
from sphinx.application import Sphinx
from sphinx.ext.napoleon.docstring import GoogleDocstring, NumpyDocstring
@@ -172,10 +172,10 @@ class Config:
.. attribute:: attr1
*int*
Description of `attr1`
:type: int
napoleon_use_param : :obj:`bool` (Defaults to True)
True to use a ``:param:`` role for each function parameter. False to
use a single ``:parameters:`` role for all the parameters.
@@ -300,7 +300,8 @@ def setup(app):
"""
if not isinstance(app, Sphinx):
return # probably called by tests
# probably called by tests
return {'version': __version__, 'parallel_read_safe': True}
_patch_python_domain()
@@ -310,7 +311,7 @@ def setup(app):
for name, (default, rebuild) in Config._config_values.items():
app.add_config_value(name, default, rebuild)
return {'version': sphinx.__display_version__, 'parallel_read_safe': True}
return {'version': __version__, 'parallel_read_safe': True}
def _patch_python_domain():

View File

@@ -268,8 +268,9 @@ class GoogleDocstring(UnicodeMixin):
# type: () -> Tuple[unicode, List[unicode]]
line = next(self._line_iter)
_type, colon, _desc = self._partition_field_on_colon(line)
if not colon:
if not colon or not _desc:
_type, _desc = _desc, _type
_desc += colon
_descs = [_desc] + self._dedent(self._consume_to_end())
_descs = self.__class__(_descs, self._config).lines()
return _type, _descs

View File

@@ -77,6 +77,34 @@ Sample namedtuple subclass
self.assertEqual(expected, actual)
class InlineAttributeTest(BaseDocstringTest):
def test_class_data_member(self):
config = Config()
docstring = """data member description:
- a: b
"""
actual = str(GoogleDocstring(docstring, config=config, app=None,
what='attribute', name='some_data', obj=0))
expected = """data member description:
- a: b"""
self.assertEqual(expected, actual)
def test_class_data_member_inline(self):
config = Config()
docstring = """b: data member description with :ref:`reference`"""
actual = str(GoogleDocstring(docstring, config=config, app=None,
what='attribute', name='some_data', obj=0))
expected = """data member description with :ref:`reference`
:type: b"""
self.assertEqual(expected, actual)
class GoogleDocstringTest(BaseDocstringTest):
docstrings = [(
"""Single line summary""",