|
30 | 30 | from sphinx.transforms import SphinxTransform
|
31 | 31 | from docutils import nodes
|
32 | 32 | from sphinx import addnodes
|
| 33 | +from sphinx.ext import intersphinx |
33 | 34 |
|
34 | 35 | tools_describe = str(pathlib.Path(__file__).parent / "tools/describe")
|
35 | 36 |
|
@@ -441,7 +442,8 @@ def autoapi_prepare_jinja_env(jinja_env):
|
441 | 442 |
|
442 | 443 | # Example configuration for intersphinx: refer to the Python standard library.
|
443 | 444 | 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)} |
445 | 447 |
|
446 | 448 | # Adapted from sphinxcontrib-redirects
|
447 | 449 | from sphinx.builders import html as builders
|
@@ -483,6 +485,26 @@ def generate_redirects(app):
|
483 | 485 | with open(redirected_filename, 'w') as f:
|
484 | 486 | f.write(TEMPLATE % urllib.parse.quote(to_path, '#/'))
|
485 | 487 |
|
| 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 | + |
486 | 508 |
|
487 | 509 | class CoreModuleTransform(SphinxTransform):
|
488 | 510 | default_priority = 870
|
@@ -519,4 +541,5 @@ def setup(app):
|
519 | 541 | app.add_js_file("filter.js")
|
520 | 542 | app.add_config_value('redirects_file', 'redirects', 'env')
|
521 | 543 | app.connect('builder-inited', generate_redirects)
|
| 544 | + app.connect('missing-reference', adafruit_typing_workaround) |
522 | 545 | app.add_transform(CoreModuleTransform)
|
0 commit comments