Skip to content

Commit 5edca97

Browse files
authored
Warn when html_sidebars values are strings (#12600)
1 parent 6d2ac39 commit 5edca97

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Release 7.4.5 (in development)
44
Bugs fixed
55
----------
66

7+
* #12593, #12600: Revert coercing the type of selected :confval:`html_sidebars`
8+
values to a list.
9+
Log an error message when string values are detected.
10+
Patch by Adam Turner.
711

812
Release 7.4.4 (released Jul 15, 2024)
913
=====================================

sphinx/builders/html/__init__.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -982,11 +982,10 @@ def has_wildcard(pattern: str) -> bool:
982982
matched = pattern
983983
sidebars = pat_sidebars
984984

985-
if len(sidebars) == 0:
986-
# keep defaults
987-
pass
988-
989-
ctx['sidebars'] = list(sidebars)
985+
# See error_on_html_sidebars_string_values.
986+
# Replace with simple list coercion in Sphinx 8.0
987+
# xref: RemovedInSphinx80Warning
988+
ctx['sidebars'] = sidebars
990989

991990
# --------- these are overwritten by the serialization builder
992991

@@ -1287,6 +1286,25 @@ def validate_html_favicon(app: Sphinx, config: Config) -> None:
12871286
config.html_favicon = None
12881287

12891288

1289+
def error_on_html_sidebars_string_values(app: Sphinx, config: Config) -> None:
1290+
"""Support removed in Sphinx 2."""
1291+
errors = {}
1292+
for pattern, pat_sidebars in config.html_sidebars.items():
1293+
if isinstance(pat_sidebars, str):
1294+
errors[pattern] = [pat_sidebars]
1295+
if not errors:
1296+
return
1297+
msg = __("Values in 'html_sidebars' must be a list of strings. "
1298+
"At least one pattern has a string value: %s. "
1299+
"Change to `html_sidebars = %r`.")
1300+
bad_patterns = ', '.join(map(repr, errors))
1301+
fixed = config.html_sidebars | errors
1302+
logger.error(msg, bad_patterns, fixed)
1303+
# Enable hard error in next major version.
1304+
# xref: RemovedInSphinx80Warning
1305+
# raise ConfigError(msg % (bad_patterns, fixed))
1306+
1307+
12901308
def error_on_html_4(_app: Sphinx, config: Config) -> None:
12911309
"""Error on HTML 4."""
12921310
if config.html4_writer:
@@ -1357,6 +1375,7 @@ def setup(app: Sphinx) -> ExtensionMetadata:
13571375
app.connect('config-inited', validate_html_static_path, priority=800)
13581376
app.connect('config-inited', validate_html_logo, priority=800)
13591377
app.connect('config-inited', validate_html_favicon, priority=800)
1378+
app.connect('config-inited', error_on_html_sidebars_string_values, priority=800)
13601379
app.connect('config-inited', error_on_html_4, priority=800)
13611380
app.connect('builder-inited', validate_math_renderer)
13621381
app.connect('html-page-context', setup_resource_paths)

tests/test_builders/test_build_html.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test the HTML builder and check output against XPath."""
22

3+
import contextlib
34
import os
45
import posixpath
56
import re
@@ -8,14 +9,39 @@
89

910
from sphinx.builders.html import validate_html_extra_path, validate_html_static_path
1011
from sphinx.deprecation import RemovedInSphinx80Warning
11-
from sphinx.errors import ConfigError
12+
from sphinx.errors import ConfigError, ThemeError
1213
from sphinx.util.console import strip_colors
1314
from sphinx.util.inventory import InventoryFile
1415

1516
from tests.test_builders.xpath_data import FIGURE_CAPTION
1617
from tests.test_builders.xpath_util import check_xpath
1718

1819

20+
def test_html_sidebars_error(make_app, tmp_path):
21+
(tmp_path / 'conf.py').touch()
22+
(tmp_path / 'index.rst').touch()
23+
app = make_app(
24+
buildername='html',
25+
srcdir=tmp_path,
26+
confoverrides={'html_sidebars': {'index': 'searchbox.html'}},
27+
)
28+
29+
# Test that the error is logged
30+
warnings = app.warning.getvalue()
31+
assert ("ERROR: Values in 'html_sidebars' must be a list of strings. "
32+
"At least one pattern has a string value: 'index'. "
33+
"Change to `html_sidebars = {'index': ['searchbox.html']}`.") in warnings
34+
35+
# But that the value is unchanged.
36+
# (Remove this bit of the test in Sphinx 8)
37+
def _html_context_hook(app, pagename, templatename, context, doctree):
38+
assert context["sidebars"] == 'searchbox.html'
39+
app.connect('html-page-context', _html_context_hook)
40+
with contextlib.suppress(ThemeError):
41+
# ignore template rendering issues (ThemeError).
42+
app.build()
43+
44+
1945
def test_html4_error(make_app, tmp_path):
2046
(tmp_path / 'conf.py').write_text('', encoding='utf-8')
2147
with pytest.raises(

0 commit comments

Comments
 (0)