Migrate to py3 style type annotation: sphinx.ext.napoleon

This commit is contained in:
Takeshi KOMIYA 2019-07-03 01:33:58 +09:00
parent dd9d02007c
commit c270efccd7

View File

@ -8,14 +8,12 @@
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
from typing import Any, Dict, List
from sphinx import __display_version__ as __version__ from sphinx import __display_version__ as __version__
from sphinx.application import Sphinx from sphinx.application import Sphinx
from sphinx.ext.napoleon.docstring import GoogleDocstring, NumpyDocstring from sphinx.ext.napoleon.docstring import GoogleDocstring, NumpyDocstring
if False:
# For type annotation
from typing import Any, Dict, List # NOQA
class Config: class Config:
"""Sphinx napoleon extension settings in `conf.py`. """Sphinx napoleon extension settings in `conf.py`.
@ -267,16 +265,14 @@ class Config:
'napoleon_custom_sections': (None, 'env') 'napoleon_custom_sections': (None, 'env')
} }
def __init__(self, **settings): def __init__(self, **settings) -> None:
# type: (Any) -> None
for name, (default, rebuild) in self._config_values.items(): for name, (default, rebuild) in self._config_values.items():
setattr(self, name, default) setattr(self, name, default)
for name, value in settings.items(): for name, value in settings.items():
setattr(self, name, value) setattr(self, name, value)
def setup(app): def setup(app: Sphinx) -> Dict[str, Any]:
# type: (Sphinx) -> Dict[str, Any]
"""Sphinx extension setup function. """Sphinx extension setup function.
When the extension is loaded, Sphinx imports this module and executes When the extension is loaded, Sphinx imports this module and executes
@ -313,8 +309,7 @@ def setup(app):
return {'version': __version__, 'parallel_read_safe': True} return {'version': __version__, 'parallel_read_safe': True}
def _patch_python_domain(): def _patch_python_domain() -> None:
# type: () -> None
try: try:
from sphinx.domains.python import PyTypedField from sphinx.domains.python import PyTypedField
except ImportError: except ImportError:
@ -333,8 +328,8 @@ def _patch_python_domain():
can_collapse=True)) can_collapse=True))
def _process_docstring(app, what, name, obj, options, lines): def _process_docstring(app: Sphinx, what: str, name: str, obj: Any,
# type: (Sphinx, str, str, Any, Any, List[str]) -> None options: Any, lines: List[str]) -> None:
"""Process the docstring for a given python object. """Process the docstring for a given python object.
Called when autodoc has read and processed a docstring. `lines` is a list Called when autodoc has read and processed a docstring. `lines` is a list
@ -383,8 +378,8 @@ def _process_docstring(app, what, name, obj, options, lines):
lines[:] = result_lines[:] lines[:] = result_lines[:]
def _skip_member(app, what, name, obj, skip, options): def _skip_member(app: Sphinx, what: str, name: str, obj: Any,
# type: (Sphinx, str, str, Any, bool, Any) -> bool skip: bool, options: Any) -> bool:
"""Determine if private and special class members are included in docs. """Determine if private and special class members are included in docs.
The following settings in conf.py determine if private and special class The following settings in conf.py determine if private and special class