Skip to main content

Enrich Sphinx documentation with Python type information

Project description

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.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

python_sphinx_doc-0.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_sphinx_doc-0.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file python_sphinx_doc-0.1.tar.gz.

File metadata

  • Download URL: python_sphinx_doc-0.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.5

File hashes

Hashes for python_sphinx_doc-0.1.tar.gz
Algorithm Hash digest
SHA256 4b985e57d3d77e970ee5b5d9b53e7f262f70c693ebe9e2aa629106adeeb4a6ed
MD5 5891d64c4e06f564f3af6778f9a4ad08
BLAKE2b-256 b00c05c20bb2bbad0ea214bab33b97ab71f69d4b5f33732efeb5ec2e61569e69

See more details on using hashes here.

File details

Details for the file python_sphinx_doc-0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_sphinx_doc-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 13f35bae42d39b92bf1de5578e38b49a74a2a58ed0d61e85a46ccec308fd9a3b
MD5 848f8bfa7998396b3ec91ef528e261ae
BLAKE2b-256 287ec38b4abefdd2d2231429a25d7cfab8bfec9fd2b9e92ef9267be7fbdb25e7

See more details on using hashes here.

Supported by

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