summaryrefslogtreecommitdiff
path: root/sphinx/builders/latex
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/builders/latex
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/builders/latex')
-rw-r--r--sphinx/builders/latex/theming.py8
-rw-r--r--sphinx/builders/latex/transforms.py16
2 files changed, 13 insertions, 11 deletions
diff --git a/sphinx/builders/latex/theming.py b/sphinx/builders/latex/theming.py
index 6bcdc7f9d..5f8300084 100644
--- a/sphinx/builders/latex/theming.py
+++ b/sphinx/builders/latex/theming.py
@@ -2,7 +2,7 @@
import configparser
from os import path
-from typing import Dict
+from typing import Dict, Optional
from sphinx.application import Sphinx
from sphinx.config import Config
@@ -113,14 +113,12 @@ class ThemeFactory:
if name in self.themes:
theme = self.themes[name]
else:
- theme = self.find_user_theme(name)
- if not theme:
- theme = Theme(name)
+ theme = self.find_user_theme(name) or Theme(name)
theme.update(self.config)
return theme
- def find_user_theme(self, name: str) -> Theme:
+ def find_user_theme(self, name: str) -> Optional[Theme]:
"""Find a theme named as *name* from latex_theme_path."""
for theme_path in self.theme_paths:
config_path = path.join(theme_path, name, 'theme.conf')
diff --git a/sphinx/builders/latex/transforms.py b/sphinx/builders/latex/transforms.py
index 5e478f5c4..e996250f7 100644
--- a/sphinx/builders/latex/transforms.py
+++ b/sphinx/builders/latex/transforms.py
@@ -1,6 +1,6 @@
"""Transforms for LaTeX builder."""
-from typing import Any, Dict, List, Set, Tuple, cast
+from typing import Any, Dict, List, Optional, Set, Tuple, cast
from docutils import nodes
from docutils.nodes import Element, Node
@@ -94,13 +94,17 @@ class ShowUrlsTransform(SphinxPostTransform):
def get_docname_for_node(self, node: Node) -> str:
while node:
if isinstance(node, nodes.document):
- return self.env.path2doc(node['source'])
+ return self.env.path2doc(node['source']) or ''
elif isinstance(node, addnodes.start_of_file):
return node['docname']
else:
node = node.parent
- return None # never reached here. only for type hinting
+ try:
+ source = node['source'] # type: ignore[index]
+ except TypeError:
+ raise ValueError('Failed to get a docname!') from None
+ raise ValueError(f'Failed to get a docname for source {source!r}!')
def create_footnote(self, uri: str, docname: str) -> Tuple[nodes.footnote, nodes.footnote_reference]: # NOQA
reference = nodes.reference('', nodes.Text(uri), refuri=uri, nolinkurl=True)
@@ -355,7 +359,7 @@ class LaTeXFootnoteVisitor(nodes.NodeVisitor):
self.footnotes: List[nodes.footnote] = footnotes
self.pendings: List[nodes.footnote] = []
self.table_footnotes: List[nodes.footnote] = []
- self.restricted: Element = None
+ self.restricted: Optional[Element] = None
super().__init__(document)
def unknown_visit(self, node: Node) -> None:
@@ -433,7 +437,7 @@ class LaTeXFootnoteVisitor(nodes.NodeVisitor):
number = node.astext().strip()
docname = node['docname']
if (docname, number) in self.appeared:
- footnote = self.appeared.get((docname, number))
+ footnote = self.appeared[(docname, number)]
footnote["referred"] = True
mark = footnotemark('', number, refid=node['refid'])
@@ -458,7 +462,7 @@ class LaTeXFootnoteVisitor(nodes.NodeVisitor):
if docname == footnote['docname'] and footnote['ids'][0] == node['refid']:
return footnote
- return None
+ raise ValueError('No footnote not found for given reference node %r' % node)
class BibliographyTransform(SphinxPostTransform):