Merge pull request #5740 from tk0miya/fix_typehints_for_TextlikeNode

Add TextlikeNode type for docfields
This commit is contained in:
Takeshi KOMIYA 2018-12-10 09:02:32 +09:00 committed by GitHub
commit 81464948cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 9 deletions

View File

@ -31,7 +31,7 @@ if False:
from sphinx.application import Sphinx # NOQA from sphinx.application import Sphinx # NOQA
from sphinx.builders import Builder # NOQA from sphinx.builders import Builder # NOQA
from sphinx.environment import BuildEnvironment # NOQA from sphinx.environment import BuildEnvironment # NOQA
from sphinx.util.typing import unicode # NOQA from sphinx.util.typing import TextlikeNode, unicode # NOQA
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -120,7 +120,7 @@ class PyXrefMixin:
rolename, # type: unicode rolename, # type: unicode
domain, # type: unicode domain, # type: unicode
target, # type: unicode target, # type: unicode
innernode=nodes.emphasis, # type: Type[nodes.TextElement] innernode=nodes.emphasis, # type: Type[TextlikeNode]
contnode=None, # type: nodes.Node contnode=None, # type: nodes.Node
env=None, # type: BuildEnvironment env=None, # type: BuildEnvironment
): ):
@ -143,7 +143,7 @@ class PyXrefMixin:
rolename, # type: unicode rolename, # type: unicode
domain, # type: unicode domain, # type: unicode
target, # type: unicode target, # type: unicode
innernode=nodes.emphasis, # type: Type[nodes.TextElement] innernode=nodes.emphasis, # type: Type[TextlikeNode]
contnode=None, # type: nodes.Node contnode=None, # type: nodes.Node
env=None, # type: BuildEnvironment env=None, # type: BuildEnvironment
): ):
@ -171,7 +171,7 @@ class PyXrefMixin:
class PyField(PyXrefMixin, Field): class PyField(PyXrefMixin, Field):
def make_xref(self, rolename, domain, target, def make_xref(self, rolename, domain, target,
innernode=nodes.emphasis, contnode=None, env=None): innernode=nodes.emphasis, contnode=None, env=None):
# type: (unicode, unicode, unicode, Type[nodes.TextElement], nodes.Node, BuildEnvironment) -> nodes.Node # NOQA # type: (unicode, unicode, unicode, Type[TextlikeNode], nodes.Node, BuildEnvironment) -> nodes.Node # NOQA
if rolename == 'class' and target == 'None': if rolename == 'class' and target == 'None':
# None is not a type, so use obj role instead. # None is not a type, so use obj role instead.
rolename = 'obj' rolename = 'obj'
@ -187,7 +187,7 @@ class PyGroupedField(PyXrefMixin, GroupedField):
class PyTypedField(PyXrefMixin, TypedField): class PyTypedField(PyXrefMixin, TypedField):
def make_xref(self, rolename, domain, target, def make_xref(self, rolename, domain, target,
innernode=nodes.emphasis, contnode=None, env=None): innernode=nodes.emphasis, contnode=None, env=None):
# type: (unicode, unicode, unicode, Type[nodes.TextElement], nodes.Node, BuildEnvironment) -> nodes.Node # NOQA # type: (unicode, unicode, unicode, Type[TextlikeNode], nodes.Node, BuildEnvironment) -> nodes.Node # NOQA
if rolename == 'class' and target == 'None': if rolename == 'class' and target == 'None':
# None is not a type, so use obj role instead. # None is not a type, so use obj role instead.
rolename = 'obj' rolename = 'obj'

View File

@ -22,7 +22,7 @@ if False:
from typing import Any, Dict, Tuple, Type # NOQA from typing import Any, Dict, Tuple, Type # NOQA
from sphinx.domains import Domain # NOQA from sphinx.domains import Domain # NOQA
from sphinx.environment import BuildEnvironment # NOQA from sphinx.environment import BuildEnvironment # NOQA
from sphinx.util.typing import unicode # NOQA from sphinx.util.typing import TextlikeNode, unicode # NOQA
def _is_single_paragraph(node): def _is_single_paragraph(node):
@ -69,7 +69,7 @@ class Field:
rolename, # type: unicode rolename, # type: unicode
domain, # type: unicode domain, # type: unicode
target, # type: unicode target, # type: unicode
innernode=addnodes.literal_emphasis, # type: Type[nodes.TextElement] innernode=addnodes.literal_emphasis, # type: Type[TextlikeNode]
contnode=None, # type: nodes.Node contnode=None, # type: nodes.Node
env=None, # type: BuildEnvironment env=None, # type: BuildEnvironment
): ):
@ -87,7 +87,7 @@ class Field:
rolename, # type: unicode rolename, # type: unicode
domain, # type: unicode domain, # type: unicode
target, # type: unicode target, # type: unicode
innernode=addnodes.literal_emphasis, # type: Type[nodes.TextElement] innernode=addnodes.literal_emphasis, # type: Type[TextlikeNode]
contnode=None, # type: nodes.Node contnode=None, # type: nodes.Node
env=None, # type: BuildEnvironment env=None, # type: BuildEnvironment
): ):

View File

@ -9,7 +9,7 @@
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
from typing import Any, Callable, Dict, List, Tuple from typing import Any, Callable, Dict, List, Tuple, Union
from docutils import nodes from docutils import nodes
from docutils.parsers.rst.states import Inliner from docutils.parsers.rst.states import Inliner
@ -26,6 +26,9 @@ else:
# An entry of Directive.option_spec # An entry of Directive.option_spec
DirectiveOption = Callable[[str], Any] DirectiveOption = Callable[[str], Any]
# Text like nodes which are initialized with text and rawsource
TextlikeNode = Union[nodes.Text, nodes.TextElement]
# common role functions # common role functions
RoleFunction = Callable[[text_type, text_type, text_type, int, Inliner, Dict, List[text_type]], RoleFunction = Callable[[text_type, text_type, text_type, int, Inliner, Dict, List[text_type]],
Tuple[List[nodes.Node], List[nodes.system_message]]] Tuple[List[nodes.Node], List[nodes.system_message]]]