-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Warn on all redirects if linkcheck_allowed_redirects
is an empty dictionary
#13452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AA-Turner
merged 3 commits into
sphinx-doc:master
from
AA-Turner:linkcheck-warn-all-redirects
Mar 27, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
|
||
from sphinx._cli.util.colour import darkgray, darkgreen, purple, red, turquoise | ||
from sphinx.builders.dummy import DummyBuilder | ||
from sphinx.errors import ConfigError | ||
from sphinx.locale import __ | ||
from sphinx.transforms.post_transforms import SphinxPostTransform | ||
from sphinx.util import logging, requests | ||
|
@@ -178,7 +179,7 @@ def process_result(self, result: CheckResult) -> None: | |
text = 'with unknown code' | ||
linkstat['text'] = text | ||
redirection = f'{text} to {result.message}' | ||
if self.config.linkcheck_allowed_redirects: | ||
if self.config.linkcheck_allowed_redirects is not None: | ||
msg = f'redirect {res_uri} - {redirection}' | ||
logger.warning(msg, location=(result.docname, result.lineno)) | ||
else: | ||
|
@@ -386,7 +387,7 @@ def __init__( | |
) | ||
self.check_anchors: bool = config.linkcheck_anchors | ||
self.allowed_redirects: dict[re.Pattern[str], re.Pattern[str]] | ||
self.allowed_redirects = config.linkcheck_allowed_redirects | ||
self.allowed_redirects = config.linkcheck_allowed_redirects or {} | ||
self.retries: int = config.linkcheck_retries | ||
self.rate_limit_timeout = config.linkcheck_rate_limit_timeout | ||
self._allow_unauthorized = config.linkcheck_allow_unauthorized | ||
|
@@ -748,20 +749,26 @@ def rewrite_github_anchor(app: Sphinx, uri: str) -> str | None: | |
|
||
|
||
def compile_linkcheck_allowed_redirects(app: Sphinx, config: Config) -> None: | ||
"""Compile patterns in linkcheck_allowed_redirects to the regexp objects.""" | ||
linkcheck_allowed_redirects = app.config.linkcheck_allowed_redirects | ||
for url, pattern in list(linkcheck_allowed_redirects.items()): | ||
"""Compile patterns to the regexp objects.""" | ||
if config.linkcheck_allowed_redirects is _sentinel_lar: | ||
config.linkcheck_allowed_redirects = None | ||
return | ||
if not isinstance(config.linkcheck_allowed_redirects, dict): | ||
raise ConfigError | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing an explanatory message here? |
||
allowed_redirects = {} | ||
for url, pattern in config.linkcheck_allowed_redirects.items(): | ||
try: | ||
linkcheck_allowed_redirects[re.compile(url)] = re.compile(pattern) | ||
allowed_redirects[re.compile(url)] = re.compile(pattern) | ||
except re.error as exc: | ||
logger.warning( | ||
__('Failed to compile regex in linkcheck_allowed_redirects: %r %s'), | ||
exc.pattern, | ||
exc.msg, | ||
) | ||
finally: | ||
# Remove the original regexp-string | ||
linkcheck_allowed_redirects.pop(url) | ||
config.linkcheck_allowed_redirects = allowed_redirects | ||
|
||
|
||
_sentinel_lar = object() | ||
|
||
|
||
def setup(app: Sphinx) -> ExtensionMetadata: | ||
|
@@ -772,7 +779,9 @@ def setup(app: Sphinx) -> ExtensionMetadata: | |
app.add_config_value( | ||
'linkcheck_exclude_documents', [], '', types=frozenset({list, tuple}) | ||
) | ||
app.add_config_value('linkcheck_allowed_redirects', {}, '', types=frozenset({dict})) | ||
app.add_config_value( | ||
'linkcheck_allowed_redirects', _sentinel_lar, '', types=frozenset({dict}) | ||
) | ||
app.add_config_value('linkcheck_auth', [], '', types=frozenset({list, tuple})) | ||
app.add_config_value('linkcheck_request_headers', {}, '', types=frozenset({dict})) | ||
app.add_config_value('linkcheck_retries', 1, '', types=frozenset({int})) | ||
|
@@ -799,7 +808,8 @@ def setup(app: Sphinx) -> ExtensionMetadata: | |
|
||
app.add_event('linkcheck-process-uri') | ||
|
||
app.connect('config-inited', compile_linkcheck_allowed_redirects, priority=800) | ||
# priority 900 to happen after ``check_confval_types()`` | ||
app.connect('config-inited', compile_linkcheck_allowed_redirects, priority=900) | ||
|
||
# FIXME: Disable URL rewrite handler for github.com. temporarily. | ||
# See: https://github.com./sphinx-doc/sphinx/issues/9435 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line/diff is confusing without context -- but the way I understand this: this code is within the
_Status.REDIRECTED
case -- and we can only reach this part of the code if a disallowed redirect occurred, or if no filtering of allowed redirects is configured. So the conditional check here is a shortcut, simply confirming whether allowed redirects are configured or not; if it is configured, we should emit a warning.