=================================
This extension allows you to use Python 3 type annotations for documenting module and class attributes and
function arguments and return types.
The extension also has support for cyclic imports needed due to type annotations.
The extension only works for Python 3.7 and higher and requires the use of ``from __future__ import annotations``.
Example:
.. code-block:: python
:caption: a_module.py
from __future__ import annotations
import typing as t
if t.TYPE_CHECKING:
import another_module
module_attribute: t.Optional[another_module.SomeClass] = None
'''An attribute of the module.'''
class AClass:
'''A class.'''
instance_attribute: int
'''An instance attribute.'''
class_attribute: float = 3.0
'''A class attribute.'''
def __init__(self, arg: t.Union[int, str]):
pass
def method(self) -> int:
'''A method.'''
pass
.. code-block:: python
:caption: another_module.py
from __future__ import annotations
import typing
if typing.TYPE_CHECKING:
import a_module
def function(arg: a_module.AClass):
'''A function.'''
pass
Installation and setup
----------------------
First, use pip to download and install the extension::
$ pip install sphinx_autodoc_future_annotations
Then, add the extension to your ``conf.py``:
.. code-block:: python
extensions = [
'sphinx.ext.autodoc',
'sphinx_autodoc_future_annotations',
]
How it works
------------
Cyclic imports needed due to type annotations are supported in the following way.
First, a hook is monkey-patched into ``sphinx.ext.autodoc`` to act when a module is imported.
This hook first imports the module normally.
Then it parses the module's code and finds any top-level ``if``\-statements that are conditional on
``TYPE_CHECKING`` from the ``typing`` module.
All statements inside such ``if``-statements are executed in the context of te module.
This makes the additional imports available to ``typing.get_type_hints``.
The extension listens to the ``autodoc-process-signature`` and ``autodoc-process-docstring``
Sphinx events. In the former, it strips the annotations from the function signature. In the latter,
it injects the appropriate ``:type argname:`` and ``:rtype:`` directives into the docstring.
The ``:type argname:`` and ``:rtype:`` directives are added if and only if no existing matching directive is found.
Empty ``:param argname:`` directives are added if no matching directive is found.
This allows to document the type of an argument without having to add trivial documentation on its use.
This extension does not currently have any configuration options.
Some of the code is adapted from `sphinx-autodoc-typehints`_.
.. _sphinx-autodoc-typehints: https://github.com/agronholm/sphinx-autodoc-typehints
Compatibility with sphinx.ext.napoleon
--------------------------------------
To use `sphinx.ext.napoleon`_ with sphinx_autodoc_future_annotations, make sure you load
`sphinx.ext.napoleon`_ first, **before** sphinx_autodoc_future_annotations.
.. _sphinx.ext.napoleon: http://www.sphinx-doc.org/en/stable/ext/napoleon.html
.. code-block:: python
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinx_autodoc_future_annotations',
]
Metadata
Release files for sphinx-autodoc-future-annotations 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sphinx_autodoc_future_annotations-0.1.1.tar.gz | 5.1 kB | Without provenance | Details for sphinx_autodoc_future_annotations-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sphinx_autodoc_future_annotations-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for sphinx_autodoc_future_annotations-0.1.1-py3-none-any.whl |
Total release size: 10.6 kB
Release files / sphinx_autodoc_future_annotations-0.1.1.tar.gz
| Download URL | sphinx_autodoc_future_annotations-0.1.1.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8ef8f4aed36aedd5364afea88f213c24819e5ff0be27ed38fd0a982451a73dfc
|
|
BLAKE2b-256 checksum How to use checksums |
970bf0970c690ed7f53f95e1b8fe535a962f986c738cfe8a02e9f99f4d3958c0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0
|
Release files / sphinx_autodoc_future_annotations-0.1.1-py3-none-any.whl
| Download URL | sphinx_autodoc_future_annotations-0.1.1-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d13871ef48c44ee355f775d01ad51ec971dd0b34209641e724cd1ead2ff560ac
|
|
BLAKE2b-256 checksum How to use checksums |
8d4eb85fa798b9e2486d43e6fc90605b4c335031997900350b163862443fd11c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0
|
Security for sphinx-autodoc-future-annotations 0.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.