Update type annotations

This commit is contained in:
Takeshi KOMIYA
2021-02-07 19:29:24 +09:00
parent 84458da828
commit d25c3ad241
9 changed files with 24 additions and 17 deletions

View File

@@ -130,6 +130,9 @@ class Sphinx:
:ivar outdir: Directory for storing build documents.
"""
warningiserror: bool
_warncount: int
def __init__(self, srcdir: str, confdir: Optional[str], outdir: str, doctreedir: str,
buildername: str, confoverrides: Dict = None,
status: IO = sys.stdout, warning: IO = sys.stderr,

View File

@@ -30,7 +30,7 @@ RemovedInNextVersionWarning = RemovedInSphinx50Warning
def deprecated_alias(modname: str, objects: Dict[str, object],
warning: "Type[Warning]", names: Dict[str, str] = None) -> None:
warning: "Type[Warning]", names: Dict[str, str] = {}) -> None:
module = import_module(modname)
sys.modules[modname] = _ModuleWrapper( # type: ignore
module, modname, objects, warning, names)

View File

@@ -106,7 +106,7 @@ class _TranslationProxy(UserString):
translators = defaultdict(NullTranslations) # type: Dict[Tuple[str, str], NullTranslations]
def init(locale_dirs: List[Optional[str]], language: str,
def init(locale_dirs: List[Optional[str]], language: Optional[str],
catalog: str = 'sphinx', namespace: str = 'general') -> Tuple[NullTranslations, bool]:
"""Look for message catalogs in `locale_dirs` and *ensure* that there is at
least a NullTranslations catalog set in `translators`. If called multiple
@@ -123,9 +123,11 @@ def init(locale_dirs: List[Optional[str]], language: str,
if language and '_' in language:
# for language having country code (like "de_AT")
languages = [language, language.split('_')[0]]
else:
languages = [language, language.split('_')[0]] # type: Optional[List[str]]
elif language:
languages = [language]
else:
languages = None
# loading
for dir_ in locale_dirs:

View File

@@ -108,7 +108,7 @@ class _UnparseVisitor(ast.NodeVisitor):
return name
def visit_arguments(self, node: ast.arguments) -> str:
defaults = list(node.defaults)
defaults = list(node.defaults) # type: List[Optional[ast.expr]]
positionals = len(node.args)
posonlyargs = 0
if hasattr(node, "posonlyargs"): # for py38+
@@ -117,7 +117,7 @@ class _UnparseVisitor(ast.NodeVisitor):
for _ in range(len(defaults), positionals):
defaults.insert(0, None)
kw_defaults = list(node.kw_defaults)
kw_defaults = list(node.kw_defaults) # type: List[Optional[ast.expr]]
for _ in range(len(kw_defaults), len(node.kwonlyargs)):
kw_defaults.insert(0, None)

View File

@@ -12,7 +12,7 @@ import os
import re
from datetime import datetime, timezone
from os import path
from typing import TYPE_CHECKING, Callable, Generator, List, NamedTuple, Tuple, Union
from typing import TYPE_CHECKING, Callable, Generator, List, NamedTuple, Optional, Tuple, Union
import babel.dates
from babel.messages.mofile import write_mo
@@ -170,7 +170,7 @@ date_format_mappings = {
date_format_re = re.compile('(%s)' % '|'.join(date_format_mappings))
def babel_format_date(date: datetime, format: str, locale: str,
def babel_format_date(date: datetime, format: str, locale: Optional[str],
formatter: Callable = babel.dates.format_date) -> str:
if locale is None:
locale = 'en'
@@ -191,7 +191,7 @@ def babel_format_date(date: datetime, format: str, locale: str,
return format
def format_date(format: str, date: datetime = None, language: str = None) -> str:
def format_date(format: str, date: datetime = None, language: Optional[str] = None) -> str:
if date is None:
# If time is not specified, try to use $SOURCE_DATE_EPOCH variable
# See https://wiki.debian.org/ReproducibleBuilds/TimestampsProposal

View File

@@ -12,7 +12,7 @@ import base64
import imghdr
from collections import OrderedDict
from os import path
from typing import IO, NamedTuple, Optional, Tuple
from typing import IO, BinaryIO, NamedTuple, Optional, Tuple
import imagesize
@@ -103,7 +103,7 @@ def parse_data_uri(uri: str) -> Optional[DataURI]:
return DataURI(mimetype, charset, image_data)
def test_svg(h: bytes, f: IO) -> Optional[str]:
def test_svg(h: bytes, f: Optional[BinaryIO]) -> Optional[str]:
"""An additional imghdr library helper; test the header is SVG's or not."""
try:
if '<svg' in h.decode().lower():

View File

@@ -12,7 +12,7 @@ import logging
import logging.handlers
from collections import defaultdict
from contextlib import contextmanager
from typing import IO, TYPE_CHECKING, Any, Dict, Generator, List, Tuple, Type, Union
from typing import IO, TYPE_CHECKING, Any, Dict, Generator, List, Optional, Tuple, Type, Union
from docutils import nodes
from docutils.nodes import Node
@@ -353,6 +353,8 @@ def is_suppressed_warning(type: str, subtype: str, suppress_warnings: List[str])
if type is None:
return False
subtarget: Optional[str]
for warning_type in suppress_warnings:
if '.' in warning_type:
target, subtarget = warning_type.split('.', 1)
@@ -506,7 +508,7 @@ class WarningLogRecordTranslator(SphinxLogRecordTranslator):
LogRecordClass = SphinxWarningLogRecord
def get_node_location(node: Node) -> str:
def get_node_location(node: Node) -> Optional[str]:
(source, line) = get_source_line(node)
if source and line:
return "%s:%s" % (source, line)

View File

@@ -9,7 +9,7 @@
"""
import re
from typing import Callable, Dict, Iterable, List, Match, Pattern
from typing import Callable, Dict, Iterable, List, Match, Optional, Pattern
from sphinx.util.osutil import canon_path
@@ -60,7 +60,7 @@ def _translate_pattern(pat: str) -> str:
return res + '$'
def compile_matchers(patterns: List[str]) -> List[Callable[[str], Match[str]]]:
def compile_matchers(patterns: List[str]) -> List[Callable[[str], Optional[Match[str]]]]:
return [re.compile(_translate_pattern(pat)).match for pat in patterns]
@@ -89,7 +89,7 @@ DOTFILES = Matcher(['**/.*'])
_pat_cache = {} # type: Dict[str, Pattern]
def patmatch(name: str, pat: str) -> Match[str]:
def patmatch(name: str, pat: str) -> Optional[Match[str]]:
"""Return if name matches pat. Adapted from fnmatch module."""
if pat not in _pat_cache:
_pat_cache[pat] = re.compile(_translate_pattern(pat))

View File

@@ -22,7 +22,7 @@ class BooleanParser(Parser):
"""
def parse_compare(self) -> Node:
node = None # type: Node
node: Node
token = self.stream.current
if token.type == 'name':
if token.value in ('true', 'false', 'True', 'False'):