Skip to content

Commit 2f18f2e

Browse files
authored
Fix translator_setup: undefined default_translator_class for linkcheck builder (#2029)
The translator setup is called unconditionally to add the HootstrapHTML5TransatorMixin. If no translators have been set explicitly this falls back to `app.builder.default_translator_class`, which is however not defined for the `linkcheck` builder (maybe others), resulting in an attribute error: ```console $ sphinx-build -T -b linkcheck <src> <dest> <...> Traceback (most recent call last): File "/PATH/TO/venv/lib/python3.11/site-packages/sphinx/events.py", line 404, in emit results.append(listener.handler(self.app, *args)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/PATH/TO/venv/lib/python3.11/site-packages/pydata_sphinx_theme/translator.py", line 88, in setup_translators app.builder.default_translator_class, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'CheckExternalLinksBuilder' object has no attribute 'default_translator_class' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/PATH/TO/venv/lib/python3.11/site-packages/sphinx/cmd/build.py", line 496, in build_main app = Sphinx( ^^^^^^^ File "/PATH/TO/venv/lib/python3.11/site-packages/sphinx/application.py", line 295, in __init__ self._init_builder() File "/PATH/TO/venv/lib/python3.11/site-packages/sphinx/application.py", line 369, in _init_builder self.events.emit('builder-inited') File "/PATH/TO/venv/lib/python3.11/site-packages/sphinx/events.py", line 415, in emit raise ExtensionError( sphinx.errors.ExtensionError: Handler <function setup_translators at 0x7f73ec330400> for event 'builder-inited' threw an exception (exception: 'CheckExternalLinksBuilder' object has no attribute 'default_translator_class') ``` This PR adds a safe check for `app.builder.default_translator_class` and skips the translator setup if the attribute is not defined. I have tested the modified version with `linkcheck`, but I am not sure where to add a unit test for these changes.
1 parent 92f5110 commit 2f18f2e

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/pydata_sphinx_theme/translator.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,17 @@ def setup_translators(app: Sphinx):
7676
If we don't detect an HTML-based translator, then we do nothing.
7777
"""
7878
if not app.registry.translators.items():
79+
try:
80+
default_translator_class = app.builder.default_translator_class
81+
except AttributeError:
82+
# some builders, e.g. linkcheck, do not define 'default_translator_class'
83+
return
84+
7985
translator = types.new_class(
8086
"BootstrapHTML5Translator",
8187
(
8288
BootstrapHTML5TranslatorMixin,
83-
app.builder.default_translator_class,
89+
default_translator_class,
8490
),
8591
{},
8692
)

0 commit comments

Comments
 (0)