Skip to content

Commit 1953749

Browse files
committed
Fix toc.empty_glob warning not being suppressible via suppress_warnings
The toctree glob-pattern warning emitted when no documents match was introduced in 8.2.0 (#13230) with the new ``toc.empty_glob`` sub-type to allow suppression via :confval:`suppress_warnings`. However, the ``logger.warning()`` call was missing the ``type='toc'`` keyword argument, so ``SphinxLoggerAdapter`` stored ``type=None`` on the log record. ``is_suppressed_warning()`` short-circuits to ``False`` whenever ``warning_type is None``, so the warning could never be suppressed. Fix by passing ``type='toc'`` alongside ``subtype='empty_glob'`` to match every other toctree warning in the same function.
1 parent cc7c6f4 commit 1953749

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Features added
1818
Bugs fixed
1919
----------
2020

21+
* #13230: Fix the ``toc.empty_glob`` warning sub-type not being suppressible
22+
via :confval:`suppress_warnings`, due to a missing ``type='toc'`` argument
23+
in the toctree directive's warning call.
24+
Patch by Ferdinand Schwenk.
2125
* #14189: autodoc: Fix duplicate ``:no-index-entry:`` for modules.
2226
Patch by Adam Turner
2327
* #13713: Fix compatibility with MyST-Parser.

sphinx/directives/other.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def parse_content(self, toctree: addnodes.toctree) -> None:
119119
__("toctree glob pattern %r didn't match any documents"),
120120
entry,
121121
location=toctree,
122+
type='toc',
122123
subtype='empty_glob',
123124
)
124125

tests/test_directives/test_directive_other.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,31 @@ def test_toctree_glob_and_url(app):
127127
)
128128

129129

130+
@pytest.mark.sphinx('html', testroot='toctree-glob')
131+
def test_toctree_glob_empty_emits_warning(app):
132+
"""An unmatched glob pattern emits a toc.empty_glob warning."""
133+
text = '.. toctree::\n :glob:\n\n nonexistent*\n'
134+
app.env.find_files(app.config, app.builder)
135+
restructuredtext.parse(app, text, 'index')
136+
assert (
137+
"toctree glob pattern 'nonexistent*' didn't match any documents"
138+
in app._warning.getvalue()
139+
)
140+
141+
142+
@pytest.mark.sphinx(
143+
'html',
144+
testroot='toctree-glob',
145+
confoverrides={'suppress_warnings': ['toc.empty_glob']},
146+
)
147+
def test_toctree_glob_empty_suppressed(app):
148+
"""suppress_warnings=['toc.empty_glob'] must suppress the empty glob warning."""
149+
text = '.. toctree::\n :glob:\n\n nonexistent*\n'
150+
app.env.find_files(app.config, app.builder)
151+
restructuredtext.parse(app, text, 'index')
152+
assert app._warning.getvalue() == ''
153+
154+
130155
@pytest.mark.sphinx('html', testroot='toctree-glob')
131156
def test_reversed_toctree(app):
132157
text = '.. toctree::\n :reversed:\n\n foo\n bar/index\n baz\n'

0 commit comments

Comments
 (0)