Relink type hints in your Sphinx API
Project description
sphinx-api-relink
This package is a plugin for the sphinx.ext.autodoc
extension. The autodoc_type_aliases
configuration does not always work well when using postponed evaluation of annotations (PEP 563, i.e. from __future__ import annotations
) or when importing through typing.TYPE_CHECKING
, because sphinx.ext.autodoc
generates the API dynamically (not statically, as opposed to sphinx-autoapi
).
Installation
Just install through PyPI with pip
:
pip install sphinx-api-relink
Next, in your Sphinx configuration file (conf.py
), add "sphinx_api_relink"
to your extensions
:
extensions = [
"sphinx_api_relink",
]
Usage
There are two ways to relink type hint references in your API. The first one, api_target_substitutions
, should be used when the target is different than the type hint itself:
api_target_substitutions: dict[str, str | tuple[str, str]] = {
"sp.Expr": "sympy.core.expr.Expr",
"Protocol": ("obj", "typing.Protocol"),
}
The second, api_target_types
, is useful when you want to redirect the reference type. This is for instance useful when Sphinx thinks the reference is a class
, but it should be an obj
:
api_target_types: dict[str, str] = {
"RangeDefinition": "obj",
}
The extension can also link to the source code on GitHub through the sphinx.ext.linkcode
extension. To activate, specify the GitHub organization and the repository name as follows:
api_github_repo: str = "ComPWA/sphinx-api-relink"
Set api_linkcode_debug = True
to print the generated URLs to the console.
Generate API
To generate the API for sphinx.ext.autodoc
, add this to your conf.py
:
generate_apidoc_package_path = "../src/my_package" # relative to conf.py
Multiple packages can be listed as well:
generate_apidoc_package_path = [
"../src/package1",
"../src/package2",
]
The API is generated with the same style used by the ComPWA repositories (see e.g. ampform.rtfd.io/en/stable/api/ampform.html). To use the default template, set:
generate_apidoc_use_compwa_template = False
Other configuration values (with their defaults):
generate_apidoc_directory = "api"
generate_apidoc_excludes = ["version.py"]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sphinx_api_relink-0.0.12.tar.gz
.
File metadata
- Download URL: sphinx_api_relink-0.0.12.tar.gz
- Upload date:
- Size: 18.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b995a6482e6bebc9aafd3194374acac2beff761f62ea2da87da35c53897b4410 |
|
MD5 | 6bbc180029cfb3a5b880ad59711992ed |
|
BLAKE2b-256 | e3e260ddb1b9fd51438d191e06048e82bb97101f00620c9cd7ab5b792e190494 |
File details
Details for the file sphinx_api_relink-0.0.12-py3-none-any.whl
.
File metadata
- Download URL: sphinx_api_relink-0.0.12-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 974aca39db9937e5d3bcab189d9e43d55e299a103dc734f8a963f8a76840735c |
|
MD5 | f6fa8dcfc378a236dd7f134ab187c13e |
|
BLAKE2b-256 | f5e32852dd608bb9b68cc2d1975e7fa1029e73a360d3b102c073edacc405a5a2 |