Skip to main content

An extension to Sphinx autodoc to augment Sphinx documentation with type annotations, when using Python 2 style type annotations.

The Problem

Python 2 doesn’t support type annotations. Thus writing Python 2 / Python 3 agnostic code with type annotations requires putting the type annotations in comments:

def add(x, y):
    # type: (int, int) -> int
    """Adds two numbers."""
    return x + y

But now when documentation is generated with Sphinx, the documentation doesn’t include type annotations:

https://raw.githubusercontent.com/patrick-kidger/py2annotate/master/imgs/without-annotations.png

Example from the Signatory project.

The Solution

This extension remedies things so that the Sphinx documentation now looks like:

https://raw.githubusercontent.com/patrick-kidger/py2annotate/master/imgs/with-annotations.png

(which is the same as what you’d get using Python 3 style type annotations e.g. def add(x: int, y: int) -> int)

Installation

Via pip:

pip install py2annotate

It’s also just a single file, so copy-paste the code if you want.

Requires Python 3.6+.

Usage

Just add py2annotate to the list of extensions in conf.py:

# conf.py
...
extensions = ['sphinx.ext.autodoc', 'py2annotate']
...

(It shouldn’t matter whether it comes before or after autodoc.)

Note that Sphinx itself must be run using Python 3.6+. This is because py2annotate uses the Python 3 style type annotations internally in order to determine the correct annotations.

Known Issues

None so far! File a report if you run into anything. mypy and stubgen are used internally though, so your code’s formatting must be in a manner that they understand.

Metadata

Release files for py2annotate 1.1.1

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

Built distribution (wheel)

Table of built distributions (wheels) for py2annotate 1.1.1
File Interpreter ABI Platform Actions
py2annotate-1.1.1-py3-none-any.whl Python 3 none any Details for py2annotate-1.1.1-py3-none-any.whl

Release files / py2annotate-1.1.1-py3-none-any.whl

Download URL py2annotate-1.1.1-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d8762e5fb655e7e14332869d09113ef62c22930755989c91ad827334b1314dc4
BLAKE2b-256 checksum
How to use checksums
12510b3f5d6859bbe9011d2922a2273445d52fd2722cbc3886d5d61a79716554
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.2.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.6

Security for py2annotate 1.1.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.

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