Skip to content

Commit a03556f

Browse files
danieleadesdaniel.eadesAA-Turner
authored
Shrink mypy whitelist for tests.test_builders.test_build_latex (#14138)
Co-authored-by: daniel.eades <daniel.eades@seebyte.com> Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
1 parent 0204145 commit a03556f

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,6 @@ ignore_missing_imports = true
219219
module = [
220220
# tests/
221221
"tests.test_search",
222-
# tests/test_builders
223-
"tests.test_builders.test_build_latex",
224222
# tests/test_config
225223
"tests.test_config.test_config",
226224
# tests/test_directives

tests/test_builders/test_build_latex.py

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pathlib import Path
1111
from shutil import copyfile
1212
from subprocess import CalledProcessError
13+
from types import NoneType
1314
from typing import TYPE_CHECKING
1415

1516
import docutils
@@ -24,7 +25,7 @@
2425
from sphinx.util.osutil import ensuredir
2526
from sphinx.writers.latex import LaTeXTranslator
2627

27-
from tests.utils import http_server
28+
from tests.utils import TEST_ROOTS_DIR, http_server
2829

2930
if TYPE_CHECKING:
3031
from sphinx.testing.util import SphinxTestApp
@@ -46,7 +47,7 @@
4647

4748

4849
# only run latex if all needed packages are there
49-
def kpsetest(*filenames):
50+
def kpsetest(*filenames: str) -> bool:
5051
try:
5152
subprocess.run(['kpsewhich', *list(filenames)], capture_output=True, check=True) # NoQA: S607
5253
return True
@@ -56,8 +57,11 @@ def kpsetest(*filenames):
5657

5758
# compile latex document with app.config.latex_engine
5859
def compile_latex_document(
59-
app, filename='projectnamenotset.tex', docclass='manual', runtwice=False
60-
):
60+
app: SphinxTestApp,
61+
filename: str = 'projectnamenotset.tex',
62+
docclass: str = 'manual',
63+
runtwice: bool = False,
64+
) -> None:
6165
# now, try to run latex over it
6266
try:
6367
with chdir(app.outdir):
@@ -112,23 +116,26 @@ def compile_latex_document(
112116
class RemoteImageHandler(http.server.BaseHTTPRequestHandler):
113117
protocol_version = 'HTTP/1.1'
114118

115-
def do_GET(self):
116-
content, content_type = None, None
117-
if self.path == '/sphinx.png':
118-
with open('tests/roots/test-local-logo/images/img.png', 'rb') as f:
119-
content = f.read()
120-
content_type = 'image/png'
121-
122-
if content:
123-
self.send_response(200, 'OK')
124-
self.send_header('Content-Length', str(len(content)))
125-
self.send_header('Content-Type', content_type)
126-
self.end_headers()
127-
self.wfile.write(content)
128-
else:
129-
self.send_response(404, 'Not Found')
130-
self.send_header('Content-Length', '0')
131-
self.end_headers()
119+
def do_GET(self) -> None:
120+
if self.path != '/sphinx.png':
121+
self._send_not_found()
122+
return
123+
124+
img_path = TEST_ROOTS_DIR / 'test-local-logo' / 'images' / 'img.png'
125+
content = img_path.read_bytes()
126+
self._send_bytes(content, 'image/png')
127+
128+
def _send_bytes(self, content: bytes, content_type: str) -> None:
129+
self.send_response(200, 'OK')
130+
self.send_header('Content-Length', str(len(content)))
131+
self.send_header('Content-Type', content_type)
132+
self.end_headers()
133+
self.wfile.write(content)
134+
135+
def _send_not_found(self) -> None:
136+
self.send_response(404, 'Not Found')
137+
self.send_header('Content-Length', '0')
138+
self.end_headers()
132139

133140

134141
@skip_if_requested
@@ -153,8 +160,12 @@ def do_GET(self):
153160
freshenv=True,
154161
)
155162
def test_build_latex_doc(
156-
app, engine, docclass, python_maximum_signature_line_length, runtwice
157-
):
163+
app: SphinxTestApp,
164+
engine: str,
165+
docclass: str,
166+
python_maximum_signature_line_length: int | None,
167+
runtwice: bool,
168+
) -> None:
158169
app.config.python_maximum_signature_line_length = (
159170
python_maximum_signature_line_length
160171
)
@@ -189,7 +200,7 @@ def test_build_latex_doc(
189200
'latex',
190201
testroot='latex-images-css3-lengths',
191202
)
192-
def test_build_latex_with_css3_lengths(app, engine):
203+
def test_build_latex_with_css3_lengths(app: SphinxTestApp, engine: str) -> None:
193204
app.config.latex_engine = engine
194205
app.config.latex_documents = [(*app.config.latex_documents[0][:4], 'howto')]
195206
app.builder.init()
@@ -1605,7 +1616,7 @@ def test_latex_table_tabulars(app: SphinxTestApp) -> None:
16051616
content = re.sub(r'\\sphinxstepscope', '', content) # filter a separator
16061617
tables[sectname] = content.strip()
16071618

1608-
def get_expected(name):
1619+
def get_expected(name: str) -> str:
16091620
return (
16101621
(app.srcdir / 'expects' / (name + '.tex'))
16111622
.read_text(encoding='utf8')
@@ -1645,7 +1656,7 @@ def test_latex_table_longtable(app: SphinxTestApp) -> None:
16451656
content = re.sub(r'\\sphinxstepscope', '', content) # filter a separator
16461657
tables[sectname] = content.strip()
16471658

1648-
def get_expected(name):
1659+
def get_expected(name: str) -> str:
16491660
return (
16501661
(app.srcdir / 'expects' / (name + '.tex'))
16511662
.read_text(encoding='utf8')
@@ -1682,7 +1693,7 @@ def test_latex_table_complex_tables(app: SphinxTestApp) -> None:
16821693
sectname, _, content = chap.partition('}')
16831694
tables[sectname] = content.strip()
16841695

1685-
def get_expected(name):
1696+
def get_expected(name: str) -> str:
16861697
return (
16871698
(app.srcdir / 'expects' / (name + '.tex'))
16881699
.read_text(encoding='utf8')
@@ -1994,7 +2005,7 @@ def test_latex_figure_in_admonition(app: SphinxTestApp) -> None:
19942005
assert f'{type} directive.\n\n\\begin{{figure}}[H]' in result
19952006

19962007

1997-
def test_default_latex_documents():
2008+
def test_default_latex_documents() -> None:
19982009
from sphinx.util import texescape
19992010

20002011
texescape.init()
@@ -2003,8 +2014,8 @@ def test_default_latex_documents():
20032014
'project': 'STASI™ Documentation',
20042015
'author': "Wolfgang Schäuble & G'Beckstein.",
20052016
})
2006-
config.add('latex_engine', None, True, None)
2007-
config.add('latex_theme', 'manual', True, None)
2017+
config.add('latex_engine', None, 'env', (str, NoneType))
2018+
config.add('latex_theme', 'manual', 'env', (str,))
20082019
expected = [
20092020
(
20102021
'index',

0 commit comments

Comments
 (0)