summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2018-11-23 23:41:43 +0900
committerGitHub <noreply@github.com>2018-11-23 23:41:43 +0900
commit8c3a06698371e356c867d7661c578058ccf11bb4 (patch)
tree2feb21271e4d6e6c13d0d847e9be9c43e9e32a4d
parent0c17f0ca897d344d8653dde4992ede93abf5a457 (diff)
parentd1aeedf67e7cb8c28cc09ea9b44a5e16f88ff07e (diff)
downloadsphinx-git-8c3a06698371e356c867d7661c578058ccf11bb4.tar.gz
Merge pull request #5663 from tk0miya/fix_typehints
Fix annotations for registry, roles and util.nodes
-rw-r--r--sphinx/registry.py10
-rw-r--r--sphinx/roles.py7
-rw-r--r--sphinx/util/nodes.py16
3 files changed, 18 insertions, 15 deletions
diff --git a/sphinx/registry.py b/sphinx/registry.py
index 523afea9c..03bdb885e 100644
--- a/sphinx/registry.py
+++ b/sphinx/registry.py
@@ -110,7 +110,7 @@ class SphinxComponentRegistry:
self.source_parsers = {} # type: Dict[unicode, Type[Parser]]
#: source inputs; file type -> input class
- self.source_inputs = {} # type: Dict[unicode, Input]
+ self.source_inputs = {} # type: Dict[unicode, Type[Input]]
#: source suffix: suffix -> file type
self.source_suffix = {} # type: Dict[unicode, unicode]
@@ -231,7 +231,7 @@ class SphinxComponentRegistry:
def add_object_type(self, directivename, rolename, indextemplate='',
parse_node=None, ref_nodeclass=None, objname='',
doc_field_types=[], override=False):
- # type: (unicode, unicode, unicode, Callable, Type[nodes.Node], unicode, List, bool) -> None # NOQA
+ # type: (unicode, unicode, unicode, Callable, Type[nodes.TextElement], unicode, List, bool) -> None # NOQA
logger.debug('[app] adding object type: %r',
(directivename, rolename, indextemplate, parse_node,
ref_nodeclass, objname, doc_field_types))
@@ -254,7 +254,7 @@ class SphinxComponentRegistry:
def add_crossref_type(self, directivename, rolename, indextemplate='',
ref_nodeclass=None, objname='', override=False):
- # type: (unicode, unicode, unicode, nodes.Node, unicode, bool) -> None
+ # type: (unicode, unicode, unicode, Type[nodes.TextElement], unicode, bool) -> None
logger.debug('[app] adding crossref type: %r',
(directivename, rolename, indextemplate, ref_nodeclass, objname))
@@ -325,7 +325,7 @@ class SphinxComponentRegistry:
raise SphinxError(__('Source parser for %s not registered') % filetype)
def get_source_parsers(self):
- # type: () -> Dict[unicode, Parser]
+ # type: () -> Dict[unicode, Type[Parser]]
return self.source_parsers
def create_source_parser(self, app, filename):
@@ -363,7 +363,7 @@ class SphinxComponentRegistry:
self.translators[name] = translator
def add_translation_handlers(self, node, **kwargs):
- # type: (nodes.Node, Any) -> None
+ # type: (Type[nodes.Element], Any) -> None
logger.debug('[app] adding translation_handlers: %r, %r', node, kwargs)
for builder_name, handlers in kwargs.items():
translation_handlers = self.translation_handlers.setdefault(builder_name, {})
diff --git a/sphinx/roles.py b/sphinx/roles.py
index dd89b77d3..454dcb802 100644
--- a/sphinx/roles.py
+++ b/sphinx/roles.py
@@ -26,6 +26,7 @@ if False:
from docutils.parsers.rst.states import Inliner # NOQA
from sphinx.application import Sphinx # NOQA
from sphinx.environment import BuildEnvironment # NOQA
+ from sphinx.util.typing import RoleFunction # NOQA
generic_docroles = {
@@ -68,8 +69,8 @@ class XRefRole:
* Subclassing and overwriting `process_link()` and/or `result_nodes()`.
"""
- nodeclass = addnodes.pending_xref # type: Type[nodes.Node]
- innernodeclass = nodes.literal
+ nodeclass = addnodes.pending_xref # type: Type[nodes.Element]
+ innernodeclass = nodes.literal # type: Type[nodes.TextElement]
def __init__(self, fix_parens=False, lowercase=False,
nodeclass=None, innernodeclass=None, warn_dangling=False):
@@ -395,7 +396,7 @@ specific_docroles = {
'samp': emph_literal_role,
'abbr': abbr_role,
'index': index_role,
-}
+} # type: Dict[str, RoleFunction]
def setup(app):
diff --git a/sphinx/util/nodes.py b/sphinx/util/nodes.py
index ff73819ee..f98c72b37 100644
--- a/sphinx/util/nodes.py
+++ b/sphinx/util/nodes.py
@@ -22,7 +22,7 @@ from sphinx.util import logging
if False:
# For type annotation
- from typing import Any, Callable, Iterable, List, Set, Tuple, Optional # NOQA
+ from typing import Any, Callable, Iterable, List, Optional, Set, Tuple, Type # NOQA
from sphinx.builders import Builder # NOQA
from sphinx.utils.tags import Tags # NOQA
@@ -57,7 +57,7 @@ class NodeMatcher:
"""
def __init__(self, *classes, **attrs):
- # type: (nodes.Node, Any) -> None
+ # type: (Type[nodes.Node], Any) -> None
self.classes = classes
self.attrs = attrs
@@ -118,7 +118,7 @@ def repr_domxml(node, length=80):
def apply_source_workaround(node):
- # type: (nodes.Node) -> None
+ # type: (nodes.Element) -> None
# workaround: nodes.term have wrong rawsource if classifier is specified.
# The behavior of docutils-0.11, 0.12 is:
# * when ``term text : classifier1 : classifier2`` is specified,
@@ -247,8 +247,9 @@ META_TYPE_NODES = (
def extract_messages(doctree):
- # type: (nodes.Node) -> Iterable[Tuple[nodes.Node, unicode]]
+ # type: (nodes.Element) -> Iterable[Tuple[nodes.Node, unicode]]
"""Extract translatable messages from a document tree."""
+ node = None # type: nodes.Element
for node in doctree.traverse(is_translatable):
if isinstance(node, addnodes.translatable):
for msg in node.extract_original_messages():
@@ -291,8 +292,9 @@ def traverse_parent(node, cls=None):
def traverse_translatable_index(doctree):
- # type: (nodes.Node) -> Iterable[Tuple[nodes.Node, List[unicode]]]
+ # type: (nodes.Element) -> Iterable[Tuple[nodes.Element, List[unicode]]]
"""Traverse translatable index node from a document tree."""
+ node = None # type: nodes.Element
for node in doctree.traverse(NodeMatcher(addnodes.index, inline=False)):
if 'raw_entries' in node:
entries = node['raw_entries']
@@ -322,7 +324,7 @@ def nested_parse_with_titles(state, content, node):
def clean_astext(node):
- # type: (nodes.Node) -> unicode
+ # type: (nodes.Element) -> unicode
"""Like node.astext(), but ignore images."""
node = node.deepcopy()
for img in node.traverse(nodes.image):
@@ -495,7 +497,7 @@ def process_only_nodes(document, tags):
# monkey-patch Element.copy to copy the rawsource and line
def _new_copy(self):
- # type: (nodes.Node) -> nodes.Node
+ # type: (nodes.Element) -> nodes.Element
newnode = self.__class__(self.rawsource, **self.attributes)
if isinstance(self, nodes.Element):
newnode.source = self.source