diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2017-02-10 15:53:42 +0900 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2017-02-10 17:01:25 +0900 |
commit | da6f4c00199ac39990bffaf40e00057996d94455 (patch) | |
tree | 88a497acaa17fef45e570d2d3e65d427bd186437 /sphinx/quickstart.py | |
parent | 82e011cae615ea9bb716ff0a1c2f1482e557f275 (diff) | |
download | sphinx-git-da6f4c00199ac39990bffaf40e00057996d94455.tar.gz |
Add mypy annotations
Diffstat (limited to 'sphinx/quickstart.py')
-rw-r--r-- | sphinx/quickstart.py | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/sphinx/quickstart.py b/sphinx/quickstart.py index 1d128b878..c599f079a 100644 --- a/sphinx/quickstart.py +++ b/sphinx/quickstart.py @@ -42,6 +42,10 @@ from sphinx.util.console import ( # type: ignore from sphinx.util.template import SphinxRenderer from sphinx.util import texescape +if False: + # For type annotation + from typing import Any, Callable, Pattern # NOQA + TERM_ENCODING = getattr(sys.stdin, 'encoding', None) DEFAULT_VALUE = { @@ -66,6 +70,7 @@ PROMPT_PREFIX = '> ' def mkdir_p(dir): + # type: (unicode) -> None if path.isdir(dir): return os.makedirs(dir) @@ -73,6 +78,7 @@ def mkdir_p(dir): # function to get input from terminal -- overridden by the test suite def term_input(prompt): + # type: (unicode) -> unicode print(prompt, end='') return input('') @@ -82,6 +88,7 @@ class ValidationError(Exception): def is_path(x): + # type: (unicode) -> unicode x = path.expanduser(x) if path.exists(x) and not path.isdir(x): raise ValidationError("Please enter a valid path name.") @@ -89,30 +96,36 @@ def is_path(x): def allow_empty(x): + # type: (unicode) -> unicode return x def nonempty(x): + # type: (unicode) -> unicode if not x: raise ValidationError("Please enter some text.") return x def choice(*l): + # type: (List[unicode]) -> Callable[[unicode], unicode] def val(x): + # type: (unicode) -> unicode if x not in l: - raise ValidationError('Please enter one of %s.' % ', '.join(l)) + raise ValidationError('Please enter one of %s.' % ', '.join(l)) # type: ignore return x return val def boolean(x): + # type: (unicode) -> bool if x.upper() not in ('Y', 'YES', 'N', 'NO'): raise ValidationError("Please enter either 'y' or 'n'.") return x.upper() in ('Y', 'YES') def suffix(x): + # type: (unicode) -> unicode if not (x[0:1] == '.' and len(x) > 1): raise ValidationError("Please enter a file suffix, " "e.g. '.rst' or '.txt'.") @@ -120,10 +133,12 @@ def suffix(x): def ok(x): + # type: (unicode) -> unicode return x def term_decode(text): + # type: (unicode) -> unicode if isinstance(text, text_type): return text @@ -145,9 +160,10 @@ def term_decode(text): def do_prompt(d, key, text, default=None, validator=nonempty): + # type: (Dict, unicode, unicode, unicode, Callable[[unicode], Any]) -> None while True: if default is not None: - prompt = PROMPT_PREFIX + '%s [%s]: ' % (text, default) + prompt = PROMPT_PREFIX + '%s [%s]: ' % (text, default) # type: unicode else: prompt = PROMPT_PREFIX + text + ': ' if PY2: @@ -179,6 +195,7 @@ def do_prompt(d, key, text, default=None, validator=nonempty): def convert_python_source(source, rex=re.compile(r"[uU]('.*?')")): + # type: (unicode, Pattern) -> unicode # remove Unicode literal prefixes if PY3: return rex.sub('\\1', source) @@ -188,10 +205,12 @@ def convert_python_source(source, rex=re.compile(r"[uU]('.*?')")): class QuickstartRenderer(SphinxRenderer): def __init__(self, templatedir): + # type: (unicode) -> None self.templatedir = templatedir or '' super(QuickstartRenderer, self).__init__() def render(self, template_name, context): + # type: (unicode, Dict) -> unicode user_template = path.join(self.templatedir, path.basename(template_name)) if self.templatedir and path.exists(user_template): return self.render_from_file(user_template, context) @@ -200,6 +219,7 @@ class QuickstartRenderer(SphinxRenderer): def ask_user(d): + # type: (Dict) -> None """Ask the user for quickstart values missing from *d*. Values are: @@ -375,6 +395,7 @@ directly.''') def generate(d, overwrite=True, silent=False, templatedir=None): + # type: (Dict, bool, bool, unicode) -> None """Generate project based on values in *d*.""" template = QuickstartRenderer(templatedir=templatedir) @@ -432,6 +453,7 @@ def generate(d, overwrite=True, silent=False, templatedir=None): mkdir_p(path.join(srcdir, d['dot'] + 'static')) def write_file(fpath, content, newline=None): + # type: (unicode, unicode, unicode) -> None if overwrite or not path.isfile(fpath): print('Creating file %s.' % fpath) with open(fpath, 'wt', encoding='utf-8', newline=newline) as f: @@ -488,6 +510,7 @@ where "builder" is one of the supported builders, e.g. html, latex or linkcheck. def usage(argv, msg=None): + # type: (List[unicode], unicode) -> None if msg: print(msg, file=sys.stderr) print(file=sys.stderr) @@ -504,6 +527,7 @@ For more information, visit <http://sphinx-doc.org/>. def valid_dir(d): + # type: (Dict) -> bool dir = d['path'] if not path.exists(dir): return True @@ -534,6 +558,7 @@ def valid_dir(d): class MyFormatter(optparse.IndentedHelpFormatter): def format_usage(self, usage): + # type: (str) -> str return usage def format_help(self, formatter): @@ -546,6 +571,7 @@ class MyFormatter(optparse.IndentedHelpFormatter): def main(argv=sys.argv): + # type: (List[str]) -> int if not color_terminal(): nocolor() |