Skip to content

Commit 68c89f4

Browse files
authored
Merge pull request #8050 from keewis/preprocess-other-sections
Preprocess other sections
2 parents 751b7a0 + 439f75a commit 68c89f4

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed

sphinx/ext/napoleon/docstring.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,9 @@ def _parse_raises_section(self, section: str) -> List[str]:
699699
m = self._name_rgx.match(_type)
700700
if m and m.group('name'):
701701
_type = m.group('name')
702+
elif _xref_regex.match(_type):
703+
pos = _type.find('`')
704+
_type = _type[pos + 1:-1]
702705
_type = ' ' + _type if _type else ''
703706
_desc = self._strip_empty(_desc)
704707
_descs = ' ' + '\n '.join(_desc) if any(_desc) else ''
@@ -1104,15 +1107,17 @@ def _consume_field(self, parse_type: bool = True, prefer_type: bool = False
11041107
_name, _type = line, ''
11051108
_name, _type = _name.strip(), _type.strip()
11061109
_name = self._escape_args_and_kwargs(_name)
1110+
1111+
if prefer_type and not _type:
1112+
_type, _name = _name, _type
1113+
11071114
if self._config.napoleon_preprocess_types:
11081115
_type = _convert_numpy_type_spec(
11091116
_type,
11101117
location=self._get_location(),
11111118
translations=self._config.napoleon_type_aliases or {},
11121119
)
11131120

1114-
if prefer_type and not _type:
1115-
_type, _name = _name, _type
11161121
indent = self._get_indent(line) + 1
11171122
_desc = self._dedent(self._consume_indented_block(indent))
11181123
_desc = self.__class__(_desc, self._config).lines()

tests/test_ext_napoleon_docstring.py

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ class NumpyDocstringTest(BaseDocstringTest):
11771177
"""
11781178
Single line summary
11791179
1180-
:returns: *str* -- Extended
1180+
:returns: :class:`str` -- Extended
11811181
description of return value
11821182
"""
11831183
), (
@@ -1193,7 +1193,7 @@ class NumpyDocstringTest(BaseDocstringTest):
11931193
"""
11941194
Single line summary
11951195
1196-
:returns: *str* -- Extended
1196+
:returns: :class:`str` -- Extended
11971197
description of return value
11981198
"""
11991199
), (
@@ -1246,7 +1246,7 @@ class NumpyDocstringTest(BaseDocstringTest):
12461246
"""
12471247
Single line summary
12481248
1249-
:Yields: *str* -- Extended
1249+
:Yields: :class:`str` -- Extended
12501250
description of yielded value
12511251
"""
12521252
), (
@@ -1262,7 +1262,7 @@ class NumpyDocstringTest(BaseDocstringTest):
12621262
"""
12631263
Single line summary
12641264
1265-
:Yields: *str* -- Extended
1265+
:Yields: :class:`str` -- Extended
12661266
description of yielded value
12671267
"""
12681268
)]
@@ -1555,6 +1555,52 @@ def test_underscore_in_attribute_strip_signature_backslash(self):
15551555

15561556
self.assertEqual(expected, actual)
15571557

1558+
def test_return_types(self):
1559+
docstring = dedent("""
1560+
Returns
1561+
-------
1562+
DataFrame
1563+
a dataframe
1564+
""")
1565+
expected = dedent("""
1566+
:returns: a dataframe
1567+
:rtype: :class:`~pandas.DataFrame`
1568+
""")
1569+
translations = {
1570+
"DataFrame": "~pandas.DataFrame",
1571+
}
1572+
config = Config(
1573+
napoleon_use_param=True,
1574+
napoleon_use_rtype=True,
1575+
napoleon_preprocess_types=True,
1576+
napoleon_type_aliases=translations,
1577+
)
1578+
actual = str(NumpyDocstring(docstring, config))
1579+
self.assertEqual(expected, actual)
1580+
1581+
def test_yield_types(self):
1582+
docstring = dedent("""
1583+
Example Function
1584+
1585+
Yields
1586+
------
1587+
scalar or array-like
1588+
The result of the computation
1589+
""")
1590+
expected = dedent("""
1591+
Example Function
1592+
1593+
:Yields: :term:`scalar` or :class:`array-like <numpy.ndarray>` -- The result of the computation
1594+
""")
1595+
translations = {
1596+
"scalar": ":term:`scalar`",
1597+
"array-like": ":class:`array-like <numpy.ndarray>`",
1598+
}
1599+
config = Config(napoleon_type_aliases=translations, napoleon_preprocess_types=True)
1600+
app = mock.Mock()
1601+
actual = str(NumpyDocstring(docstring, config, app, "method"))
1602+
self.assertEqual(expected, actual)
1603+
15581604
def test_raises_types(self):
15591605
docstrings = [("""
15601606
Example Function
@@ -1719,6 +1765,34 @@ def test_raises_types(self):
17191765
("""
17201766
Example Function
17211767
1768+
Raises
1769+
------
1770+
CustomError
1771+
If the dimensions couldn't be parsed.
1772+
1773+
""", """
1774+
Example Function
1775+
1776+
:raises package.CustomError: If the dimensions couldn't be parsed.
1777+
"""),
1778+
################################
1779+
("""
1780+
Example Function
1781+
1782+
Raises
1783+
------
1784+
AnotherError
1785+
If the dimensions couldn't be parsed.
1786+
1787+
""", """
1788+
Example Function
1789+
1790+
:raises ~package.AnotherError: If the dimensions couldn't be parsed.
1791+
"""),
1792+
################################
1793+
("""
1794+
Example Function
1795+
17221796
Raises
17231797
------
17241798
:class:`exc.InvalidDimensionsError`
@@ -1731,7 +1805,11 @@ def test_raises_types(self):
17311805
:raises exc.InvalidArgumentsError:
17321806
""")]
17331807
for docstring, expected in docstrings:
1734-
config = Config()
1808+
translations = {
1809+
"CustomError": "package.CustomError",
1810+
"AnotherError": ":py:exc:`~package.AnotherError`",
1811+
}
1812+
config = Config(napoleon_type_aliases=translations, napoleon_preprocess_types=True)
17351813
app = mock.Mock()
17361814
actual = str(NumpyDocstring(docstring, config, app, "method"))
17371815
self.assertEqual(expected, actual)

0 commit comments

Comments
 (0)