summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2019-06-04 01:00:45 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2019-06-05 01:29:54 +0900
commit073b92d45a59eb5065d304fbaf0fc2d2ff88c5e8 (patch)
tree729994f40c055378123fdfc9bc42daf026ee700b
parenta3c9edfbe6d50b486ea28876e47b7d6e27379916 (diff)
downloadsphinx-git-073b92d45a59eb5065d304fbaf0fc2d2ff88c5e8.tar.gz
Migrate to py3 style type annotation: sphinx.util.rst
-rw-r--r--sphinx/util/rst.py30
1 files changed, 10 insertions, 20 deletions
diff --git a/sphinx/util/rst.py b/sphinx/util/rst.py
index c897b075a..0824e413f 100644
--- a/sphinx/util/rst.py
+++ b/sphinx/util/rst.py
@@ -11,23 +11,20 @@
import re
from collections import defaultdict
from contextlib import contextmanager
+from typing import Dict, Generator
from unicodedata import east_asian_width
from docutils.parsers.rst import roles
from docutils.parsers.rst.languages import en as english
+from docutils.statemachine import StringList
from docutils.utils import Reporter
+from jinja2 import Environment
from jinja2 import environmentfilter
from sphinx.locale import __
from sphinx.util import docutils
from sphinx.util import logging
-if False:
- # For type annotation
- from typing import Callable, Dict, Generator # NOQA
- from docutils.statemachine import StringList # NOQA
- from jinja2 import Environment # NOQA
-
logger = logging.getLogger(__name__)
docinfo_re = re.compile(':\\w+:.*?')
@@ -40,18 +37,15 @@ WIDECHARS = defaultdict(lambda: "WF") # type: Dict[str, str]
WIDECHARS["ja"] = "WFA" # In Japanese, Ambiguous characters also have double width
-def escape(text):
- # type: (str) -> str
+def escape(text: str) -> str:
text = symbols_re.sub(r'\\\1', text)
text = re.sub(r'^\.', r'\.', text) # escape a dot at top
return text
-def textwidth(text, widechars='WF'):
- # type: (str, str) -> int
+def textwidth(text: str, widechars: str = 'WF') -> int:
"""Get width of text."""
- def charwidth(char, widechars):
- # type: (str, str) -> int
+ def charwidth(char: str, widechars: str) -> int:
if east_asian_width(char) in widechars:
return 2
else:
@@ -61,8 +55,7 @@ def textwidth(text, widechars='WF'):
@environmentfilter
-def heading(env, text, level=1):
- # type: (Environment, str, int) -> str
+def heading(env: Environment, text: str, level: int = 1) -> str:
"""Create a heading for *level*."""
assert level <= 3
width = textwidth(text, WIDECHARS[env.language]) # type: ignore
@@ -71,8 +64,7 @@ def heading(env, text, level=1):
@contextmanager
-def default_role(docname, name):
- # type: (str, str) -> Generator
+def default_role(docname: str, name: str) -> Generator[None, None, None]:
if name:
dummy_reporter = Reporter('', 4, 4)
role_fn, _ = roles.role(name, english, 0, dummy_reporter)
@@ -86,8 +78,7 @@ def default_role(docname, name):
docutils.unregister_role('')
-def prepend_prolog(content, prolog):
- # type: (StringList, str) -> None
+def prepend_prolog(content: StringList, prolog: str) -> None:
"""Prepend a string to content body as prolog."""
if prolog:
pos = 0
@@ -109,8 +100,7 @@ def prepend_prolog(content, prolog):
content.insert(pos + lineno + 1, '', '<generated>', 0)
-def append_epilog(content, epilog):
- # type: (StringList, str) -> None
+def append_epilog(content: StringList, epilog: str) -> None:
"""Append a string to content body as epilog."""
if epilog:
content.append('', '<generated>', 0)