| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import sphinx |
| 12 |
from sphinx.application import Sphinx |
| 13 |
from sphinx.builders.linkcheck import HyperlinkAvailabilityChecker |
| 14 |
|
| 15 |
# We want to customize the linkcheck behavior so that references from intersphinx |
| 16 |
# mappings are not checked. We use these liberally and don't want to spend CI time |
| 17 |
# checking their validity. If it's in an inventory, sphinx should guarantee |
| 18 |
# it's valid, sans very weird things happening. |
| 19 |
# |
| 20 |
# Sphinx splits the link check logic across a CheckExternalLinksBuilder builder |
| 21 |
# and a HyperlinkCollector post_transform (and a HyperlinkAvailabilityChecker |
| 22 |
# helper class). There are various points in each where we could add this |
| 23 |
# ignore-intersphinx hook. |
| 24 |
# |
| 25 |
# Monkey-patching HyperlinkAvailabilityChecker isn't great, but is the best way |
| 26 |
# I found to go about this. |
| 27 |
|
| 28 |
# set by on_builder_inited |
| 29 |
inventories = {} |
| 30 |
|
| 31 |
|
| 32 |
def is_intersphinx_link(uri): |
| 33 |
for inventory in inventories.values(): |
| 34 |
uris = {uri for _name, _version, uri, _display_name in inventory.values()} |
| 35 |
if uri in uris: |
| 36 |
return True |
| 37 |
return False |
| 38 |
|
| 39 |
|
| 40 |
class HypothesisLinkChecker(HyperlinkAvailabilityChecker): |
| 41 |
def is_ignored_uri(self, uri: str) -> bool: |
| 42 |
if is_intersphinx_link(uri): |
| 43 |
return True |
| 44 |
return super().is_ignored_uri(uri) |
| 45 |
|
| 46 |
|
| 47 |
sphinx.builders.linkcheck.HyperlinkAvailabilityChecker = HypothesisLinkChecker |
| 48 |
|
| 49 |
|
| 50 |
# Hook the builder to get access to the intersphinx inventory. app.env is not |
| 51 |
# available in setup() |
| 52 |
def on_builder_inited(app: Sphinx) -> None: |
| 53 |
global inventories |
| 54 |
inventories = getattr(app.env, "intersphinx_inventory", {}) |
| 55 |
|
| 56 |
|
| 57 |
def setup(app: Sphinx): |
| 58 |
app.connect("builder-inited", on_builder_inited) |