mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #8095 from keewis/toggle-preprocessor
add a setting to enable / disable the numpy type preprocessor
This commit is contained in:
commit
6e62d33566
@ -41,6 +41,7 @@ class Config:
|
|||||||
napoleon_use_param = True
|
napoleon_use_param = True
|
||||||
napoleon_use_rtype = True
|
napoleon_use_rtype = True
|
||||||
napoleon_use_keyword = True
|
napoleon_use_keyword = True
|
||||||
|
napoleon_preprocess_types = False
|
||||||
napoleon_type_aliases = None
|
napoleon_type_aliases = None
|
||||||
napoleon_custom_sections = None
|
napoleon_custom_sections = None
|
||||||
|
|
||||||
@ -237,9 +238,12 @@ class Config:
|
|||||||
|
|
||||||
:returns: *bool* -- True if successful, False otherwise
|
:returns: *bool* -- True if successful, False otherwise
|
||||||
|
|
||||||
|
napoleon_preprocess_types : :obj:`bool` (Defaults to False)
|
||||||
|
Enable the type preprocessor for numpy style docstrings.
|
||||||
|
|
||||||
napoleon_type_aliases : :obj:`dict` (Defaults to None)
|
napoleon_type_aliases : :obj:`dict` (Defaults to None)
|
||||||
Add a mapping of strings to string, translating types in numpy
|
Add a mapping of strings to string, translating types in numpy
|
||||||
style docstrings.
|
style docstrings. Only works if ``napoleon_preprocess_types = True``.
|
||||||
|
|
||||||
napoleon_custom_sections : :obj:`list` (Defaults to None)
|
napoleon_custom_sections : :obj:`list` (Defaults to None)
|
||||||
Add a list of custom sections to include, expanding the list of parsed sections.
|
Add a list of custom sections to include, expanding the list of parsed sections.
|
||||||
@ -268,6 +272,7 @@ class Config:
|
|||||||
'napoleon_use_param': (True, 'env'),
|
'napoleon_use_param': (True, 'env'),
|
||||||
'napoleon_use_rtype': (True, 'env'),
|
'napoleon_use_rtype': (True, 'env'),
|
||||||
'napoleon_use_keyword': (True, 'env'),
|
'napoleon_use_keyword': (True, 'env'),
|
||||||
|
'napoleon_preprocess_types': (False, 'env'),
|
||||||
'napoleon_type_aliases': (None, 'env'),
|
'napoleon_type_aliases': (None, 'env'),
|
||||||
'napoleon_custom_sections': (None, 'env')
|
'napoleon_custom_sections': (None, 'env')
|
||||||
}
|
}
|
||||||
|
@ -1104,11 +1104,12 @@ class NumpyDocstring(GoogleDocstring):
|
|||||||
_name, _type = line, ''
|
_name, _type = line, ''
|
||||||
_name, _type = _name.strip(), _type.strip()
|
_name, _type = _name.strip(), _type.strip()
|
||||||
_name = self._escape_args_and_kwargs(_name)
|
_name = self._escape_args_and_kwargs(_name)
|
||||||
_type = _convert_numpy_type_spec(
|
if self._config.napoleon_preprocess_types:
|
||||||
_type,
|
_type = _convert_numpy_type_spec(
|
||||||
location=self._get_location(),
|
_type,
|
||||||
translations=self._config.napoleon_type_aliases or {},
|
location=self._get_location(),
|
||||||
)
|
translations=self._config.napoleon_type_aliases or {},
|
||||||
|
)
|
||||||
|
|
||||||
if prefer_type and not _type:
|
if prefer_type and not _type:
|
||||||
_type, _name = _name, _type
|
_type, _name = _name, _type
|
||||||
|
@ -66,19 +66,19 @@ Sample namedtuple subclass
|
|||||||
|
|
||||||
Quick description of attr1
|
Quick description of attr1
|
||||||
|
|
||||||
:type: :class:`Arbitrary type`
|
:type: Arbitrary type
|
||||||
|
|
||||||
.. attribute:: attr2
|
.. attribute:: attr2
|
||||||
|
|
||||||
Quick description of attr2
|
Quick description of attr2
|
||||||
|
|
||||||
:type: :class:`Another arbitrary type`
|
:type: Another arbitrary type
|
||||||
|
|
||||||
.. attribute:: attr3
|
.. attribute:: attr3
|
||||||
|
|
||||||
Adds a newline after the type
|
Adds a newline after the type
|
||||||
|
|
||||||
:type: :class:`Type`
|
:type: Type
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
@ -1311,12 +1311,34 @@ class NumpyDocstringTest(BaseDocstringTest):
|
|||||||
config = Config(
|
config = Config(
|
||||||
napoleon_use_param=False,
|
napoleon_use_param=False,
|
||||||
napoleon_use_rtype=False,
|
napoleon_use_rtype=False,
|
||||||
napoleon_use_keyword=False)
|
napoleon_use_keyword=False,
|
||||||
|
napoleon_preprocess_types=True)
|
||||||
for docstring, expected in self.docstrings:
|
for docstring, expected in self.docstrings:
|
||||||
actual = str(NumpyDocstring(dedent(docstring), config))
|
actual = str(NumpyDocstring(dedent(docstring), config))
|
||||||
expected = dedent(expected)
|
expected = dedent(expected)
|
||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
|
def test_type_preprocessor(self):
|
||||||
|
docstring = dedent("""
|
||||||
|
Single line summary
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
arg1:str
|
||||||
|
Extended
|
||||||
|
description of arg1
|
||||||
|
""")
|
||||||
|
|
||||||
|
config = Config(napoleon_preprocess_types=False, napoleon_use_param=False)
|
||||||
|
actual = str(NumpyDocstring(docstring, config))
|
||||||
|
expected = dedent("""
|
||||||
|
Single line summary
|
||||||
|
|
||||||
|
:Parameters: **arg1** (*str*) -- Extended
|
||||||
|
description of arg1
|
||||||
|
""")
|
||||||
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
def test_parameters_with_class_reference(self):
|
def test_parameters_with_class_reference(self):
|
||||||
docstring = """\
|
docstring = """\
|
||||||
Parameters
|
Parameters
|
||||||
@ -1352,7 +1374,7 @@ x1, x2 : array_like
|
|||||||
config = Config(napoleon_use_param=False)
|
config = Config(napoleon_use_param=False)
|
||||||
actual = str(NumpyDocstring(docstring, config))
|
actual = str(NumpyDocstring(docstring, config))
|
||||||
expected = """\
|
expected = """\
|
||||||
:Parameters: **x1, x2** (:class:`array_like`) -- Input arrays, description of ``x1``, ``x2``.
|
:Parameters: **x1, x2** (*array_like*) -- Input arrays, description of ``x1``, ``x2``.
|
||||||
"""
|
"""
|
||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
@ -1360,9 +1382,9 @@ x1, x2 : array_like
|
|||||||
actual = str(NumpyDocstring(dedent(docstring), config))
|
actual = str(NumpyDocstring(dedent(docstring), config))
|
||||||
expected = """\
|
expected = """\
|
||||||
:param x1: Input arrays, description of ``x1``, ``x2``.
|
:param x1: Input arrays, description of ``x1``, ``x2``.
|
||||||
:type x1: :class:`array_like`
|
:type x1: array_like
|
||||||
:param x2: Input arrays, description of ``x1``, ``x2``.
|
:param x2: Input arrays, description of ``x1``, ``x2``.
|
||||||
:type x2: :class:`array_like`
|
:type x2: array_like
|
||||||
"""
|
"""
|
||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
@ -1377,7 +1399,7 @@ param1 : MyClass instance
|
|||||||
config = Config(napoleon_use_param=False)
|
config = Config(napoleon_use_param=False)
|
||||||
actual = str(NumpyDocstring(docstring, config))
|
actual = str(NumpyDocstring(docstring, config))
|
||||||
expected = """\
|
expected = """\
|
||||||
:Parameters: **param1** (:class:`MyClass instance`)
|
:Parameters: **param1** (*MyClass instance*)
|
||||||
"""
|
"""
|
||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
@ -1385,7 +1407,7 @@ param1 : MyClass instance
|
|||||||
actual = str(NumpyDocstring(dedent(docstring), config))
|
actual = str(NumpyDocstring(dedent(docstring), config))
|
||||||
expected = """\
|
expected = """\
|
||||||
:param param1:
|
:param param1:
|
||||||
:type param1: :class:`MyClass instance`
|
:type param1: MyClass instance
|
||||||
"""
|
"""
|
||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
@ -1474,7 +1496,7 @@ arg_ : type
|
|||||||
|
|
||||||
expected = """
|
expected = """
|
||||||
:ivar arg_: some description
|
:ivar arg_: some description
|
||||||
:vartype arg_: :class:`type`
|
:vartype arg_: type
|
||||||
"""
|
"""
|
||||||
|
|
||||||
config = Config(napoleon_use_ivar=True)
|
config = Config(napoleon_use_ivar=True)
|
||||||
@ -1494,7 +1516,7 @@ arg_ : type
|
|||||||
|
|
||||||
expected = """
|
expected = """
|
||||||
:ivar arg\\_: some description
|
:ivar arg\\_: some description
|
||||||
:vartype arg\\_: :class:`type`
|
:vartype arg\\_: type
|
||||||
"""
|
"""
|
||||||
|
|
||||||
config = Config(napoleon_use_ivar=True)
|
config = Config(napoleon_use_ivar=True)
|
||||||
@ -1862,59 +1884,59 @@ definition_after_normal_text : int
|
|||||||
expected = """One line summary.
|
expected = """One line summary.
|
||||||
|
|
||||||
:param no_list:
|
:param no_list:
|
||||||
:type no_list: :class:`int`
|
:type no_list: int
|
||||||
:param one_bullet_empty:
|
:param one_bullet_empty:
|
||||||
*
|
*
|
||||||
:type one_bullet_empty: :class:`int`
|
:type one_bullet_empty: int
|
||||||
:param one_bullet_single_line:
|
:param one_bullet_single_line:
|
||||||
- first line
|
- first line
|
||||||
:type one_bullet_single_line: :class:`int`
|
:type one_bullet_single_line: int
|
||||||
:param one_bullet_two_lines:
|
:param one_bullet_two_lines:
|
||||||
+ first line
|
+ first line
|
||||||
continued
|
continued
|
||||||
:type one_bullet_two_lines: :class:`int`
|
:type one_bullet_two_lines: int
|
||||||
:param two_bullets_single_line:
|
:param two_bullets_single_line:
|
||||||
- first line
|
- first line
|
||||||
- second line
|
- second line
|
||||||
:type two_bullets_single_line: :class:`int`
|
:type two_bullets_single_line: int
|
||||||
:param two_bullets_two_lines:
|
:param two_bullets_two_lines:
|
||||||
* first line
|
* first line
|
||||||
continued
|
continued
|
||||||
* second line
|
* second line
|
||||||
continued
|
continued
|
||||||
:type two_bullets_two_lines: :class:`int`
|
:type two_bullets_two_lines: int
|
||||||
:param one_enumeration_single_line:
|
:param one_enumeration_single_line:
|
||||||
1. first line
|
1. first line
|
||||||
:type one_enumeration_single_line: :class:`int`
|
:type one_enumeration_single_line: int
|
||||||
:param one_enumeration_two_lines:
|
:param one_enumeration_two_lines:
|
||||||
1) first line
|
1) first line
|
||||||
continued
|
continued
|
||||||
:type one_enumeration_two_lines: :class:`int`
|
:type one_enumeration_two_lines: int
|
||||||
:param two_enumerations_one_line:
|
:param two_enumerations_one_line:
|
||||||
(iii) first line
|
(iii) first line
|
||||||
(iv) second line
|
(iv) second line
|
||||||
:type two_enumerations_one_line: :class:`int`
|
:type two_enumerations_one_line: int
|
||||||
:param two_enumerations_two_lines:
|
:param two_enumerations_two_lines:
|
||||||
a. first line
|
a. first line
|
||||||
continued
|
continued
|
||||||
b. second line
|
b. second line
|
||||||
continued
|
continued
|
||||||
:type two_enumerations_two_lines: :class:`int`
|
:type two_enumerations_two_lines: int
|
||||||
:param one_definition_one_line:
|
:param one_definition_one_line:
|
||||||
item 1
|
item 1
|
||||||
first line
|
first line
|
||||||
:type one_definition_one_line: :class:`int`
|
:type one_definition_one_line: int
|
||||||
:param one_definition_two_lines:
|
:param one_definition_two_lines:
|
||||||
item 1
|
item 1
|
||||||
first line
|
first line
|
||||||
continued
|
continued
|
||||||
:type one_definition_two_lines: :class:`int`
|
:type one_definition_two_lines: int
|
||||||
:param two_definitions_one_line:
|
:param two_definitions_one_line:
|
||||||
item 1
|
item 1
|
||||||
first line
|
first line
|
||||||
item 2
|
item 2
|
||||||
second line
|
second line
|
||||||
:type two_definitions_one_line: :class:`int`
|
:type two_definitions_one_line: int
|
||||||
:param two_definitions_two_lines:
|
:param two_definitions_two_lines:
|
||||||
item 1
|
item 1
|
||||||
first line
|
first line
|
||||||
@ -1922,14 +1944,14 @@ definition_after_normal_text : int
|
|||||||
item 2
|
item 2
|
||||||
second line
|
second line
|
||||||
continued
|
continued
|
||||||
:type two_definitions_two_lines: :class:`int`
|
:type two_definitions_two_lines: int
|
||||||
:param one_definition_blank_line:
|
:param one_definition_blank_line:
|
||||||
item 1
|
item 1
|
||||||
|
|
||||||
first line
|
first line
|
||||||
|
|
||||||
extra first line
|
extra first line
|
||||||
:type one_definition_blank_line: :class:`int`
|
:type one_definition_blank_line: int
|
||||||
:param two_definitions_blank_lines:
|
:param two_definitions_blank_lines:
|
||||||
item 1
|
item 1
|
||||||
|
|
||||||
@ -1942,12 +1964,12 @@ definition_after_normal_text : int
|
|||||||
second line
|
second line
|
||||||
|
|
||||||
extra second line
|
extra second line
|
||||||
:type two_definitions_blank_lines: :class:`int`
|
:type two_definitions_blank_lines: int
|
||||||
:param definition_after_normal_text: text line
|
:param definition_after_normal_text: text line
|
||||||
|
|
||||||
item 1
|
item 1
|
||||||
first line
|
first line
|
||||||
:type definition_after_normal_text: :class:`int`
|
:type definition_after_normal_text: int
|
||||||
"""
|
"""
|
||||||
config = Config(napoleon_use_param=True)
|
config = Config(napoleon_use_param=True)
|
||||||
actual = str(NumpyDocstring(docstring, config))
|
actual = str(NumpyDocstring(docstring, config))
|
||||||
@ -2041,7 +2063,7 @@ definition_after_normal_text : int
|
|||||||
item 1
|
item 1
|
||||||
first line
|
first line
|
||||||
"""
|
"""
|
||||||
config = Config(napoleon_use_param=False)
|
config = Config(napoleon_use_param=False, napoleon_preprocess_types=True)
|
||||||
actual = str(NumpyDocstring(docstring, config))
|
actual = str(NumpyDocstring(docstring, config))
|
||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
@ -2222,6 +2244,7 @@ definition_after_normal_text : int
|
|||||||
config = Config(
|
config = Config(
|
||||||
napoleon_use_param=True,
|
napoleon_use_param=True,
|
||||||
napoleon_use_rtype=True,
|
napoleon_use_rtype=True,
|
||||||
|
napoleon_preprocess_types=True,
|
||||||
napoleon_type_aliases=translations,
|
napoleon_type_aliases=translations,
|
||||||
)
|
)
|
||||||
actual = str(NumpyDocstring(docstring, config))
|
actual = str(NumpyDocstring(docstring, config))
|
||||||
|
Loading…
Reference in New Issue
Block a user