refactor: Use PEP-526 based variable annotation (sphinx.directives)

This commit is contained in:
Takeshi KOMIYA
2021-03-23 01:44:24 +09:00
parent 09a037006c
commit 555a52be82
4 changed files with 15 additions and 15 deletions

View File

@@ -63,13 +63,13 @@ class ObjectDescription(SphinxDirective, Generic[T]):
}
# types of doc fields that this directive handles, see sphinx.util.docfields
doc_field_types = [] # type: List[Field]
domain = None # type: str
objtype = None # type: str
indexnode = None # type: addnodes.index
doc_field_types: List[Field] = []
domain: str = None
objtype: str = None
indexnode: addnodes.index = None
# 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: Dict[str, Tuple[Field, bool]] = {}
def get_field_type_map(self) -> Dict[str, Tuple[Field, bool]]:
if self._doc_field_type_map == {}:
@@ -173,7 +173,7 @@ class ObjectDescription(SphinxDirective, Generic[T]):
if self.domain:
node['classes'].append(self.domain)
self.names = [] # type: List[T]
self.names: List[T] = []
signatures = self.get_signatures()
for i, sig in enumerate(signatures):
# add a signature node for each signature in the current unit

View File

@@ -142,7 +142,7 @@ class CodeBlock(SphinxDirective):
lines = dedent_lines(lines, self.options['dedent'], location=location)
code = '\n'.join(lines)
literal = nodes.literal_block(code, code) # type: Element
literal: Element = nodes.literal_block(code, code)
if 'linenos' in self.options or 'lineno-start' in self.options:
literal['linenos'] = True
literal['classes'] += self.options.get('class', [])
@@ -422,7 +422,7 @@ class LiteralInclude(SphinxDirective):
reader = LiteralIncludeReader(filename, self.options, self.config)
text, lines = reader.read(location=location)
retnode = nodes.literal_block(text, text, source=filename) # type: Element
retnode: Element = nodes.literal_block(text, text, source=filename)
retnode['force'] = 'force' in self.options
self.set_source_info(retnode)
if self.options.get('diff'): # if diff is set, set udiff

View File

@@ -90,7 +90,7 @@ class TocTree(SphinxDirective):
all_docnames = self.env.found_docs.copy()
all_docnames.remove(self.env.docname) # remove current document
ret = [] # type: List[Node]
ret: List[Node] = []
excluded = Matcher(self.config.exclude_patterns)
for entry in self.content:
if not entry:
@@ -168,7 +168,7 @@ class Author(SphinxDirective):
def run(self) -> List[Node]:
if not self.config.show_authors:
return []
para = nodes.paragraph(translatable=False) # type: Element
para: Element = nodes.paragraph(translatable=False)
emph = nodes.emphasis()
para += emph
if self.name == 'sectionauthor':
@@ -183,7 +183,7 @@ class Author(SphinxDirective):
inodes, messages = self.state.inline_text(self.arguments[0], self.lineno)
emph.extend(inodes)
ret = [para] # type: List[Node]
ret: List[Node] = [para]
ret += messages
return ret
@@ -225,11 +225,11 @@ class Centered(SphinxDirective):
def run(self) -> List[Node]:
if not self.arguments:
return []
subnode = addnodes.centered() # type: Element
subnode: Element = addnodes.centered()
inodes, messages = self.state.inline_text(self.arguments[0], self.lineno)
subnode.extend(inodes)
ret = [subnode] # type: List[Node]
ret: List[Node] = [subnode]
ret += messages
return ret
@@ -309,7 +309,7 @@ class Only(SphinxDirective):
# Same as util.nested_parse_with_titles but try to handle nested
# sections which should be raised higher up the doctree.
memo = self.state.memo # type: Any
memo: Any = self.state.memo
surrounding_title_styles = memo.title_styles
surrounding_section_level = memo.section_level
memo.title_styles = []

View File

@@ -206,7 +206,7 @@ class MathDirective(SphinxDirective):
self.add_name(node)
self.set_source_info(node)
ret = [node] # type: List[Node]
ret: List[Node] = [node]
self.add_target(ret)
return ret