summaryrefslogtreecommitdiff
path: root/sphinx/environment
diff options
context:
space:
mode:
authordanieleades <33452915+danieleades@users.noreply.github.com>2022-08-28 19:50:01 +0100
committerGitHub <noreply@github.com>2022-08-28 19:50:01 +0100
commit2b02173617cbf1c334f8f908f07391c9f09712b5 (patch)
tree585bf111db8a179c783c91f1ae965f7fb7b10da8 /sphinx/environment
parenta9b4b19be5ed5ae78df384f7c669e7d64a50aed6 (diff)
downloadsphinx-git-2b02173617cbf1c334f8f908f07391c9f09712b5.tar.gz
Further improve type annotations, reduce mypy whitelist (#10770)
Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
Diffstat (limited to 'sphinx/environment')
-rw-r--r--sphinx/environment/__init__.py27
-rw-r--r--sphinx/environment/adapters/indexentries.py2
-rw-r--r--sphinx/environment/collectors/toctree.py10
3 files changed, 25 insertions, 14 deletions
diff --git a/sphinx/environment/__init__.py b/sphinx/environment/__init__.py
index df82c5a8a..430e03358 100644
--- a/sphinx/environment/__init__.py
+++ b/sphinx/environment/__init__.py
@@ -91,7 +91,7 @@ class BuildEnvironment:
# --------- ENVIRONMENT INITIALIZATION -------------------------------------
- def __init__(self, app: "Sphinx" = None):
+ def __init__(self, app: Optional["Sphinx"] = None):
self.app: Sphinx = None
self.doctreedir: str = None
self.srcdir: str = None
@@ -327,7 +327,7 @@ class BuildEnvironment:
"""
return self.project.doc2path(docname, base)
- def relfn2path(self, filename: str, docname: str = None) -> Tuple[str, str]:
+ def relfn2path(self, filename: str, docname: Optional[str] = None) -> Tuple[str, str]:
"""Return paths to a file referenced from a document, relative to
documentation root and absolute.
@@ -340,7 +340,7 @@ class BuildEnvironment:
rel_fn = filename[1:]
else:
docdir = path.dirname(self.doc2path(docname or self.docname,
- base=None))
+ base=False))
rel_fn = path.join(docdir, filename)
return (canon_path(path.normpath(rel_fn)),
@@ -488,7 +488,9 @@ class BuildEnvironment:
*filename* should be absolute or relative to the source directory.
"""
- self.included[self.docname].add(self.path2doc(filename))
+ doc = self.path2doc(filename)
+ if doc:
+ self.included[self.docname].add(doc)
def note_reread(self) -> None:
"""Add the current document to the list of documents that will
@@ -517,9 +519,14 @@ class BuildEnvironment:
doctree.reporter = LoggingReporter(self.doc2path(docname))
return doctree
- def get_and_resolve_doctree(self, docname: str, builder: "Builder",
- doctree: nodes.document = None, prune_toctrees: bool = True,
- includehidden: bool = False) -> nodes.document:
+ def get_and_resolve_doctree(
+ self,
+ docname: str,
+ builder: "Builder",
+ doctree: Optional[nodes.document] = None,
+ prune_toctrees: bool = True,
+ includehidden: bool = False
+ ) -> nodes.document:
"""Read the doctree from the pickle, resolve cross-references and
toctrees and return it.
"""
@@ -543,7 +550,7 @@ class BuildEnvironment:
def resolve_toctree(self, docname: str, builder: "Builder", toctree: addnodes.toctree,
prune: bool = True, maxdepth: int = 0, titles_only: bool = False,
- collapse: bool = False, includehidden: bool = False) -> Node:
+ collapse: bool = False, includehidden: bool = False) -> Optional[Node]:
"""Resolve a *toctree* node into individual bullet lists with titles
as items, returning None (if no containing titles are found) or
a new node.
@@ -583,7 +590,9 @@ class BuildEnvironment:
def collect_relations(self) -> Dict[str, List[Optional[str]]]:
traversed = set()
- def traverse_toctree(parent: str, docname: str) -> Iterator[Tuple[str, str]]:
+ def traverse_toctree(
+ parent: Optional[str], docname: str
+ ) -> Iterator[Tuple[Optional[str], str]]:
if parent == docname:
logger.warning(__('self referenced toctree found. Ignored.'),
location=docname, type='toc',
diff --git a/sphinx/environment/adapters/indexentries.py b/sphinx/environment/adapters/indexentries.py
index 364331286..b651acae0 100644
--- a/sphinx/environment/adapters/indexentries.py
+++ b/sphinx/environment/adapters/indexentries.py
@@ -25,7 +25,7 @@ class IndexEntries:
"""Create the real index from the collected index entries."""
new: Dict[str, List] = {}
- def add_entry(word: str, subword: str, main: str, link: bool = True,
+ def add_entry(word: str, subword: str, main: Optional[str], link: bool = True,
dic: Dict[str, List] = new, key: Optional[str] = None) -> None:
# Force the word to be unicode if it's a ASCII bytestring.
# This will solve problems with unicode normalization later.
diff --git a/sphinx/environment/collectors/toctree.py b/sphinx/environment/collectors/toctree.py
index 507c83e97..52a8fd8d1 100644
--- a/sphinx/environment/collectors/toctree.py
+++ b/sphinx/environment/collectors/toctree.py
@@ -1,6 +1,6 @@
"""Toctree collector for sphinx.environment."""
-from typing import Any, Dict, List, Set, Tuple, Type, TypeVar, cast
+from typing import Any, Dict, List, Optional, Set, Tuple, Type, TypeVar, cast
from docutils import nodes
from docutils.nodes import Element, Node
@@ -66,7 +66,7 @@ class TocTreeCollector(EnvironmentCollector):
result.extend(traverse_in_section(child, cls))
return result
- def build_toc(node: Element, depth: int = 1) -> nodes.bullet_list:
+ def build_toc(node: Element, depth: int = 1) -> Optional[nodes.bullet_list]:
entries: List[Element] = []
for sectionnode in node:
# find all toctree nodes in this section and add them
@@ -132,7 +132,9 @@ class TocTreeCollector(EnvironmentCollector):
old_secnumbers = env.toc_secnumbers
env.toc_secnumbers = {}
- def _walk_toc(node: Element, secnums: Dict, depth: int, titlenode: nodes.title = None) -> None: # NOQA
+ def _walk_toc(
+ node: Element, secnums: Dict, depth: int, titlenode: Optional[nodes.title] = None
+ ) -> None:
# titlenode is the title of the document, it will get assigned a
# secnumber too, so that it shows up in next/prev/parent rellinks
for subnode in node.children:
@@ -207,7 +209,7 @@ class TocTreeCollector(EnvironmentCollector):
env.toc_fignumbers = {}
fignum_counter: Dict[str, Dict[Tuple[int, ...], int]] = {}
- def get_figtype(node: Node) -> str:
+ def get_figtype(node: Node) -> Optional[str]:
for domain in env.domains.values():
figtype = domain.get_enumerable_node_type(node)
if domain.name == 'std' and not domain.get_numfig_title(node): # type: ignore