From a03556feb46e369a4b7cdc28ae8ecfda8701c194 Mon Sep 17 00:00:00 2001 From: danieleades <33452915+danieleades@users.noreply.github.com> Date: Mon, 1 Dec 2025 10:58:51 +0000 Subject: [PATCH] Shrink mypy whitelist for ``tests.test_builders.test_build_latex`` (#14138) Co-authored-by: daniel.eades Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> --- pyproject.toml | 2 - tests/test_builders/test_build_latex.py | 69 ++++++++++++++----------- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 78b1b9d165..5ef7cb1493 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -219,8 +219,6 @@ ignore_missing_imports = true module = [ # tests/ "tests.test_search", - # tests/test_builders - "tests.test_builders.test_build_latex", # tests/test_config "tests.test_config.test_config", # tests/test_directives diff --git a/tests/test_builders/test_build_latex.py b/tests/test_builders/test_build_latex.py index 7a51a82e51..ad0de59f37 100644 --- a/tests/test_builders/test_build_latex.py +++ b/tests/test_builders/test_build_latex.py @@ -10,6 +10,7 @@ from contextlib import chdir from pathlib import Path from shutil import copyfile from subprocess import CalledProcessError +from types import NoneType from typing import TYPE_CHECKING import docutils @@ -24,7 +25,7 @@ from sphinx.ext.intersphinx._load import load_mappings, validate_intersphinx_map from sphinx.util.osutil import ensuredir from sphinx.writers.latex import LaTeXTranslator -from tests.utils import http_server +from tests.utils import TEST_ROOTS_DIR, http_server if TYPE_CHECKING: from sphinx.testing.util import SphinxTestApp @@ -46,7 +47,7 @@ STYLEFILES = [ # only run latex if all needed packages are there -def kpsetest(*filenames): +def kpsetest(*filenames: str) -> bool: try: subprocess.run(['kpsewhich', *list(filenames)], capture_output=True, check=True) # NoQA: S607 return True @@ -56,8 +57,11 @@ def kpsetest(*filenames): # compile latex document with app.config.latex_engine def compile_latex_document( - app, filename='projectnamenotset.tex', docclass='manual', runtwice=False -): + app: SphinxTestApp, + filename: str = 'projectnamenotset.tex', + docclass: str = 'manual', + runtwice: bool = False, +) -> None: # now, try to run latex over it try: with chdir(app.outdir): @@ -112,23 +116,26 @@ skip_if_docutils_not_at_least_at_0_22 = pytest.mark.skipif( class RemoteImageHandler(http.server.BaseHTTPRequestHandler): protocol_version = 'HTTP/1.1' - def do_GET(self): - content, content_type = None, None - if self.path == '/sphinx.png': - with open('tests/roots/test-local-logo/images/img.png', 'rb') as f: - content = f.read() - content_type = 'image/png' + def do_GET(self) -> None: + if self.path != '/sphinx.png': + self._send_not_found() + return - if content: - self.send_response(200, 'OK') - self.send_header('Content-Length', str(len(content))) - self.send_header('Content-Type', content_type) - self.end_headers() - self.wfile.write(content) - else: - self.send_response(404, 'Not Found') - self.send_header('Content-Length', '0') - self.end_headers() + img_path = TEST_ROOTS_DIR / 'test-local-logo' / 'images' / 'img.png' + content = img_path.read_bytes() + self._send_bytes(content, 'image/png') + + def _send_bytes(self, content: bytes, content_type: str) -> None: + self.send_response(200, 'OK') + self.send_header('Content-Length', str(len(content))) + self.send_header('Content-Type', content_type) + self.end_headers() + self.wfile.write(content) + + def _send_not_found(self) -> None: + self.send_response(404, 'Not Found') + self.send_header('Content-Length', '0') + self.end_headers() @skip_if_requested @@ -153,8 +160,12 @@ class RemoteImageHandler(http.server.BaseHTTPRequestHandler): freshenv=True, ) def test_build_latex_doc( - app, engine, docclass, python_maximum_signature_line_length, runtwice -): + app: SphinxTestApp, + engine: str, + docclass: str, + python_maximum_signature_line_length: int | None, + runtwice: bool, +) -> None: app.config.python_maximum_signature_line_length = ( python_maximum_signature_line_length ) @@ -189,7 +200,7 @@ def test_build_latex_doc( 'latex', testroot='latex-images-css3-lengths', ) -def test_build_latex_with_css3_lengths(app, engine): +def test_build_latex_with_css3_lengths(app: SphinxTestApp, engine: str) -> None: app.config.latex_engine = engine app.config.latex_documents = [(*app.config.latex_documents[0][:4], 'howto')] app.builder.init() @@ -1605,7 +1616,7 @@ def test_latex_table_tabulars(app: SphinxTestApp) -> None: content = re.sub(r'\\sphinxstepscope', '', content) # filter a separator tables[sectname] = content.strip() - def get_expected(name): + def get_expected(name: str) -> str: return ( (app.srcdir / 'expects' / (name + '.tex')) .read_text(encoding='utf8') @@ -1645,7 +1656,7 @@ def test_latex_table_longtable(app: SphinxTestApp) -> None: content = re.sub(r'\\sphinxstepscope', '', content) # filter a separator tables[sectname] = content.strip() - def get_expected(name): + def get_expected(name: str) -> str: return ( (app.srcdir / 'expects' / (name + '.tex')) .read_text(encoding='utf8') @@ -1682,7 +1693,7 @@ def test_latex_table_complex_tables(app: SphinxTestApp) -> None: sectname, _, content = chap.partition('}') tables[sectname] = content.strip() - def get_expected(name): + def get_expected(name: str) -> str: return ( (app.srcdir / 'expects' / (name + '.tex')) .read_text(encoding='utf8') @@ -1994,7 +2005,7 @@ def test_latex_figure_in_admonition(app: SphinxTestApp) -> None: assert f'{type} directive.\n\n\\begin{{figure}}[H]' in result -def test_default_latex_documents(): +def test_default_latex_documents() -> None: from sphinx.util import texescape texescape.init() @@ -2003,8 +2014,8 @@ def test_default_latex_documents(): 'project': 'STASI™ Documentation', 'author': "Wolfgang Schäuble & G'Beckstein.", }) - config.add('latex_engine', None, True, None) - config.add('latex_theme', 'manual', True, None) + config.add('latex_engine', None, 'env', (str, NoneType)) + config.add('latex_theme', 'manual', 'env', (str,)) expected = [ ( 'index',