Fix typehints: sphinx.domains

This commit is contained in:
Takeshi KOMIYA 2018-11-05 01:04:33 +09:00
parent 67cbc7d96c
commit 99bb07740e
5 changed files with 20 additions and 20 deletions

View File

@ -17,7 +17,7 @@ from sphinx.locale import _
if False: if False:
# For type annotation # For type annotation
from typing import Any, Callable, Dict, Iterable, List, Tuple, Type, Union # NOQA from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Type, Union # NOQA
from docutils import nodes # NOQA from docutils import nodes # NOQA
from docutils.parsers.rst.states import Inliner # NOQA from docutils.parsers.rst.states import Inliner # NOQA
from sphinx.builders import Builder # NOQA from sphinx.builders import Builder # NOQA
@ -149,7 +149,7 @@ class Domain:
#: role name -> a warning message if reference is missing #: role name -> a warning message if reference is missing
dangling_warnings = {} # type: Dict[unicode, unicode] dangling_warnings = {} # type: Dict[unicode, unicode]
#: node_class -> (enum_node_type, title_getter) #: node_class -> (enum_node_type, title_getter)
enumerable_nodes = {} # type: Dict[nodes.Node, Tuple[unicode, Callable]] enumerable_nodes = {} # type: Dict[Type[nodes.Node], Tuple[unicode, Callable]]
#: data value for a fresh environment #: data value for a fresh environment
initial_data = {} # type: Dict initial_data = {} # type: Dict
@ -201,7 +201,7 @@ class Domain:
self._role2type.setdefault(role, []).append(name) self._role2type.setdefault(role, []).append(name)
def role(self, name): def role(self, name):
# type: (unicode) -> Callable # type: (unicode) -> RoleFunction
"""Return a role adapter function that always gives the registered """Return a role adapter function that always gives the registered
role its full name ('domain:name') as the first argument. role its full name ('domain:name') as the first argument.
""" """
@ -212,7 +212,7 @@ class Domain:
fullname = '%s:%s' % (self.name, name) fullname = '%s:%s' % (self.name, name)
def role_adapter(typ, rawtext, text, lineno, inliner, options={}, content=[]): def role_adapter(typ, rawtext, text, lineno, inliner, options={}, content=[]):
# type: (unicode, unicode, unicode, int, Inliner, Dict, List[unicode]) -> nodes.Node # NOQA # type: (unicode, unicode, unicode, int, Inliner, Optional[Dict], Optional[List[unicode]]) -> Tuple[List[nodes.Node], List[nodes.Node]] # NOQA
return self.roles[name](fullname, rawtext, text, lineno, return self.roles[name](fullname, rawtext, text, lineno,
inliner, options, content) inliner, options, content)
self._role_cache[name] = role_adapter self._role_cache[name] = role_adapter

View File

@ -63,7 +63,7 @@ class VersionChange(SphinxDirective):
def run(self): def run(self):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
node = addnodes.versionmodified() node = addnodes.versionmodified() # type: nodes.Node
node.document = self.state.document node.document = self.state.document
set_source_info(self, node) set_source_info(self, node)
node['type'] = self.name node['type'] = self.name

View File

@ -21,11 +21,10 @@ from sphinx.util.nodes import make_refnode
if False: if False:
# For type annotation # For type annotation
from typing import Any, Callable, Dict, Iterable, List, Tuple, Union # NOQA from typing import Any, Callable, Dict, Iterable, List, Tuple, Type, Union # NOQA
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 RoleFunction # NOQA
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -52,7 +51,7 @@ class MathDomain(Domain):
enumerable_nodes = { # node_class -> (figtype, title_getter) enumerable_nodes = { # node_class -> (figtype, title_getter)
displaymath: ('displaymath', None), displaymath: ('displaymath', None),
nodes.math_block: ('displaymath', None), nodes.math_block: ('displaymath', None),
} # type: Dict[nodes.Node, Tuple[unicode, Callable]] } # type: Dict[Type[nodes.Node], Tuple[unicode, Callable]]
roles = { roles = {
'numref': MathReferenceRole(), 'numref': MathReferenceRole(),
} }

View File

@ -16,7 +16,7 @@ from copy import copy
from docutils import nodes from docutils import nodes
from docutils.parsers.rst import directives from docutils.parsers.rst import directives
from docutils.statemachine import ViewList from docutils.statemachine import StringList
from sphinx import addnodes from sphinx import addnodes
from sphinx.deprecation import RemovedInSphinx30Warning from sphinx.deprecation import RemovedInSphinx30Warning
@ -127,7 +127,7 @@ class Target(SphinxDirective):
targetname = '%s-%s' % (self.name, fullname) targetname = '%s-%s' % (self.name, fullname)
node = nodes.target('', '', ids=[targetname]) node = nodes.target('', '', ids=[targetname])
self.state.document.note_explicit_target(node) self.state.document.note_explicit_target(node)
ret = [node] ret = [node] # type: List[nodes.Node]
if self.indextemplate: if self.indextemplate:
indexentry = self.indextemplate % (fullname,) indexentry = self.indextemplate % (fullname,)
indextype = 'single' indextype = 'single'
@ -294,7 +294,7 @@ class Glossary(SphinxDirective):
# be* a definition list. # be* a definition list.
# first, collect single entries # first, collect single entries
entries = [] # type: List[Tuple[List[Tuple[unicode, unicode, int]], ViewList]] entries = [] # type: List[Tuple[List[Tuple[unicode, unicode, int]], StringList]]
in_definition = True in_definition = True
was_empty = True was_empty = True
messages = [] messages = []
@ -316,7 +316,7 @@ class Glossary(SphinxDirective):
messages.append(self.state.reporter.system_message( messages.append(self.state.reporter.system_message(
2, 'glossary term must be preceded by empty line', 2, 'glossary term must be preceded by empty line',
source=source, line=lineno)) source=source, line=lineno))
entries.append(([(line, source, lineno)], ViewList())) entries.append(([(line, source, lineno)], StringList()))
in_definition = False in_definition = False
# second term and following # second term and following
else: else:
@ -346,9 +346,9 @@ class Glossary(SphinxDirective):
# now, parse all the entries into a big definition list # now, parse all the entries into a big definition list
items = [] items = []
for terms, definition in entries: for terms, definition in entries:
termtexts = [] termtexts = [] # type: List[unicode]
termnodes = [] termnodes = [] # type: List[nodes.Node]
system_messages = [] # type: List[unicode] system_messages = [] # type: List[nodes.Node]
for line, source, lineno in terms: for line, source, lineno in terms:
parts = split_term_classifiers(line) parts = split_term_classifiers(line)
# parse the term with inline markup # parse the term with inline markup
@ -385,7 +385,7 @@ class Glossary(SphinxDirective):
def token_xrefs(text): def token_xrefs(text):
# type: (unicode) -> List[nodes.Node] # type: (unicode) -> List[nodes.Node]
retnodes = [] retnodes = [] # type: List[nodes.Node]
pos = 0 pos = 0
for m in token_re.finditer(text): for m in token_re.finditer(text):
if m.start() > pos: if m.start() > pos:
@ -415,7 +415,7 @@ class ProductionList(SphinxDirective):
def run(self): def run(self):
# type: () -> List[nodes.Node] # type: () -> List[nodes.Node]
objects = self.env.domaindata['std']['objects'] objects = self.env.domaindata['std']['objects']
node = addnodes.productionlist() node = addnodes.productionlist() # type: nodes.Node
messages = [] # type: List[nodes.Node] messages = [] # type: List[nodes.Node]
i = 0 i = 0
@ -520,7 +520,7 @@ class StandardDomain(Domain):
nodes.figure: ('figure', None), nodes.figure: ('figure', None),
nodes.table: ('table', None), nodes.table: ('table', None),
nodes.container: ('code-block', None), nodes.container: ('code-block', None),
} # type: Dict[nodes.Node, Tuple[unicode, Callable]] } # type: Dict[Type[nodes.Node], Tuple[unicode, Callable]]
def __init__(self, env): def __init__(self, env):
# type: (BuildEnvironment) -> None # type: (BuildEnvironment) -> None

View File

@ -9,7 +9,7 @@
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
from typing import Callable, Dict, List, Tuple from typing import Callable, Dict, List, Optional, Tuple
from docutils import nodes from docutils import nodes
from docutils.parsers.rst.states import Inliner from docutils.parsers.rst.states import Inliner
@ -17,7 +17,8 @@ from six import text_type
# 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,
Optional[Dict], Optional[List[text_type]]],
Tuple[List[nodes.Node], List[nodes.Node]]] Tuple[List[nodes.Node], List[nodes.Node]]]
# title getter functions for enumerable nodes (see sphinx.domains.std) # title getter functions for enumerable nodes (see sphinx.domains.std)