mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #5663 from tk0miya/fix_typehints
Fix annotations for registry, roles and util.nodes
This commit is contained in:
commit
8c3a066983
@ -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, {})
|
||||
|
@ -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):
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user