Skip to main content

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

Project description

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.

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

py2annotate-1.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

py2annotate-1.1.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file py2annotate-1.1.0.tar.gz.

File metadata

  • Download URL: py2annotate-1.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for py2annotate-1.1.0.tar.gz
Algorithm Hash digest
SHA256 5f8de5604f874a06769e7e37bc203f5a50161bb34856fca453557fd5d3b47766
MD5 89ab19fd00aee5c5a62459b27b9229c9
BLAKE2b-256 31a882f0fb55f32d9f16e63d24165c77e0007a7c479ef7e0d6c3dedf2fb59cce

See more details on using hashes here.

File details

Details for the file py2annotate-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: py2annotate-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for py2annotate-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e87e32a87ec7b292950d0a670ab88664c0dd296b2c070568dbbf24bd6f0890b6
MD5 66a3c230f9bdd890d9af7f2e6e0078a1
BLAKE2b-256 36d3afeff6feb828c077bee041c55f2f6bdf593d8f65d264a511065978378488

See more details on using hashes here.

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