diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2019-08-21 00:48:21 +0900 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2019-08-21 01:25:45 +0900 |
commit | e505b812a243f79885a276589ee66437c41c6698 (patch) | |
tree | 0b16e11d3d40c625268b0addf846f2471602add2 /sphinx/transforms/post_transforms/code.py | |
parent | f078d8264704513ca981d812b1ea3da415502300 (diff) | |
download | sphinx-git-e505b812a243f79885a276589ee66437c41c6698.tar.gz |
Migrate to py3 style type annotation: sphinx.transforms.post_transforms.code
Diffstat (limited to 'sphinx/transforms/post_transforms/code.py')
-rw-r--r-- | sphinx/transforms/post_transforms/code.py | 51 |
1 files changed, 17 insertions, 34 deletions
diff --git a/sphinx/transforms/post_transforms/code.py b/sphinx/transforms/post_transforms/code.py index bae1216bd..0707b85a8 100644 --- a/sphinx/transforms/post_transforms/code.py +++ b/sphinx/transforms/post_transforms/code.py @@ -9,20 +9,17 @@ """ import sys -from typing import NamedTuple, Union +from typing import Any, Dict, List, NamedTuple, Union from docutils import nodes +from docutils.nodes import Node from pygments.lexers import PythonConsoleLexer, guess_lexer from sphinx import addnodes +from sphinx.application import Sphinx from sphinx.ext import doctest from sphinx.transforms import SphinxTransform -if False: - # For type annotation - from typing import Any, Dict, List # NOQA - from sphinx.application import Sphinx # NOQA - HighlightSetting = NamedTuple('HighlightSetting', [('language', str), ('force', bool), @@ -39,8 +36,7 @@ class HighlightLanguageTransform(SphinxTransform): """ default_priority = 400 - def apply(self, **kwargs): - # type: (Any) -> None + def apply(self, **kwargs) -> None: visitor = HighlightLanguageVisitor(self.document, self.config.highlight_language) self.document.walkabout(visitor) @@ -50,44 +46,35 @@ class HighlightLanguageTransform(SphinxTransform): class HighlightLanguageVisitor(nodes.NodeVisitor): - def __init__(self, document, default_language): - # type: (nodes.document, str) -> None + def __init__(self, document: nodes.document, default_language: str) -> None: self.default_setting = HighlightSetting(default_language, False, sys.maxsize) self.settings = [] # type: List[HighlightSetting] super().__init__(document) - def unknown_visit(self, node): - # type: (nodes.Node) -> None + def unknown_visit(self, node: Node) -> None: pass - def unknown_departure(self, node): - # type: (nodes.Node) -> None + def unknown_departure(self, node: Node) -> None: pass - def visit_document(self, node): - # type: (nodes.Node) -> None + def visit_document(self, node: Node) -> None: self.settings.append(self.default_setting) - def depart_document(self, node): - # type: (nodes.Node) -> None + def depart_document(self, node: Node) -> None: self.settings.pop() - def visit_start_of_file(self, node): - # type: (nodes.Node) -> None + def visit_start_of_file(self, node: Node) -> None: self.settings.append(self.default_setting) - def depart_start_of_file(self, node): - # type: (nodes.Node) -> None + def depart_start_of_file(self, node: Node) -> None: self.settings.pop() - def visit_highlightlang(self, node): - # type: (addnodes.highlightlang) -> None + def visit_highlightlang(self, node: addnodes.highlightlang) -> None: self.settings[-1] = HighlightSetting(node['lang'], node['force'], node['linenothreshold']) - def visit_literal_block(self, node): - # type: (nodes.literal_block) -> None + def visit_literal_block(self, node: nodes.literal_block) -> None: setting = self.settings[-1] if 'language' not in node: node['language'] = setting.language @@ -105,8 +92,7 @@ class TrimDoctestFlagsTransform(SphinxTransform): """ default_priority = HighlightLanguageTransform.default_priority + 1 - def apply(self, **kwargs): - # type: (Any) -> None + def apply(self, **kwargs) -> None: if not self.config.trim_doctest_flags: return @@ -118,8 +104,7 @@ class TrimDoctestFlagsTransform(SphinxTransform): self.strip_doctest_flags(dbnode) @staticmethod - def strip_doctest_flags(node): - # type: (Union[nodes.literal_block, nodes.doctest_block]) -> None + def strip_doctest_flags(node: Union[nodes.literal_block, nodes.doctest_block]) -> None: source = node.rawsource source = doctest.blankline_re.sub('', source) source = doctest.doctestopt_re.sub('', source) @@ -127,8 +112,7 @@ class TrimDoctestFlagsTransform(SphinxTransform): node[:] = [nodes.Text(source)] @staticmethod - def is_pyconsole(node): - # type: (nodes.literal_block) -> bool + def is_pyconsole(node: nodes.literal_block) -> bool: if node.rawsource != node.astext(): return False # skip parsed-literal node @@ -147,8 +131,7 @@ class TrimDoctestFlagsTransform(SphinxTransform): return False -def setup(app): - # type: (Sphinx) -> Dict[str, Any] +def setup(app: Sphinx) -> Dict[str, Any]: app.add_post_transform(HighlightLanguageTransform) app.add_post_transform(TrimDoctestFlagsTransform) |