summaryrefslogtreecommitdiff
path: root/sphinx/util
diff options
context:
space:
mode:
authordanieleades <33452915+danieleades@users.noreply.github.com>2022-07-18 22:08:16 +0100
committerGitHub <noreply@github.com>2022-07-18 22:08:16 +0100
commita504ac6100a577cbda1bedf80d69636603ee287c (patch)
tree0aa79585440486bdb109f7238f8f5c992dad0d99 /sphinx/util
parenta340427ba4d208193d539cb7e8401be23d75547e (diff)
downloadsphinx-git-a504ac6100a577cbda1bedf80d69636603ee287c.tar.gz
Improve static typing strictness (#10569)
Diffstat (limited to 'sphinx/util')
-rw-r--r--sphinx/util/math.py4
-rw-r--r--sphinx/util/nodes.py5
-rw-r--r--sphinx/util/osutil.py2
-rw-r--r--sphinx/util/parallel.py4
-rw-r--r--sphinx/util/stemmer/__init__.py8
-rw-r--r--sphinx/util/tags.py4
-rw-r--r--sphinx/util/template.py22
-rw-r--r--sphinx/util/texescape.py6
-rw-r--r--sphinx/util/typing.py4
9 files changed, 35 insertions, 24 deletions
diff --git a/sphinx/util/math.py b/sphinx/util/math.py
index 7caf662dc..0c42a00ed 100644
--- a/sphinx/util/math.py
+++ b/sphinx/util/math.py
@@ -1,5 +1,7 @@
"""Utility functions for math."""
+from typing import Optional
+
from docutils import nodes
from sphinx.builders.html import HTMLTranslator
@@ -20,7 +22,7 @@ def get_node_equation_number(writer: HTMLTranslator, node: nodes.math_block) ->
return node['number']
-def wrap_displaymath(text: str, label: str, numbering: bool) -> str:
+def wrap_displaymath(text: str, label: Optional[str], numbering: bool) -> str:
def is_equation(part: str) -> str:
return part.strip()
diff --git a/sphinx/util/nodes.py b/sphinx/util/nodes.py
index 1a861a060..0646b44e6 100644
--- a/sphinx/util/nodes.py
+++ b/sphinx/util/nodes.py
@@ -513,7 +513,7 @@ _non_id_translate_digraphs = {
def make_id(env: "BuildEnvironment", document: nodes.document,
- prefix: str = '', term: str = None) -> str:
+ prefix: str = '', term: Optional[str] = None) -> str:
"""Generate an appropriate node_id for given *prefix* and *term*."""
node_id = None
if prefix:
@@ -550,7 +550,8 @@ def find_pending_xref_condition(node: addnodes.pending_xref, condition: str
def make_refnode(builder: "Builder", fromdocname: str, todocname: str, targetid: str,
- child: Union[Node, List[Node]], title: str = None) -> nodes.reference:
+ child: Union[Node, List[Node]], title: Optional[str] = None
+ ) -> nodes.reference:
"""Shortcut to create a reference node."""
node = nodes.reference('', '', internal=True)
if fromdocname == todocname and targetid:
diff --git a/sphinx/util/osutil.py b/sphinx/util/osutil.py
index d0ed42c8c..bfc80adcf 100644
--- a/sphinx/util/osutil.py
+++ b/sphinx/util/osutil.py
@@ -112,7 +112,7 @@ def make_filename_from_project(project: str) -> str:
return make_filename(project_suffix_re.sub('', project)).lower()
-def relpath(path: str, start: str = os.curdir) -> str:
+def relpath(path: str, start: Optional[str] = os.curdir) -> str:
"""Return a relative filepath to *path* either from the current directory or
from an optional *start* directory.
diff --git a/sphinx/util/parallel.py b/sphinx/util/parallel.py
index 193d2a80d..e89418ad3 100644
--- a/sphinx/util/parallel.py
+++ b/sphinx/util/parallel.py
@@ -81,7 +81,9 @@ class ParallelTasks:
logging.convert_serializable(collector.logs)
pipe.send((failed, collector.logs, ret))
- def add_task(self, task_func: Callable, arg: Any = None, result_func: Callable = None) -> None: # NOQA
+ def add_task(
+ self, task_func: Callable, arg: Any = None, result_func: Optional[Callable] = None
+ ) -> None:
tid = self._taskid
self._taskid += 1
self._result_funcs[tid] = result_func or (lambda arg, result: None)
diff --git a/sphinx/util/stemmer/__init__.py b/sphinx/util/stemmer/__init__.py
index 6d27592d8..9f91ce59e 100644
--- a/sphinx/util/stemmer/__init__.py
+++ b/sphinx/util/stemmer/__init__.py
@@ -8,7 +8,7 @@ from sphinx.deprecation import RemovedInSphinx70Warning
class PorterStemmer:
- def __init__(self):
+ def __init__(self) -> None:
warnings.warn(f"{self.__class__.__name__} is deprecated, use "
"snowballstemmer.stemmer('porter') instead.",
RemovedInSphinx70Warning, stacklevel=2)
@@ -22,7 +22,7 @@ class PorterStemmer:
class BaseStemmer:
- def __init__(self):
+ def __init__(self) -> None:
warnings.warn(f"{self.__class__.__name__} is deprecated, use "
"snowballstemmer.stemmer('porter') instead.",
RemovedInSphinx70Warning, stacklevel=3)
@@ -32,7 +32,7 @@ class BaseStemmer:
class PyStemmer(BaseStemmer):
- def __init__(self): # NoQA
+ def __init__(self) -> None:
super().__init__()
self.stemmer = snowballstemmer.stemmer('porter')
@@ -44,7 +44,7 @@ class PyStemmer(BaseStemmer):
class StandardStemmer(BaseStemmer):
- def __init__(self): # NoQA
+ def __init__(self) -> None:
super().__init__()
self.stemmer = snowballstemmer.stemmer('porter')
diff --git a/sphinx/util/tags.py b/sphinx/util/tags.py
index 89276732d..ac69ae852 100644
--- a/sphinx/util/tags.py
+++ b/sphinx/util/tags.py
@@ -1,4 +1,4 @@
-from typing import Iterator, List
+from typing import Iterator, List, Optional
from jinja2 import nodes
from jinja2.environment import Environment
@@ -35,7 +35,7 @@ class BooleanParser(Parser):
class Tags:
- def __init__(self, tags: List[str] = None) -> None:
+ def __init__(self, tags: Optional[List[str]] = None) -> None:
self.tags = dict.fromkeys(tags or [], True)
def has(self, tag: str) -> bool:
diff --git a/sphinx/util/template.py b/sphinx/util/template.py
index 5a73515b3..23c68e36b 100644
--- a/sphinx/util/template.py
+++ b/sphinx/util/template.py
@@ -3,7 +3,7 @@
import os
from functools import partial
from os import path
-from typing import Callable, Dict, List, Tuple, Union
+from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from jinja2 import TemplateNotFound
from jinja2.environment import Environment
@@ -17,15 +17,15 @@ from sphinx.util import rst, texescape
class BaseRenderer:
- def __init__(self, loader: BaseLoader = None) -> None:
+ def __init__(self, loader: Optional[BaseLoader] = None) -> None:
self.env = SandboxedEnvironment(loader=loader, extensions=['jinja2.ext.i18n'])
self.env.filters['repr'] = repr
self.env.install_gettext_translations(get_translator())
- def render(self, template_name: str, context: Dict) -> str:
+ def render(self, template_name: str, context: Dict[str, Any]) -> str:
return self.env.get_template(template_name).render(context)
- def render_string(self, source: str, context: Dict) -> str:
+ def render_string(self, source: str, context: Dict[str, Any]) -> str:
return self.env.from_string(source).render(context)
@@ -41,25 +41,27 @@ class FileRenderer(BaseRenderer):
super().__init__(loader)
@classmethod
- def render_from_file(cls, filename: str, context: Dict) -> str:
+ def render_from_file(cls, filename: str, context: Dict[str, Any]) -> str:
dirname = os.path.dirname(filename)
basename = os.path.basename(filename)
return cls(dirname).render(basename, context)
class SphinxRenderer(FileRenderer):
- def __init__(self, template_path: Union[str, List[str]] = None) -> None:
+ def __init__(self, template_path: Union[None, str, List[str]] = None) -> None:
if template_path is None:
template_path = os.path.join(package_dir, 'templates')
super().__init__(template_path)
@classmethod
- def render_from_file(cls, filename: str, context: Dict) -> str:
+ def render_from_file(cls, filename: str, context: Dict[str, Any]) -> str:
return FileRenderer.render_from_file(filename, context)
class LaTeXRenderer(SphinxRenderer):
- def __init__(self, template_path: str = None, latex_engine: str = None) -> None:
+ def __init__(
+ self, template_path: Optional[str] = None, latex_engine: Optional[str] = None
+ ) -> None:
if template_path is None:
template_path = os.path.join(package_dir, 'templates', 'latex')
super().__init__(template_path)
@@ -81,7 +83,9 @@ class LaTeXRenderer(SphinxRenderer):
class ReSTRenderer(SphinxRenderer):
- def __init__(self, template_path: Union[str, List[str]] = None, language: str = None) -> None: # NOQA
+ def __init__(
+ self, template_path: Union[None, str, List[str]] = None, language: Optional[str] = None
+ ) -> None:
super().__init__(template_path)
# add language to environment
diff --git a/sphinx/util/texescape.py b/sphinx/util/texescape.py
index 83c78bb38..ba885f695 100644
--- a/sphinx/util/texescape.py
+++ b/sphinx/util/texescape.py
@@ -1,7 +1,7 @@
"""TeX escaping helper."""
import re
-from typing import Dict
+from typing import Dict, Optional
tex_replacements = [
# map TeX special chars
@@ -100,7 +100,7 @@ _tex_hlescape_map: Dict[int, str] = {}
_tex_hlescape_map_without_unicode: Dict[int, str] = {}
-def escape(s: str, latex_engine: str = None) -> str:
+def escape(s: str, latex_engine: Optional[str] = None) -> str:
"""Escape text for LaTeX output."""
if latex_engine in ('lualatex', 'xelatex'):
# unicode based LaTeX engine
@@ -109,7 +109,7 @@ def escape(s: str, latex_engine: str = None) -> str:
return s.translate(_tex_escape_map)
-def hlescape(s: str, latex_engine: str = None) -> str:
+def hlescape(s: str, latex_engine: Optional[str] = None) -> str:
"""Escape text for LaTeX highlighter."""
if latex_engine in ('lualatex', 'xelatex'):
# unicode based LaTeX engine
diff --git a/sphinx/util/typing.py b/sphinx/util/typing.py
index c070eb00c..eed7a7b95 100644
--- a/sphinx/util/typing.py
+++ b/sphinx/util/typing.py
@@ -69,7 +69,9 @@ InventoryItem = Tuple[str, str, str, str]
Inventory = Dict[str, Dict[str, InventoryItem]]
-def get_type_hints(obj: Any, globalns: Dict = None, localns: Dict = None) -> Dict[str, Any]:
+def get_type_hints(
+ obj: Any, globalns: Optional[Dict[str, Any]] = None, localns: Optional[Dict] = None
+) -> Dict[str, Any]:
"""Return a dictionary containing type hints for a function, method, module or class object.
This is a simple wrapper of `typing.get_type_hints()` that does not raise an error on