Skip to main content

Relink type hints in your Sphinx API

Project description

PDG role for Sphinx

PyPI package Supported Python versions BSD 3-Clause license Open in Visual Studio Code CI status Checked with mypy pre-commit.ci status Spelling checked code style: prettier Code style: black Ruff

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",
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphinx-api-relink-0.0.1.tar.gz (11.6 kB view hashes)

Uploaded Source

Built Distribution

sphinx_api_relink-0.0.1-py3-none-any.whl (5.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page