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

This commit is contained in:
Takeshi KOMIYA
2021-03-23 01:45:04 +09:00
parent 141735f90c
commit 7e6ea15b68
6 changed files with 23 additions and 23 deletions

View File

@@ -72,8 +72,8 @@ class SphinxTransformer(Transformer):
A transformer for Sphinx.
"""
document = None # type: nodes.document
env = None # type: BuildEnvironment
document: nodes.document = None
env: "BuildEnvironment" = None
def set_environment(self, env: "BuildEnvironment") -> None:
self.env = env
@@ -170,7 +170,7 @@ class AutoNumbering(SphinxTransform):
default_priority = 210
def apply(self, **kwargs: Any) -> None:
domain = self.env.get_domain('std') # type: StandardDomain
domain: StandardDomain = self.env.get_domain('std')
for node in self.document.traverse(nodes.Element):
if (domain.is_enumerable_node(node) and

View File

@@ -32,7 +32,7 @@ class RefOnlyListChecker(nodes.GenericNodeVisitor):
pass
def visit_list_item(self, node: nodes.list_item) -> None:
children = [] # type: List[Node]
children: List[Node] = []
for child in node.children:
if not isinstance(child, nodes.Invisible):
children.append(child)

View File

@@ -275,10 +275,10 @@ class Locale(SphinxTransform):
patch = patch.next_node()
# ignore unexpected markups in translation message
unexpected = (
unexpected: Tuple[Type[Element], ...] = (
nodes.paragraph, # expected form of translation
nodes.title # generated by above "Subelements phase2"
) # type: Tuple[Type[Element], ...]
)
# following types are expected if
# config.gettext_additional_targets is configured
@@ -296,8 +296,8 @@ class Locale(SphinxTransform):
lst.append(new)
is_autofootnote_ref = NodeMatcher(nodes.footnote_reference, auto=Any)
old_foot_refs = node.traverse(is_autofootnote_ref) # type: List[nodes.footnote_reference] # NOQA
new_foot_refs = patch.traverse(is_autofootnote_ref) # type: List[nodes.footnote_reference] # NOQA
old_foot_refs: List[nodes.footnote_reference] = node.traverse(is_autofootnote_ref)
new_foot_refs: List[nodes.footnote_reference] = patch.traverse(is_autofootnote_ref)
if len(old_foot_refs) != len(new_foot_refs):
old_foot_ref_rawsources = [ref.rawsource for ref in old_foot_refs]
new_foot_ref_rawsources = [ref.rawsource for ref in new_foot_refs]
@@ -305,7 +305,7 @@ class Locale(SphinxTransform):
' original: {0}, translated: {1}')
.format(old_foot_ref_rawsources, new_foot_ref_rawsources),
location=node)
old_foot_namerefs = {} # type: Dict[str, List[nodes.footnote_reference]]
old_foot_namerefs: Dict[str, List[nodes.footnote_reference]] = {}
for r in old_foot_refs:
old_foot_namerefs.setdefault(r.get('refname'), []).append(r)
for newf in new_foot_refs:
@@ -339,8 +339,8 @@ class Locale(SphinxTransform):
# * use translated refname for section refname.
# * inline reference "`Python <...>`_" has no 'refname'.
is_refnamed_ref = NodeMatcher(nodes.reference, refname=Any)
old_refs = node.traverse(is_refnamed_ref) # type: List[nodes.reference]
new_refs = patch.traverse(is_refnamed_ref) # type: List[nodes.reference]
old_refs: List[nodes.reference] = node.traverse(is_refnamed_ref)
new_refs: List[nodes.reference] = patch.traverse(is_refnamed_ref)
if len(old_refs) != len(new_refs):
old_ref_rawsources = [ref.rawsource for ref in old_refs]
new_ref_rawsources = [ref.rawsource for ref in new_refs]
@@ -368,7 +368,7 @@ class Locale(SphinxTransform):
is_refnamed_footnote_ref = NodeMatcher(nodes.footnote_reference, refname=Any)
old_foot_refs = node.traverse(is_refnamed_footnote_ref)
new_foot_refs = patch.traverse(is_refnamed_footnote_ref)
refname_ids_map = {} # type: Dict[str, List[str]]
refname_ids_map: Dict[str, List[str]] = {}
if len(old_foot_refs) != len(new_foot_refs):
old_foot_ref_rawsources = [ref.rawsource for ref in old_foot_refs]
new_foot_ref_rawsources = [ref.rawsource for ref in new_foot_refs]
@@ -385,8 +385,8 @@ class Locale(SphinxTransform):
# citation should use original 'ids'.
is_citation_ref = NodeMatcher(nodes.citation_reference, refname=Any)
old_cite_refs = node.traverse(is_citation_ref) # type: List[nodes.citation_reference] # NOQA
new_cite_refs = patch.traverse(is_citation_ref) # type: List[nodes.citation_reference] # NOQA
old_cite_refs: List[nodes.citation_reference] = node.traverse(is_citation_ref)
new_cite_refs: List[nodes.citation_reference] = patch.traverse(is_citation_ref)
refname_ids_map = {}
if len(old_cite_refs) != len(new_cite_refs):
old_cite_ref_rawsources = [ref.rawsource for ref in old_cite_refs]
@@ -456,7 +456,7 @@ class Locale(SphinxTransform):
if 'index' in self.config.gettext_additional_targets:
# Extract and translate messages for index entries.
for node, entries in traverse_translatable_index(self.document):
new_entries = [] # type: List[Tuple[str, str, str, str, str]]
new_entries: List[Tuple[str, str, str, str, str]] = []
for type, msg, tid, main, key_ in entries:
msg_parts = split_index_msg(type, msg)
msgstr_parts = []

View File

@@ -38,8 +38,8 @@ class SphinxPostTransform(SphinxTransform):
They do resolving references, convert images, special transformation for each output
formats and so on. This class helps to implement these post transforms.
"""
builders = () # type: Tuple[str, ...]
formats = () # type: Tuple[str, ...]
builders: Tuple[str, ...] = ()
formats: Tuple[str, ...] = ()
def apply(self, **kwargs: Any) -> None:
if self.is_supported():
@@ -104,7 +104,7 @@ class ReferencesResolver(SphinxPostTransform):
newnode = None
if newnode:
newnodes = [newnode] # type: List[Node]
newnodes: List[Node] = [newnode]
else:
newnodes = [contnode]
if newnode is None and isinstance(node[0], addnodes.pending_xref_condition):
@@ -121,7 +121,7 @@ class ReferencesResolver(SphinxPostTransform):
"""Resolve reference generated by the "any" role."""
stddomain = self.env.get_domain('std')
target = node['reftarget']
results = [] # type: List[Tuple[str, Element]]
results: List[Tuple[str, Element]] = []
# first, try resolving as :doc:
doc_ref = stddomain.resolve_xref(self.env, refdoc, self.app.builder,
'doc', target, node, contnode)

View File

@@ -49,7 +49,7 @@ class HighlightLanguageTransform(SphinxTransform):
class HighlightLanguageVisitor(nodes.NodeVisitor):
def __init__(self, document: nodes.document, default_language: str) -> None:
self.default_setting = HighlightSetting(default_language, False, sys.maxsize)
self.settings = [] # type: List[HighlightSetting]
self.settings: List[HighlightSetting] = []
super().__init__(document)
def unknown_visit(self, node: Node) -> None:

View File

@@ -75,7 +75,7 @@ class ImageDownloader(BaseImageConverter):
headers = {}
if os.path.exists(path):
timestamp = ceil(os.stat(path).st_mtime) # type: float
timestamp: float = ceil(os.stat(path).st_mtime)
headers['If-Modified-Since'] = epoch_to_rfc1123(timestamp)
r = requests.get(node['uri'], headers=headers)
@@ -178,7 +178,7 @@ class ImageConverter(BaseImageConverter):
#:
#: .. todo:: This should be refactored not to store the state without class
#: variable.
available = None # type: Optional[bool]
available: Optional[bool] = None
#: A conversion rules the image converter supports.
#: It is represented as a list of pair of source image format (mimetype) and
@@ -189,7 +189,7 @@ class ImageConverter(BaseImageConverter):
#: ('image/gif', 'image/png'),
#: ('application/pdf', 'image/png'),
#: ]
conversion_rules = [] # type: List[Tuple[str, str]]
conversion_rules: List[Tuple[str, str]] = []
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)