Skip to content

Commit 0b8bf31

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 4f30c17 + e8c9eaa commit 0b8bf31

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

conf.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from sphinx.transforms import SphinxTransform
3131
from docutils import nodes
3232
from sphinx import addnodes
33+
from sphinx.ext import intersphinx
3334

3435
tools_describe = str(pathlib.Path(__file__).parent / "tools/describe")
3536

@@ -441,7 +442,8 @@ def autoapi_prepare_jinja_env(jinja_env):
441442

442443
# Example configuration for intersphinx: refer to the Python standard library.
443444
intersphinx_mapping = {"cpython": ('https://docs.python.org/3/', None),
444-
"register": ('https://circuitpython.readthedocs.io/projects/register/en/latest/', None)}
445+
"register": ('https://circuitpython.readthedocs.io/projects/register/en/latest/', None),
446+
"typing": ('https://circuitpython.readthedocs.io/projects/adafruit-circuitpython-typing/en/latest/', None)}
445447

446448
# Adapted from sphinxcontrib-redirects
447449
from sphinx.builders import html as builders
@@ -483,6 +485,26 @@ def generate_redirects(app):
483485
with open(redirected_filename, 'w') as f:
484486
f.write(TEMPLATE % urllib.parse.quote(to_path, '#/'))
485487

488+
def adafruit_typing_workaround(app, env, node, contnode):
489+
# Sphinx marks a requesting node that uses circuitpython-typing
490+
# as looking for a "class" definition, but unfortunately
491+
# Sphinx doesn't recognize TypeAlias based types usefully from
492+
# the typing library.
493+
# (see: https://github.com/sphinx-doc/sphinx/issues/8934)
494+
# Instead, it categorizes these types as "data".
495+
# (see: python -m sphinx.ext.intersphinx \
496+
# https://docs.circuitpython.org/projects/adafruit-circuitpython-typing/en/latest/objects.inv)
497+
# This workaround traps missing references, checks if
498+
# they are likely to be in the circuitpython_typing package,
499+
# and changes the requesting type from "class" to "data" if
500+
# needed, and re-tries the reference resolver.
501+
ref = node.get("reftarget", None)
502+
if ref and ref.startswith("circuitpython_typing."):
503+
dtype = node.get("reftype", None)
504+
if dtype != "data":
505+
node.attributes.update({"reftype": "data"})
506+
return intersphinx.missing_reference(app, env, node, contnode)
507+
486508

487509
class CoreModuleTransform(SphinxTransform):
488510
default_priority = 870
@@ -519,4 +541,5 @@ def setup(app):
519541
app.add_js_file("filter.js")
520542
app.add_config_value('redirects_file', 'redirects', 'env')
521543
app.connect('builder-inited', generate_redirects)
544+
app.connect('missing-reference', adafruit_typing_workaround)
522545
app.add_transform(CoreModuleTransform)

0 commit comments

Comments
 (0)