2525
2626from sphinx ._cli .util .colour import darkgray , darkgreen , purple , red , turquoise
2727from sphinx .builders .dummy import DummyBuilder
28+ from sphinx .errors import ConfigError
2829from sphinx .locale import __
2930from sphinx .transforms .post_transforms import SphinxPostTransform
3031from sphinx .util import logging , requests
@@ -178,7 +179,7 @@ def process_result(self, result: CheckResult) -> None:
178179 text = 'with unknown code'
179180 linkstat ['text' ] = text
180181 redirection = f'{ text } to { result .message } '
181- if self .config .linkcheck_allowed_redirects :
182+ if self .config .linkcheck_allowed_redirects is not None :
182183 msg = f'redirect { res_uri } - { redirection } '
183184 logger .warning (msg , location = (result .docname , result .lineno ))
184185 else :
@@ -386,7 +387,7 @@ def __init__(
386387 )
387388 self .check_anchors : bool = config .linkcheck_anchors
388389 self .allowed_redirects : dict [re .Pattern [str ], re .Pattern [str ]]
389- self .allowed_redirects = config .linkcheck_allowed_redirects
390+ self .allowed_redirects = config .linkcheck_allowed_redirects or {}
390391 self .retries : int = config .linkcheck_retries
391392 self .rate_limit_timeout = config .linkcheck_rate_limit_timeout
392393 self ._allow_unauthorized = config .linkcheck_allow_unauthorized
@@ -748,20 +749,26 @@ def rewrite_github_anchor(app: Sphinx, uri: str) -> str | None:
748749
749750
750751def compile_linkcheck_allowed_redirects (app : Sphinx , config : Config ) -> None :
751- """Compile patterns in linkcheck_allowed_redirects to the regexp objects."""
752- linkcheck_allowed_redirects = app .config .linkcheck_allowed_redirects
753- for url , pattern in list (linkcheck_allowed_redirects .items ()):
752+ """Compile patterns to the regexp objects."""
753+ if config .linkcheck_allowed_redirects is _sentinel_lar :
754+ config .linkcheck_allowed_redirects = None
755+ return
756+ if not isinstance (config .linkcheck_allowed_redirects , dict ):
757+ raise ConfigError
758+ allowed_redirects = {}
759+ for url , pattern in config .linkcheck_allowed_redirects .items ():
754760 try :
755- linkcheck_allowed_redirects [re .compile (url )] = re .compile (pattern )
761+ allowed_redirects [re .compile (url )] = re .compile (pattern )
756762 except re .error as exc :
757763 logger .warning (
758764 __ ('Failed to compile regex in linkcheck_allowed_redirects: %r %s' ),
759765 exc .pattern ,
760766 exc .msg ,
761767 )
762- finally :
763- # Remove the original regexp-string
764- linkcheck_allowed_redirects .pop (url )
768+ config .linkcheck_allowed_redirects = allowed_redirects
769+
770+
771+ _sentinel_lar = object ()
765772
766773
767774def setup (app : Sphinx ) -> ExtensionMetadata :
@@ -772,7 +779,9 @@ def setup(app: Sphinx) -> ExtensionMetadata:
772779 app .add_config_value (
773780 'linkcheck_exclude_documents' , [], '' , types = frozenset ({list , tuple })
774781 )
775- app .add_config_value ('linkcheck_allowed_redirects' , {}, '' , types = frozenset ({dict }))
782+ app .add_config_value (
783+ 'linkcheck_allowed_redirects' , _sentinel_lar , '' , types = frozenset ({dict })
784+ )
776785 app .add_config_value ('linkcheck_auth' , [], '' , types = frozenset ({list , tuple }))
777786 app .add_config_value ('linkcheck_request_headers' , {}, '' , types = frozenset ({dict }))
778787 app .add_config_value ('linkcheck_retries' , 1 , '' , types = frozenset ({int }))
@@ -799,7 +808,8 @@ def setup(app: Sphinx) -> ExtensionMetadata:
799808
800809 app .add_event ('linkcheck-process-uri' )
801810
802- app .connect ('config-inited' , compile_linkcheck_allowed_redirects , priority = 800 )
811+ # priority 900 to happen after ``check_confval_types()``
812+ app .connect ('config-inited' , compile_linkcheck_allowed_redirects , priority = 900 )
803813
804814 # FIXME: Disable URL rewrite handler for github.com temporarily.
805815 # See: https://github.com/sphinx-doc/sphinx/issues/9435
0 commit comments