@@ -71,6 +71,15 @@ class _Status(StrEnum):
7171DEFAULT_DELAY = 60.0
7272
7373
74+ @object .__new__
75+ class _SENTINEL_LAR :
76+ def __repr__ (self ) -> str :
77+ return '_SENTINEL_LAR'
78+
79+ def __reduce__ (self ) -> str :
80+ return self .__class__ .__name__
81+
82+
7483class CheckExternalLinksBuilder (DummyBuilder ):
7584 """Checks for broken external links."""
7685
@@ -179,7 +188,7 @@ def process_result(self, result: CheckResult) -> None:
179188 text = 'with unknown code'
180189 linkstat ['text' ] = text
181190 redirection = f'{ text } to { result .message } '
182- if self .config .linkcheck_allowed_redirects is not None :
191+ if self .config .linkcheck_allowed_redirects is not _SENTINEL_LAR :
183192 msg = f'redirect { res_uri } - { redirection } '
184193 logger .warning (msg , location = (result .docname , result .lineno ))
185194 else :
@@ -387,7 +396,7 @@ def __init__(
387396 )
388397 self .check_anchors : bool = config .linkcheck_anchors
389398 self .allowed_redirects : dict [re .Pattern [str ], re .Pattern [str ]]
390- self .allowed_redirects = config .linkcheck_allowed_redirects or {}
399+ self .allowed_redirects = config .linkcheck_allowed_redirects
391400 self .retries : int = config .linkcheck_retries
392401 self .rate_limit_timeout = config .linkcheck_rate_limit_timeout
393402 self ._allow_unauthorized = config .linkcheck_allow_unauthorized
@@ -722,6 +731,8 @@ def handle_starttag(self, tag: Any, attrs: Any) -> None:
722731def _allowed_redirect (
723732 url : str , new_url : str , allowed_redirects : dict [re .Pattern [str ], re .Pattern [str ]]
724733) -> bool :
734+ if allowed_redirects is _SENTINEL_LAR :
735+ return False
725736 return any (
726737 from_url .match (url ) and to_url .match (new_url )
727738 for from_url , to_url in allowed_redirects .items ()
@@ -750,8 +761,7 @@ def rewrite_github_anchor(app: Sphinx, uri: str) -> str | None:
750761
751762def compile_linkcheck_allowed_redirects (app : Sphinx , config : Config ) -> None :
752763 """Compile patterns to the regexp objects."""
753- if config .linkcheck_allowed_redirects is _sentinel_lar :
754- config .linkcheck_allowed_redirects = None
764+ if config .linkcheck_allowed_redirects is _SENTINEL_LAR :
755765 return
756766 if not isinstance (config .linkcheck_allowed_redirects , dict ):
757767 msg = __ (
@@ -772,9 +782,6 @@ def compile_linkcheck_allowed_redirects(app: Sphinx, config: Config) -> None:
772782 config .linkcheck_allowed_redirects = allowed_redirects
773783
774784
775- _sentinel_lar = object ()
776-
777-
778785def setup (app : Sphinx ) -> ExtensionMetadata :
779786 app .add_builder (CheckExternalLinksBuilder )
780787 app .add_post_transform (HyperlinkCollector )
@@ -784,7 +791,7 @@ def setup(app: Sphinx) -> ExtensionMetadata:
784791 'linkcheck_exclude_documents' , [], '' , types = frozenset ({list , tuple })
785792 )
786793 app .add_config_value (
787- 'linkcheck_allowed_redirects' , _sentinel_lar , '' , types = frozenset ({dict })
794+ 'linkcheck_allowed_redirects' , _SENTINEL_LAR , '' , types = frozenset ({dict })
788795 )
789796 app .add_config_value ('linkcheck_auth' , [], '' , types = frozenset ({list , tuple }))
790797 app .add_config_value ('linkcheck_request_headers' , {}, '' , types = frozenset ({dict }))
0 commit comments