Merge pull request #6459 from tk0miya/refactor_type_annotation_directives

Migrate to py3 style type annotation: sphinx.directives
This commit is contained in:
Takeshi KOMIYA 2019-06-09 01:28:03 +09:00 committed by GitHub
commit c80e8cfdd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 119 deletions

View File

@ -9,23 +9,24 @@
""" """
import re import re
from typing import List, cast from typing import Any, Dict, List, Tuple
from typing import cast
from docutils import nodes from docutils import nodes
from docutils.nodes import Node
from docutils.parsers.rst import directives, roles from docutils.parsers.rst import directives, roles
from sphinx import addnodes from sphinx import addnodes
from sphinx.addnodes import desc_signature
from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias
from sphinx.util import docutils from sphinx.util import docutils
from sphinx.util.docfields import DocFieldTransformer, TypedField from sphinx.util.docfields import DocFieldTransformer, Field, TypedField
from sphinx.util.docutils import SphinxDirective from sphinx.util.docutils import SphinxDirective
from sphinx.util.typing import DirectiveOption
if False: if False:
# For type annotation # For type annotation
from typing import Any, Dict, Tuple # NOQA from sphinx.application import Sphinx
from sphinx.application import Sphinx # NOQA
from sphinx.util.docfields import Field # NOQA
from sphinx.util.typing import DirectiveOption # NOQA
# RE to strip backslash escapes # RE to strip backslash escapes
@ -70,8 +71,7 @@ class ObjectDescription(SphinxDirective):
# Warning: this might be removed in future version. Don't touch this from extensions. # Warning: this might be removed in future version. Don't touch this from extensions.
_doc_field_type_map = {} # type: Dict[str, Tuple[Field, bool]] _doc_field_type_map = {} # type: Dict[str, Tuple[Field, bool]]
def get_field_type_map(self): def get_field_type_map(self) -> Dict[str, Tuple[Field, bool]]:
# type: () -> Dict[str, Tuple[Field, bool]]
if self._doc_field_type_map == {}: if self._doc_field_type_map == {}:
for field in self.doc_field_types: for field in self.doc_field_types:
for name in field.names: for name in field.names:
@ -84,8 +84,7 @@ class ObjectDescription(SphinxDirective):
return self._doc_field_type_map return self._doc_field_type_map
def get_signatures(self): def get_signatures(self) -> List[str]:
# type: () -> List[str]
""" """
Retrieve the signatures to document from the directive arguments. By Retrieve the signatures to document from the directive arguments. By
default, signatures are given as arguments, one per line. default, signatures are given as arguments, one per line.
@ -96,8 +95,7 @@ class ObjectDescription(SphinxDirective):
# remove backslashes to support (dummy) escapes; helps Vim highlighting # remove backslashes to support (dummy) escapes; helps Vim highlighting
return [strip_backslash_re.sub(r'\1', line.strip()) for line in lines] return [strip_backslash_re.sub(r'\1', line.strip()) for line in lines]
def handle_signature(self, sig, signode): def handle_signature(self, sig: str, signode: desc_signature) -> Any:
# type: (str, addnodes.desc_signature) -> Any
""" """
Parse the signature *sig* into individual nodes and append them to Parse the signature *sig* into individual nodes and append them to
*signode*. If ValueError is raised, parsing is aborted and the whole *signode*. If ValueError is raised, parsing is aborted and the whole
@ -109,8 +107,7 @@ class ObjectDescription(SphinxDirective):
""" """
raise ValueError raise ValueError
def add_target_and_index(self, name, sig, signode): def add_target_and_index(self, name: Any, sig: str, signode: desc_signature) -> None:
# type: (Any, str, addnodes.desc_signature) -> None
""" """
Add cross-reference IDs and entries to self.indexnode, if applicable. Add cross-reference IDs and entries to self.indexnode, if applicable.
@ -118,24 +115,21 @@ class ObjectDescription(SphinxDirective):
""" """
return # do nothing by default return # do nothing by default
def before_content(self): def before_content(self) -> None:
# type: () -> None
""" """
Called before parsing content. Used to set information about the current Called before parsing content. Used to set information about the current
directive context on the build environment. directive context on the build environment.
""" """
pass pass
def after_content(self): def after_content(self) -> None:
# type: () -> None
""" """
Called after parsing content. Used to reset information about the Called after parsing content. Used to reset information about the
current directive context on the build environment. current directive context on the build environment.
""" """
pass pass
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
""" """
Main directive entry function, called by docutils upon encountering the Main directive entry function, called by docutils upon encountering the
directive. directive.
@ -212,8 +206,7 @@ class DefaultRole(SphinxDirective):
optional_arguments = 1 optional_arguments = 1
final_argument_whitespace = False final_argument_whitespace = False
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
if not self.arguments: if not self.arguments:
docutils.unregister_role('') docutils.unregister_role('')
return [] return []
@ -244,8 +237,7 @@ class DefaultDomain(SphinxDirective):
final_argument_whitespace = False final_argument_whitespace = False
option_spec = {} # type: Dict option_spec = {} # type: Dict
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
domain_name = self.arguments[0].lower() domain_name = self.arguments[0].lower()
# if domain_name not in env.domains: # if domain_name not in env.domains:
# # try searching by label # # try searching by label
@ -294,8 +286,7 @@ deprecated_alias('sphinx.directives',
DescDirective = ObjectDescription DescDirective = ObjectDescription
def setup(app): def setup(app: "Sphinx") -> Dict[str, Any]:
# type: (Sphinx) -> Dict[str, Any]
directives.register_directive('default-role', DefaultRole) directives.register_directive('default-role', DefaultRole)
directives.register_directive('default-domain', DefaultDomain) directives.register_directive('default-domain', DefaultDomain)
directives.register_directive('describe', ObjectDescription) directives.register_directive('describe', ObjectDescription)

View File

@ -9,12 +9,15 @@
import sys import sys
import warnings import warnings
from difflib import unified_diff from difflib import unified_diff
from typing import Any, Dict, List, Tuple
from docutils import nodes from docutils import nodes
from docutils.nodes import Element, Node
from docutils.parsers.rst import directives from docutils.parsers.rst import directives
from docutils.statemachine import StringList from docutils.statemachine import StringList
from sphinx import addnodes from sphinx import addnodes
from sphinx.config import Config
from sphinx.deprecation import RemovedInSphinx40Warning from sphinx.deprecation import RemovedInSphinx40Warning
from sphinx.locale import __ from sphinx.locale import __
from sphinx.util import logging from sphinx.util import logging
@ -23,9 +26,7 @@ from sphinx.util.docutils import SphinxDirective
if False: if False:
# For type annotation # For type annotation
from typing import Any, Dict, List, Tuple # NOQA from sphinx.application import Sphinx
from sphinx.application import Sphinx # NOQA
from sphinx.config import Config # NOQA
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -45,8 +46,7 @@ class Highlight(SphinxDirective):
'linenothreshold': directives.positive_int, 'linenothreshold': directives.positive_int,
} }
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
language = self.arguments[0].strip() language = self.arguments[0].strip()
linenothreshold = self.options.get('linenothreshold', sys.maxsize) linenothreshold = self.options.get('linenothreshold', sys.maxsize)
force = 'force' in self.options force = 'force' in self.options
@ -60,16 +60,14 @@ class Highlight(SphinxDirective):
class HighlightLang(Highlight): class HighlightLang(Highlight):
"""highlightlang directive (deprecated)""" """highlightlang directive (deprecated)"""
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
warnings.warn('highlightlang directive is deprecated. ' warnings.warn('highlightlang directive is deprecated. '
'Please use highlight directive instead.', 'Please use highlight directive instead.',
RemovedInSphinx40Warning, stacklevel=2) RemovedInSphinx40Warning, stacklevel=2)
return super().run() return super().run()
def dedent_lines(lines, dedent, location=None): def dedent_lines(lines: List[str], dedent: int, location: Tuple[str, int] = None) -> List[str]:
# type: (List[str], int, Tuple[str, int]) -> List[str]
if not dedent: if not dedent:
return lines return lines
@ -86,8 +84,7 @@ def dedent_lines(lines, dedent, location=None):
return new_lines return new_lines
def container_wrapper(directive, literal_node, caption): def container_wrapper(directive: SphinxDirective, literal_node: Node, caption: str) -> nodes.container: # NOQA
# type: (SphinxDirective, nodes.Node, str) -> nodes.container
container_node = nodes.container('', literal_block=True, container_node = nodes.container('', literal_block=True,
classes=['literal-block-wrapper']) classes=['literal-block-wrapper'])
parsed = nodes.Element() parsed = nodes.Element()
@ -129,8 +126,7 @@ class CodeBlock(SphinxDirective):
'name': directives.unchanged, 'name': directives.unchanged,
} }
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
document = self.state.document document = self.state.document
code = '\n'.join(self.content) code = '\n'.join(self.content)
location = self.state_machine.get_source_and_line(self.lineno) location = self.state_machine.get_source_and_line(self.lineno)
@ -157,7 +153,7 @@ class CodeBlock(SphinxDirective):
lines = dedent_lines(lines, self.options['dedent'], location=location) lines = dedent_lines(lines, self.options['dedent'], location=location)
code = '\n'.join(lines) code = '\n'.join(lines)
literal = nodes.literal_block(code, code) # type: nodes.Element literal = nodes.literal_block(code, code) # type: Element
if 'linenos' in self.options or 'lineno-start' in self.options: if 'linenos' in self.options or 'lineno-start' in self.options:
literal['linenos'] = True literal['linenos'] = True
literal['classes'] += self.options.get('class', []) literal['classes'] += self.options.get('class', [])
@ -209,8 +205,7 @@ class LiteralIncludeReader:
('diff', 'end-at'), ('diff', 'end-at'),
] ]
def __init__(self, filename, options, config): def __init__(self, filename: str, options: Dict, config: Config) -> None:
# type: (str, Dict, Config) -> None
self.filename = filename self.filename = filename
self.options = options self.options = options
self.encoding = options.get('encoding', config.source_encoding) self.encoding = options.get('encoding', config.source_encoding)
@ -218,15 +213,13 @@ class LiteralIncludeReader:
self.parse_options() self.parse_options()
def parse_options(self): def parse_options(self) -> None:
# type: () -> None
for option1, option2 in self.INVALID_OPTIONS_PAIR: for option1, option2 in self.INVALID_OPTIONS_PAIR:
if option1 in self.options and option2 in self.options: if option1 in self.options and option2 in self.options:
raise ValueError(__('Cannot use both "%s" and "%s" options') % raise ValueError(__('Cannot use both "%s" and "%s" options') %
(option1, option2)) (option1, option2))
def read_file(self, filename, location=None): def read_file(self, filename: str, location: Tuple[str, int] = None) -> List[str]:
# type: (str, Tuple[str, int]) -> List[str]
try: try:
with open(filename, encoding=self.encoding, errors='strict') as f: with open(filename, encoding=self.encoding, errors='strict') as f:
text = f.read() text = f.read()
@ -241,8 +234,7 @@ class LiteralIncludeReader:
'be wrong, try giving an :encoding: option') % 'be wrong, try giving an :encoding: option') %
(self.encoding, filename)) (self.encoding, filename))
def read(self, location=None): def read(self, location: Tuple[str, int] = None) -> Tuple[str, int]:
# type: (Tuple[str, int]) -> Tuple[str, int]
if 'diff' in self.options: if 'diff' in self.options:
lines = self.show_diff() lines = self.show_diff()
else: else:
@ -259,16 +251,14 @@ class LiteralIncludeReader:
return ''.join(lines), len(lines) return ''.join(lines), len(lines)
def show_diff(self, location=None): def show_diff(self, location: Tuple[str, int] = None) -> List[str]:
# type: (Tuple[str, int]) -> List[str]
new_lines = self.read_file(self.filename) new_lines = self.read_file(self.filename)
old_filename = self.options.get('diff') old_filename = self.options.get('diff')
old_lines = self.read_file(old_filename) old_lines = self.read_file(old_filename)
diff = unified_diff(old_lines, new_lines, old_filename, self.filename) diff = unified_diff(old_lines, new_lines, old_filename, self.filename)
return list(diff) return list(diff)
def pyobject_filter(self, lines, location=None): def pyobject_filter(self, lines: List[str], location: Tuple[str, int] = None) -> List[str]:
# type: (List[str], Tuple[str, int]) -> List[str]
pyobject = self.options.get('pyobject') pyobject = self.options.get('pyobject')
if pyobject: if pyobject:
from sphinx.pycode import ModuleAnalyzer from sphinx.pycode import ModuleAnalyzer
@ -286,8 +276,7 @@ class LiteralIncludeReader:
return lines return lines
def lines_filter(self, lines, location=None): def lines_filter(self, lines: List[str], location: Tuple[str, int] = None) -> List[str]:
# type: (List[str], Tuple[str, int]) -> List[str]
linespec = self.options.get('lines') linespec = self.options.get('lines')
if linespec: if linespec:
linelist = parselinenos(linespec, len(lines)) linelist = parselinenos(linespec, len(lines))
@ -311,8 +300,7 @@ class LiteralIncludeReader:
return lines return lines
def start_filter(self, lines, location=None): def start_filter(self, lines: List[str], location: Tuple[str, int] = None) -> List[str]:
# type: (List[str], Tuple[str, int]) -> List[str]
if 'start-at' in self.options: if 'start-at' in self.options:
start = self.options.get('start-at') start = self.options.get('start-at')
inclusive = False inclusive = False
@ -343,8 +331,7 @@ class LiteralIncludeReader:
return lines return lines
def end_filter(self, lines, location=None): def end_filter(self, lines: List[str], location: Tuple[str, int] = None) -> List[str]:
# type: (List[str], Tuple[str, int]) -> List[str]
if 'end-at' in self.options: if 'end-at' in self.options:
end = self.options.get('end-at') end = self.options.get('end-at')
inclusive = True inclusive = True
@ -371,24 +358,21 @@ class LiteralIncludeReader:
return lines return lines
def prepend_filter(self, lines, location=None): def prepend_filter(self, lines: List[str], location: Tuple[str, int] = None) -> List[str]:
# type: (List[str], Tuple[str, int]) -> List[str]
prepend = self.options.get('prepend') prepend = self.options.get('prepend')
if prepend: if prepend:
lines.insert(0, prepend + '\n') lines.insert(0, prepend + '\n')
return lines return lines
def append_filter(self, lines, location=None): def append_filter(self, lines: List[str], location: Tuple[str, int] = None) -> List[str]:
# type: (List[str], Tuple[str, int]) -> List[str]
append = self.options.get('append') append = self.options.get('append')
if append: if append:
lines.append(append + '\n') lines.append(append + '\n')
return lines return lines
def dedent_filter(self, lines, location=None): def dedent_filter(self, lines: List[str], location: Tuple[str, int] = None) -> List[str]:
# type: (List[str], Tuple[str, int]) -> List[str]
if 'dedent' in self.options: if 'dedent' in self.options:
return dedent_lines(lines, self.options.get('dedent'), location=location) return dedent_lines(lines, self.options.get('dedent'), location=location)
else: else:
@ -430,8 +414,7 @@ class LiteralInclude(SphinxDirective):
'diff': directives.unchanged_required, 'diff': directives.unchanged_required,
} }
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
document = self.state.document document = self.state.document
if not document.settings.file_insertion_enabled: if not document.settings.file_insertion_enabled:
return [document.reporter.warning('File insertion disabled', return [document.reporter.warning('File insertion disabled',
@ -449,7 +432,7 @@ class LiteralInclude(SphinxDirective):
reader = LiteralIncludeReader(filename, self.options, self.config) reader = LiteralIncludeReader(filename, self.options, self.config)
text, lines = reader.read(location=location) text, lines = reader.read(location=location)
retnode = nodes.literal_block(text, text, source=filename) # type: nodes.Element retnode = nodes.literal_block(text, text, source=filename) # type: Element
retnode['force'] = 'force' in self.options retnode['force'] = 'force' in self.options
self.set_source_info(retnode) self.set_source_info(retnode)
if self.options.get('diff'): # if diff is set, set udiff if self.options.get('diff'): # if diff is set, set udiff
@ -483,8 +466,7 @@ class LiteralInclude(SphinxDirective):
return [document.reporter.warning(exc, line=self.lineno)] return [document.reporter.warning(exc, line=self.lineno)]
def setup(app): def setup(app: "Sphinx") -> Dict[str, Any]:
# type: (Sphinx) -> Dict[str, Any]
directives.register_directive('highlight', Highlight) directives.register_directive('highlight', Highlight)
directives.register_directive('highlightlang', HighlightLang) directives.register_directive('highlightlang', HighlightLang)
directives.register_directive('code-block', CodeBlock) directives.register_directive('code-block', CodeBlock)

View File

@ -7,9 +7,11 @@
""" """
import re import re
from typing import Any, Dict, List
from typing import cast from typing import cast
from docutils import nodes from docutils import nodes
from docutils.nodes import Element, Node
from docutils.parsers.rst import directives from docutils.parsers.rst import directives
from docutils.parsers.rst.directives.admonitions import BaseAdmonition from docutils.parsers.rst.directives.admonitions import BaseAdmonition
from docutils.parsers.rst.directives.misc import Class from docutils.parsers.rst.directives.misc import Class
@ -25,15 +27,13 @@ from sphinx.util.nodes import explicit_title_re, process_index_entry
if False: if False:
# For type annotation # For type annotation
from typing import Any, Dict, List # NOQA from sphinx.application import Sphinx
from sphinx.application import Sphinx # NOQA
glob_re = re.compile(r'.*[*?\[].*') glob_re = re.compile(r'.*[*?\[].*')
def int_or_nothing(argument): def int_or_nothing(argument: str) -> int:
# type: (str) -> int
if not argument: if not argument:
return 999 return 999
return int(argument) return int(argument)
@ -60,8 +60,7 @@ class TocTree(SphinxDirective):
'reversed': directives.flag, 'reversed': directives.flag,
} }
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
subnode = addnodes.toctree() subnode = addnodes.toctree()
subnode['parent'] = self.env.docname subnode['parent'] = self.env.docname
@ -160,11 +159,10 @@ class Author(SphinxDirective):
final_argument_whitespace = True final_argument_whitespace = True
option_spec = {} # type: Dict option_spec = {} # type: Dict
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
if not self.config.show_authors: if not self.config.show_authors:
return [] return []
para = nodes.paragraph(translatable=False) # type: nodes.Element para = nodes.paragraph(translatable=False) # type: Element
emph = nodes.emphasis() emph = nodes.emphasis()
para += emph para += emph
if self.name == 'sectionauthor': if self.name == 'sectionauthor':
@ -179,7 +177,7 @@ class Author(SphinxDirective):
inodes, messages = self.state.inline_text(self.arguments[0], self.lineno) inodes, messages = self.state.inline_text(self.arguments[0], self.lineno)
emph.extend(inodes) emph.extend(inodes)
ret = [para] # type: List[nodes.Node] ret = [para] # type: List[Node]
ret += messages ret += messages
return ret return ret
@ -194,8 +192,7 @@ class Index(SphinxDirective):
final_argument_whitespace = True final_argument_whitespace = True
option_spec = {} # type: Dict option_spec = {} # type: Dict
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
arguments = self.arguments[0].split('\n') arguments = self.arguments[0].split('\n')
targetid = 'index-%s' % self.env.new_serialno('index') targetid = 'index-%s' % self.env.new_serialno('index')
targetnode = nodes.target('', '', ids=[targetid]) targetnode = nodes.target('', '', ids=[targetid])
@ -226,8 +223,7 @@ class TabularColumns(SphinxDirective):
final_argument_whitespace = True final_argument_whitespace = True
option_spec = {} # type: Dict option_spec = {} # type: Dict
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
node = addnodes.tabular_col_spec() node = addnodes.tabular_col_spec()
node['spec'] = self.arguments[0] node['spec'] = self.arguments[0]
self.set_source_info(node) self.set_source_info(node)
@ -244,15 +240,14 @@ class Centered(SphinxDirective):
final_argument_whitespace = True final_argument_whitespace = True
option_spec = {} # type: Dict option_spec = {} # type: Dict
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
if not self.arguments: if not self.arguments:
return [] return []
subnode = addnodes.centered() # type: nodes.Element subnode = addnodes.centered() # type: Element
inodes, messages = self.state.inline_text(self.arguments[0], self.lineno) inodes, messages = self.state.inline_text(self.arguments[0], self.lineno)
subnode.extend(inodes) subnode.extend(inodes)
ret = [subnode] # type: List[nodes.Node] ret = [subnode] # type: List[Node]
ret += messages ret += messages
return ret return ret
@ -267,8 +262,7 @@ class Acks(SphinxDirective):
final_argument_whitespace = False final_argument_whitespace = False
option_spec = {} # type: Dict option_spec = {} # type: Dict
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
node = addnodes.acks() node = addnodes.acks()
node.document = self.state.document node.document = self.state.document
self.state.nested_parse(self.content, self.content_offset, node) self.state.nested_parse(self.content, self.content_offset, node)
@ -291,8 +285,7 @@ class HList(SphinxDirective):
'columns': int, 'columns': int,
} }
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
ncolumns = self.options.get('columns', 2) ncolumns = self.options.get('columns', 2)
node = nodes.paragraph() node = nodes.paragraph()
node.document = self.state.document node.document = self.state.document
@ -325,8 +318,7 @@ class Only(SphinxDirective):
final_argument_whitespace = True final_argument_whitespace = True
option_spec = {} # type: Dict option_spec = {} # type: Dict
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
node = addnodes.only() node = addnodes.only()
node.document = self.state.document node.document = self.state.document
self.set_source_info(node) self.set_source_info(node)
@ -380,8 +372,7 @@ class Include(BaseInclude, SphinxDirective):
"correctly", i.e. relative to source directory. "correctly", i.e. relative to source directory.
""" """
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
if self.arguments[0].startswith('<') and \ if self.arguments[0].startswith('<') and \
self.arguments[0].endswith('>'): self.arguments[0].endswith('>'):
# docutils "standard" includes, do not do path processing # docutils "standard" includes, do not do path processing
@ -392,8 +383,7 @@ class Include(BaseInclude, SphinxDirective):
return super().run() return super().run()
def setup(app): def setup(app: "Sphinx") -> Dict[str, Any]:
# type: (Sphinx) -> Dict[str, Any]
directives.register_directive('toctree', TocTree) directives.register_directive('toctree', TocTree)
directives.register_directive('sectionauthor', Author) directives.register_directive('sectionauthor', Author)
directives.register_directive('moduleauthor', Author) directives.register_directive('moduleauthor', Author)

View File

@ -6,10 +6,11 @@
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
from typing import Any, Dict, List, Tuple
from typing import cast from typing import cast
from docutils import nodes from docutils import nodes
from docutils.nodes import make_id from docutils.nodes import Node, make_id, system_message
from docutils.parsers.rst import directives from docutils.parsers.rst import directives
from docutils.parsers.rst.directives import images, html, tables from docutils.parsers.rst.directives import images, html, tables
@ -20,8 +21,7 @@ from sphinx.util.nodes import set_source_info
if False: if False:
# For type annotation # For type annotation
from typing import Dict, List, Tuple # NOQA from sphinx.application import Sphinx
from sphinx.application import Sphinx # NOQA
class Figure(images.Figure): class Figure(images.Figure):
@ -29,8 +29,7 @@ class Figure(images.Figure):
instead of the image node. instead of the image node.
""" """
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
name = self.options.pop('name', None) name = self.options.pop('name', None)
result = super().run() result = super().run()
if len(result) == 2 or isinstance(result[0], nodes.system_message): if len(result) == 2 or isinstance(result[0], nodes.system_message):
@ -52,8 +51,7 @@ class Figure(images.Figure):
class Meta(html.Meta, SphinxDirective): class Meta(html.Meta, SphinxDirective):
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
result = super().run() result = super().run()
for node in result: for node in result:
if (isinstance(node, nodes.pending) and if (isinstance(node, nodes.pending) and
@ -74,8 +72,7 @@ class RSTTable(tables.RSTTable):
Only for docutils-0.13 or older version.""" Only for docutils-0.13 or older version."""
def make_title(self): def make_title(self) -> Tuple[nodes.title, List[system_message]]:
# type: () -> Tuple[nodes.title, List[nodes.system_message]]
title, message = super().make_title() title, message = super().make_title()
if title: if title:
set_source_info(self, title) set_source_info(self, title)
@ -88,8 +85,7 @@ class CSVTable(tables.CSVTable):
Only for docutils-0.13 or older version.""" Only for docutils-0.13 or older version."""
def make_title(self): def make_title(self) -> Tuple[nodes.title, List[system_message]]:
# type: () -> Tuple[nodes.title, List[nodes.system_message]]
title, message = super().make_title() title, message = super().make_title()
if title: if title:
set_source_info(self, title) set_source_info(self, title)
@ -102,8 +98,7 @@ class ListTable(tables.ListTable):
Only for docutils-0.13 or older version.""" Only for docutils-0.13 or older version."""
def make_title(self): def make_title(self) -> Tuple[nodes.title, List[system_message]]:
# type: () -> Tuple[nodes.title, List[nodes.system_message]]
title, message = super().make_title() title, message = super().make_title()
if title: if title:
set_source_info(self, title) set_source_info(self, title)
@ -125,8 +120,7 @@ class Code(SphinxDirective):
} }
has_content = True has_content = True
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
self.assert_has_content() self.assert_has_content()
code = '\n'.join(self.content) code = '\n'.join(self.content)
@ -169,8 +163,7 @@ class MathDirective(SphinxDirective):
'nowrap': directives.flag, 'nowrap': directives.flag,
} }
def run(self): def run(self) -> List[Node]:
# type: () -> List[nodes.Node]
latex = '\n'.join(self.content) latex = '\n'.join(self.content)
if self.arguments and self.arguments[0]: if self.arguments and self.arguments[0]:
latex = self.arguments[0] + '\n\n' + latex latex = self.arguments[0] + '\n\n' + latex
@ -184,12 +177,11 @@ class MathDirective(SphinxDirective):
self.add_name(node) self.add_name(node)
self.set_source_info(node) self.set_source_info(node)
ret = [node] # type: List[nodes.Node] ret = [node] # type: List[Node]
self.add_target(ret) self.add_target(ret)
return ret return ret
def add_target(self, ret): def add_target(self, ret: List[Node]) -> None:
# type: (List[nodes.Node]) -> None
node = cast(nodes.math_block, ret[0]) node = cast(nodes.math_block, ret[0])
# assign label automatically if math_number_all enabled # assign label automatically if math_number_all enabled
@ -216,8 +208,7 @@ class MathDirective(SphinxDirective):
self.state_machine.reporter.warning(exc, line=self.lineno) self.state_machine.reporter.warning(exc, line=self.lineno)
def setup(app): def setup(app: "Sphinx") -> Dict[str, Any]:
# type: (Sphinx) -> Dict
directives.register_directive('figure', Figure) directives.register_directive('figure', Figure)
directives.register_directive('meta', Meta) directives.register_directive('meta', Meta)
directives.register_directive('table', RSTTable) directives.register_directive('table', RSTTable)