Skip to content

Commit

Permalink
Merge pull request #8050 from keewis/preprocess-other-sections
Browse files Browse the repository at this point in the history
Preprocess other sections
  • Loading branch information
tk0miya committed Oct 29, 2020
2 parents 751b7a0 + 439f75a commit 68c89f4
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
9 changes: 7 additions & 2 deletions sphinx/ext/napoleon/docstring.py
Expand Up @@ -699,6 +699,9 @@ def _parse_raises_section(self, section: str) -> List[str]:
m = self._name_rgx.match(_type)
if m and m.group('name'):
_type = m.group('name')
elif _xref_regex.match(_type):
pos = _type.find('`')
_type = _type[pos + 1:-1]
_type = ' ' + _type if _type else ''
_desc = self._strip_empty(_desc)
_descs = ' ' + '\n '.join(_desc) if any(_desc) else ''
Expand Down Expand Up @@ -1104,15 +1107,17 @@ def _consume_field(self, parse_type: bool = True, prefer_type: bool = False
_name, _type = line, ''
_name, _type = _name.strip(), _type.strip()
_name = self._escape_args_and_kwargs(_name)

if prefer_type and not _type:
_type, _name = _name, _type

if self._config.napoleon_preprocess_types:
_type = _convert_numpy_type_spec(
_type,
location=self._get_location(),
translations=self._config.napoleon_type_aliases or {},
)

if prefer_type and not _type:
_type, _name = _name, _type
indent = self._get_indent(line) + 1
_desc = self._dedent(self._consume_indented_block(indent))
_desc = self.__class__(_desc, self._config).lines()
Expand Down
88 changes: 83 additions & 5 deletions tests/test_ext_napoleon_docstring.py
Expand Up @@ -1177,7 +1177,7 @@ class NumpyDocstringTest(BaseDocstringTest):
"""
Single line summary
:returns: *str* -- Extended
:returns: :class:`str` -- Extended
description of return value
"""
), (
Expand All @@ -1193,7 +1193,7 @@ class NumpyDocstringTest(BaseDocstringTest):
"""
Single line summary
:returns: *str* -- Extended
:returns: :class:`str` -- Extended
description of return value
"""
), (
Expand Down Expand Up @@ -1246,7 +1246,7 @@ class NumpyDocstringTest(BaseDocstringTest):
"""
Single line summary
:Yields: *str* -- Extended
:Yields: :class:`str` -- Extended
description of yielded value
"""
), (
Expand All @@ -1262,7 +1262,7 @@ class NumpyDocstringTest(BaseDocstringTest):
"""
Single line summary
:Yields: *str* -- Extended
:Yields: :class:`str` -- Extended
description of yielded value
"""
)]
Expand Down Expand Up @@ -1555,6 +1555,52 @@ def test_underscore_in_attribute_strip_signature_backslash(self):

self.assertEqual(expected, actual)

def test_return_types(self):
docstring = dedent("""
Returns
-------
DataFrame
a dataframe
""")
expected = dedent("""
:returns: a dataframe
:rtype: :class:`~pandas.DataFrame`
""")
translations = {
"DataFrame": "~pandas.DataFrame",
}
config = Config(
napoleon_use_param=True,
napoleon_use_rtype=True,
napoleon_preprocess_types=True,
napoleon_type_aliases=translations,
)
actual = str(NumpyDocstring(docstring, config))
self.assertEqual(expected, actual)

def test_yield_types(self):
docstring = dedent("""
Example Function
Yields
------
scalar or array-like
The result of the computation
""")
expected = dedent("""
Example Function
:Yields: :term:`scalar` or :class:`array-like <numpy.ndarray>` -- The result of the computation
""")
translations = {
"scalar": ":term:`scalar`",
"array-like": ":class:`array-like <numpy.ndarray>`",
}
config = Config(napoleon_type_aliases=translations, napoleon_preprocess_types=True)
app = mock.Mock()
actual = str(NumpyDocstring(docstring, config, app, "method"))
self.assertEqual(expected, actual)

def test_raises_types(self):
docstrings = [("""
Example Function
Expand Down Expand Up @@ -1719,6 +1765,34 @@ def test_raises_types(self):
("""
Example Function
Raises
------
CustomError
If the dimensions couldn't be parsed.
""", """
Example Function
:raises package.CustomError: If the dimensions couldn't be parsed.
"""),
################################
("""
Example Function
Raises
------
AnotherError
If the dimensions couldn't be parsed.
""", """
Example Function
:raises ~package.AnotherError: If the dimensions couldn't be parsed.
"""),
################################
("""
Example Function
Raises
------
:class:`exc.InvalidDimensionsError`
Expand All @@ -1731,7 +1805,11 @@ def test_raises_types(self):
:raises exc.InvalidArgumentsError:
""")]
for docstring, expected in docstrings:
config = Config()
translations = {
"CustomError": "package.CustomError",
"AnotherError": ":py:exc:`~package.AnotherError`",
}
config = Config(napoleon_type_aliases=translations, napoleon_preprocess_types=True)
app = mock.Mock()
actual = str(NumpyDocstring(docstring, config, app, "method"))
self.assertEqual(expected, actual)
Expand Down

0 comments on commit 68c89f4

Please sign in to comment.