diff --git a/sphinx/builders/latex/util.py b/sphinx/builders/latex/util.py index b7d79121c..0e3eb739d 100644 --- a/sphinx/builders/latex/util.py +++ b/sphinx/builders/latex/util.py @@ -8,6 +8,8 @@ :license: BSD, see LICENSE for details. """ +from typing import Optional + from docutils.writers.latex2e import Babel @@ -40,7 +42,7 @@ class ExtBabel(Babel): self.supported = False return 'english' # fallback to english - def get_mainlanguage_options(self) -> str: + def get_mainlanguage_options(self) -> Optional[str]: """Return options for polyglossia's ``\\setmainlanguage``.""" if self.use_polyglossia is False: return None diff --git a/sphinx/deprecation.py b/sphinx/deprecation.py index 5e5e673d2..a3fda02c4 100644 --- a/sphinx/deprecation.py +++ b/sphinx/deprecation.py @@ -73,6 +73,6 @@ class DeprecatedDict(dict): warnings.warn(self.message, self.warning, stacklevel=2) return super().get(key, default) - def update(self, other: Dict = None) -> None: # type: ignore + def update(self, other: Dict) -> None: # type: ignore warnings.warn(self.message, self.warning, stacklevel=2) super().update(other) diff --git a/sphinx/ext/napoleon/iterators.py b/sphinx/ext/napoleon/iterators.py index e91a3ec14..fc41afdb3 100644 --- a/sphinx/ext/napoleon/iterators.py +++ b/sphinx/ext/napoleon/iterators.py @@ -11,7 +11,7 @@ """ import collections -from typing import Any, Iterable +from typing import Any, Iterable, Optional class peek_iter: @@ -62,7 +62,7 @@ class peek_iter: def __next__(self, n: int = None) -> Any: return self.next(n) - def _fillcache(self, n: int) -> None: + def _fillcache(self, n: Optional[int]) -> None: """Cache `n` items. If `n` is 0 or None, then 1 item is cached.""" if not n: n = 1 @@ -123,7 +123,7 @@ class peek_iter: result = [self._cache.popleft() for i in range(n)] return result - def peek(self, n: int = None) -> Any: + def peek(self, n: Optional[int] = None) -> Any: """Preview the next item or `n` items of the iterator. The iterator is not advanced when peek is called. @@ -220,7 +220,7 @@ class modify_iter(peek_iter): 'modifier must be callable') super().__init__(*args) - def _fillcache(self, n: int) -> None: + def _fillcache(self, n: Optional[int]) -> None: """Cache `n` modified items. If `n` is 0 or None, 1 item is cached. Each item returned by the iterator is passed through the diff --git a/sphinx/locale/__init__.py b/sphinx/locale/__init__.py index 9812355ca..91adfef7c 100644 --- a/sphinx/locale/__init__.py +++ b/sphinx/locale/__init__.py @@ -12,7 +12,7 @@ import gettext import locale from collections import UserString, defaultdict from gettext import NullTranslations -from typing import Any, Callable, Dict, Iterable, List, Tuple, Union +from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union class _TranslationProxy(UserString): @@ -173,7 +173,7 @@ def init_console(locale_dir: str, catalog: str) -> Tuple[NullTranslations, bool] """ try: # encoding is ignored - language, _ = locale.getlocale(locale.LC_MESSAGES) + language, _ = locale.getlocale(locale.LC_MESSAGES) # type: Tuple[Optional[str], Any] except AttributeError: # LC_MESSAGES is not always defined. Fallback to the default language # in case it is not. diff --git a/sphinx/pycode/ast.py b/sphinx/pycode/ast.py index c885db494..9664e7edb 100644 --- a/sphinx/pycode/ast.py +++ b/sphinx/pycode/ast.py @@ -58,7 +58,7 @@ def parse(code: str, mode: str = 'exec') -> "ast.AST": return ast.parse(code, mode=mode) -def unparse(node: ast.AST) -> str: +def unparse(node: Optional[ast.AST]) -> Optional[str]: """Unparse an AST to string.""" if node is None: return None @@ -138,7 +138,7 @@ def _unparse_arg(arg: ast.arg, default: Optional[ast.AST]) -> str: def unparse_arguments(node: ast.arguments) -> str: """Unparse an arguments to string.""" - defaults = list(node.defaults) + defaults = list(node.defaults) # type: List[Optional[ast.AST]] positionals = len(node.args) posonlyargs = 0 if hasattr(node, "posonlyargs"): # for py38+ @@ -147,7 +147,7 @@ def unparse_arguments(node: ast.arguments) -> str: 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.AST]] for _ in range(len(kw_defaults), len(node.kwonlyargs)): kw_defaults.insert(0, None) diff --git a/sphinx/util/images.py b/sphinx/util/images.py index 568682b37..115007d31 100644 --- a/sphinx/util/images.py +++ b/sphinx/util/images.py @@ -12,7 +12,7 @@ import base64 import imghdr from collections import OrderedDict from os import path -from typing import IO, NamedTuple, Tuple +from typing import IO, NamedTuple, Optional, Tuple import imagesize @@ -36,7 +36,7 @@ DataURI = NamedTuple('DataURI', [('mimetype', str), ('data', bytes)]) -def get_image_size(filename: str) -> Tuple[int, int]: +def get_image_size(filename: str) -> Optional[Tuple[int, int]]: try: size = imagesize.get(filename) if size[0] == -1: @@ -53,7 +53,7 @@ def get_image_size(filename: str) -> Tuple[int, int]: return None -def guess_mimetype_for_stream(stream: IO, default: str = None) -> str: +def guess_mimetype_for_stream(stream: IO, default: Optional[str] = None) -> Optional[str]: imgtype = imghdr.what(stream) # type: ignore if imgtype: return 'image/' + imgtype @@ -61,7 +61,7 @@ def guess_mimetype_for_stream(stream: IO, default: str = None) -> str: return default -def guess_mimetype(filename: str = '', default: str = None) -> str: +def guess_mimetype(filename: str = '', default: Optional[str] = None) -> Optional[str]: _, ext = path.splitext(filename.lower()) if ext in mime_suffixes: return mime_suffixes[ext] @@ -72,7 +72,7 @@ def guess_mimetype(filename: str = '', default: str = None) -> str: return default -def get_image_extension(mimetype: str) -> str: +def get_image_extension(mimetype: str) -> Optional[str]: for ext, _mimetype in mime_suffixes.items(): if mimetype == _mimetype: return ext @@ -80,7 +80,7 @@ def get_image_extension(mimetype: str) -> str: return None -def parse_data_uri(uri: str) -> DataURI: +def parse_data_uri(uri: str) -> Optional[DataURI]: if not uri.startswith('data:'): return None @@ -101,7 +101,7 @@ def parse_data_uri(uri: str) -> DataURI: return DataURI(mimetype, charset, image_data) -def test_svg(h: bytes, f: IO) -> str: +def test_svg(h: bytes, f: IO) -> Optional[str]: """An additional imghdr library helper; test the header is SVG's or not.""" try: if ' None: self._path = path - self._io = None # type: StringIO + self._io = None # type: Optional[StringIO] def write(self, data: str) -> None: if not self._io: diff --git a/sphinx/util/png.py b/sphinx/util/png.py index c7f73fd50..22c35d991 100644 --- a/sphinx/util/png.py +++ b/sphinx/util/png.py @@ -10,6 +10,7 @@ import binascii import struct +from typing import Optional LEN_IEND = 12 @@ -20,7 +21,7 @@ DEPTH_CHUNK_START = b'tEXtDepth\x00' IEND_CHUNK = b'\x00\x00\x00\x00IEND\xAE\x42\x60\x82' -def read_png_depth(filename: str) -> int: +def read_png_depth(filename: str) -> Optional[int]: """Read the special tEXt chunk indicating the depth from a PNG file.""" with open(filename, 'rb') as f: f.seek(- (LEN_IEND + LEN_DEPTH), 2)