Skip to main content

Enrich Sphinx documentation with Python type information

This extension to Sphinx autodoc enriches class member variable and function parameter lists with type information extracted from Python type annotations.

Refer to the Sphinx HTML output for a live demonstration.

Usage

  1. Ensure that you have type hints in all your classes, functions and methods.
  2. Add description to your classes, functions and methods as a doc-string.
  3. Use :param name: text to assign a description to member variables and function parameters.
  4. Register Processor to the events autodoc-process-docstring and autodoc-before-process-signature.
  5. Enjoy how type information is automatically injected in the doc-string on make html.

Minimalistic example

The following code shows how to hook Processor to the events autodoc-process-docstring and autodoc-before-process-signature in Sphinx's conf.py:

from sphinx.application import Sphinx
from sphinx_doc.autodoc import Processor, include_special

def setup(app: Sphinx) -> None:
    processor = Processor()
    app.connect("autodoc-process-docstring", processor.process_docstring)
    app.connect("autodoc-before-process-signature", processor.process_signature)
    app.connect("autodoc-skip-member", include_special)

Refer to the published sample for a more detailed example how to use this extension with Sphinx.

Motivation

To pass type information to autodoc, you would normally be required to use the info field list items :param: and/or :type: with explicit type specification:

def send_message(
    sender: str,
    recipient: str,
    message_body: str,
    priority: int = 1,
) -> int:
    """
    :param str sender: The person sending the message.
    :param str recipient: The recipient of the message.
    :param str message_body: The body of the message.
    :param priority: The priority of the message, can be a number 1-5.
    :type priority: integer or None
    :return: The message identifier.
    :rtype: int
    """

However, a great deal of information is already present in the Python type signature. This extension promotes a more compact parameter definition whereby type information is injected automatically in documentation strings, and you only need to provide description text:

def send_message(
    sender: str,
    recipient: str,
    message_body: str,
    priority: int = 1,
) -> int:
    """
    :param sender: The person sending the message.
    :param recipient: The recipient of the message.
    :param message_body: The body of the message.
    :param priority: The priority of the message, can be a number 1-5.
    :returns: The message identifier.
    """

Features

  • Data-class member variables are published if they have a corresponding :param ...: in the class-level doc-string.
  • All enumeration members are published, even if they lack a description.
  • Magic methods (e.g. __eq__) are published if they have a doc-string.
  • Multi-line code blocks in doc-strings are converted to syntax-highlighted monospace text.
  • Primary keys in entity classes have extra visuals (e.g. with a symbol 🔑). See pysqlsync for how to define an entity class (using the @dataclass syntax) with a primary key (with the type hint PrimaryKey[T]).
  • Type aliases are substituted even if Postponed Evaluation of Annotations (PEP 563) is turned off.

Metadata

Release files for python-sphinx-doc 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-sphinx-doc 0.1
File Size Uploaded Provenance Actions
python_sphinx_doc-0.1.tar.gz 8.1 kB Without provenance Details for python_sphinx_doc-0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python-sphinx-doc 0.1
File Interpreter ABI Platform Actions
python_sphinx_doc-0.1-py3-none-any.whl Python 3 none any Details for python_sphinx_doc-0.1-py3-none-any.whl

Total release size: 16.5 kB

Release files / python_sphinx_doc-0.1.tar.gz

Download URL python_sphinx_doc-0.1.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
4b985e57d3d77e970ee5b5d9b53e7f262f70c693ebe9e2aa629106adeeb4a6ed
BLAKE2b-256 checksum
How to use checksums
b00c05c20bb2bbad0ea214bab33b97ab71f69d4b5f33732efeb5ec2e61569e69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.5

Release files / python_sphinx_doc-0.1-py3-none-any.whl

Download URL python_sphinx_doc-0.1-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
13f35bae42d39b92bf1de5578e38b49a74a2a58ed0d61e85a46ccec308fd9a3b
BLAKE2b-256 checksum
How to use checksums
287ec38b4abefdd2d2231429a25d7cfab8bfec9fd2b9e92ef9267be7fbdb25e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.5

Security for python-sphinx-doc 0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page