2023-08-28 15:58:03 -04:00
|
|
|
PEP: 727
|
2023-10-03 09:01:11 -04:00
|
|
|
Title: Documentation in Annotated Metadata
|
2023-08-28 15:58:03 -04:00
|
|
|
Author: Sebastián Ramírez <tiangolo@gmail.com>
|
|
|
|
Sponsor: Jelle Zijlstra <jelle.zijlstra@gmail.com>
|
2023-09-08 01:48:58 -04:00
|
|
|
Discussions-To: https://discuss.python.org/t/32566
|
2023-08-28 15:58:03 -04:00
|
|
|
Status: Draft
|
|
|
|
Type: Standards Track
|
|
|
|
Topic: Typing
|
|
|
|
Content-Type: text/x-rst
|
|
|
|
Created: 28-Aug-2023
|
|
|
|
Python-Version: 3.13
|
2023-09-08 01:48:58 -04:00
|
|
|
Post-History: `30-Aug-2023 <https://discuss.python.org/t/32566>`__
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
This PEP proposes a standardized way to provide documentation strings for Python
|
|
|
|
symbols defined with :py:class:`~typing.Annotated` using a new class
|
|
|
|
``typing.Doc``.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
Motivation
|
|
|
|
==========
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
There's already a well-defined way to provide documentation for classes,
|
|
|
|
functions, class methods, and modules: using docstrings.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Currently there is no formalized standard to provide documentation strings for other
|
|
|
|
types of symbols: parameters, return values, class-scoped variables (class variables
|
|
|
|
and instance variables), local variables, and type aliases.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Nevertheless, to allow documenting most of these additional symbols, several
|
|
|
|
conventions have been created as microsyntaxes inside of docstrings, and are
|
|
|
|
currently commonly used: Sphinx, numpydoc, Google, Keras, etc.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
There are two scenarios in which these conventions would be supported by tools: for
|
|
|
|
authors, while **editing** the contents of the documentation strings and for users,
|
|
|
|
while **rendering** that content in some way (in documentation sites, tooltips
|
|
|
|
in editors, etc).
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Because each of these conventions uses a microsyntax inside a string, when
|
|
|
|
**editing** those docstrings, editors can't easily provide support for autocompletion,
|
|
|
|
inline errors for broken syntax, etc. Any type of **editing** support for these
|
|
|
|
conventions would be on top of the support for editing standard Python syntax.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
When documenting parameters with current conventions, because the docstring is in
|
|
|
|
a different place in the code than the actual parameters and it requires
|
|
|
|
duplication of information (the parameter name) the information about
|
|
|
|
a parameter is easily in a place in the code quite far away from the
|
|
|
|
declaration of the actual parameter and it is disconnected from it.
|
|
|
|
This means it's easy to refactor a function, remove a parameter, and forget to
|
|
|
|
remove its docs. The same happens when adding a new parameter: it's easy to forget
|
|
|
|
to add the docstring for it.
|
2023-08-29 18:33:48 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
And because of this same duplication of information (the parameter name) editors and
|
|
|
|
other tools need complex custom logic to check or ensure the consistency of the
|
|
|
|
parameters in the signature and in their docstring, or they simply don't
|
|
|
|
fully support that.
|
|
|
|
|
|
|
|
As these existing conventions are different types of microsyntaxes inside of
|
|
|
|
strings, robustly parsing them for **rendering** requires complex logic that
|
|
|
|
needs to be implemented by the tools supporting them. Additionally, libraries
|
|
|
|
and tools don't have a straightforward way to obtain the documentation for
|
|
|
|
each individual parameter or variable at runtime, without depending on a
|
|
|
|
specific docstring convention parser. Accessing the parameter documentation
|
|
|
|
strings at runtime would be useful, for example, for testing the contents
|
|
|
|
of each parameter's documentation, to ensure consistency across several
|
|
|
|
similar functions, or to extract and expose that same parameter
|
|
|
|
documentation in some other way (e.g. an API with FastAPI, a CLI with Typer, etc).
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
Some of these previous formats tried to account for the lack of type annotations
|
2023-10-03 09:01:11 -04:00
|
|
|
in older Python versions by including typing information in the docstrings (e.g.
|
|
|
|
`Sphinx <https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#info-field-lists>`__,
|
|
|
|
`numpydoc <https://numpydoc.readthedocs.io/en/latest/format.html#parameters>`__)
|
2023-08-28 15:58:03 -04:00
|
|
|
but now that information doesn't need to be in docstrings as there is now an official
|
2023-09-08 01:48:58 -04:00
|
|
|
:pep:`syntax for type annotations <484>`.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
Rationale
|
|
|
|
=========
|
|
|
|
|
|
|
|
This proposal intends to address these shortcomings by extending and complementing the
|
2023-10-03 09:01:11 -04:00
|
|
|
information in docstrings, keeping backwards compatibility with existing docstrings
|
|
|
|
(it doesn't deprecate them), and doing it in a way that leverages the Python
|
|
|
|
language and structure, via type annotations with :py:class:`~typing.Annotated`, and
|
|
|
|
a new class ``Doc`` in ``typing``.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
The reason why this would belong in the standard Python library instead of an
|
|
|
|
external package is because although the implementation would be quite trivial,
|
2023-10-03 09:01:11 -04:00
|
|
|
the actual power and benefit from it would come from being a standard, to facilitate
|
|
|
|
its usage from library authors and to provide a default way to document Python
|
|
|
|
symbols using :py:class:`~typing.Annotated`. Some tool providers (at least VS Code
|
|
|
|
and PyCharm) have shown they would consider implementing support for this only if
|
|
|
|
it was a standard.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
This doesn't deprecate current usage of docstrings, docstrings should be considered
|
|
|
|
the preferred documentation method when available (not available in type aliases,
|
|
|
|
parameters, etc).
|
|
|
|
And docstrings would be complemented by this proposal for documentation specific to
|
|
|
|
the symbols that can be declared with :py:class:`~typing.Annotated`
|
|
|
|
(currently only covered by the several available microsyntax conventions).
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
This should be relatively transparent to common developers (library users) unless
|
|
|
|
they manually open the source files from libraries adopting it.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
It should be considered opt-in for library authors who would like to adopt it and
|
|
|
|
they should be free to decide to use it or not.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
It would be only useful for libraries that are willing to use optional type hints.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Summary
|
|
|
|
-------
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Here's a short summary of the features of this proposal in contrast to current
|
|
|
|
conventions:
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
* **Editing** would be already fully supported by default by any editor (current
|
|
|
|
or future) supporting Python syntax, including syntax errors, syntax
|
|
|
|
highlighting, etc.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
* **Rendering** would be relatively straightforward to implement by static tools
|
|
|
|
(tools that don't need runtime execution), as the information can be extracted
|
|
|
|
from the AST they normally already create.
|
|
|
|
|
|
|
|
* Deduplication of information: the name of a parameter would be defined in a single
|
|
|
|
place, not duplicated inside of a docstring.
|
|
|
|
|
|
|
|
* Elimination of the possibility of having inconsistencies when removing a parameter
|
|
|
|
or class variable and forgetting to remove its documentation.
|
|
|
|
|
|
|
|
* Minimization of the probability of adding a new parameter or class variable
|
|
|
|
and forgetting to add its documentation.
|
|
|
|
|
|
|
|
* Elimination of the possibility of having inconsistencies between the name of a
|
|
|
|
parameter in the signature and the name in the docstring when it is renamed.
|
|
|
|
|
|
|
|
* Reuse of documentation for symbols used in multiple places via type aliases.
|
|
|
|
|
|
|
|
* Access to the documentation string for each symbol at runtime, including existing
|
|
|
|
(older) Python versions.
|
|
|
|
|
|
|
|
* No microsyntax to learn for newcomers, it's just Python syntax.
|
|
|
|
|
|
|
|
* Parameter documentation inheritance for functions captured
|
|
|
|
by :py:class:`~typing.ParamSpec`.
|
|
|
|
|
|
|
|
|
|
|
|
Specification
|
|
|
|
=============
|
|
|
|
|
|
|
|
The main proposal is to introduce a new class, ``typing.Doc``.
|
|
|
|
This class should only be used within :py:class:`~typing.Annotated` annotations.
|
|
|
|
It takes a single positional-only string argument. It should be used to
|
|
|
|
document the intended meaning and use of the symbol declared using
|
|
|
|
:py:class:`~typing.Annotated`.
|
|
|
|
|
|
|
|
For example:
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-09-08 01:48:58 -04:00
|
|
|
.. code:: python
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
from typing import Annotated, Doc
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-09-08 01:48:58 -04:00
|
|
|
class User:
|
2023-10-12 10:16:44 -04:00
|
|
|
name: Annotated[str, Doc("The user's name")]
|
|
|
|
age: Annotated[int, Doc("The user's age")]
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-09-08 01:48:58 -04:00
|
|
|
...
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
:py:class:`~typing.Annotated` is normally used as a type annotation, in those cases,
|
|
|
|
any ``typing.Doc`` inside of it would document the symbol being annotated.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
When :py:class:`~typing.Annotated` is used to declare a type alias and that type
|
|
|
|
alias is used in an annotation, ``typing.Doc`` would document the symbol being
|
|
|
|
annotated instead of exclusively the type alias.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
For example:
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
.. code:: python
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
from typing import Annotated, Doc, TypeAlias
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
UserName: TypeAlias = Annotated[str, Doc("The user's name")]
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
def create_user(name: UserName): ...
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
def delete_user(name: UserName): ...
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
When a type alias is put inside of :py:class:`~typing.Annotated` and it has a
|
|
|
|
``typing.Doc``, the last one used (the top-most) takes precedence, this allows
|
|
|
|
overriding the documentation.
|
|
|
|
|
|
|
|
For example:
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-09-08 01:48:58 -04:00
|
|
|
.. code:: python
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
from typing import Annotated, Doc, TypeAlias
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
UserName: TypeAlias = Annotated[str, Doc("The user's name")]
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
def create_user(name: UserName): ...
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
def delete_user(name: Annotated[UserName, Doc("The user to delete")]): ...
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
In this case, for the ``name`` parameter in ``delete_user()``, the documentation string
|
|
|
|
would be ``"The user to delete"``.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
For tools extracting the information at runtime, they would normally use
|
|
|
|
:py:func:`~typing.get_type_hints` with the parameter ``include_extras=True``,
|
|
|
|
and as :py:class:`~typing.Annotated` is normalized (even with type aliases), this
|
|
|
|
would mean they should use the last ``typing.Doc`` available, as that is the last
|
|
|
|
one used.
|
|
|
|
|
|
|
|
At runtime, ``typing.Doc`` instances have an attribute ``documentation`` with the
|
|
|
|
string passed to it.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
When a type alias is used on its own, without annotating any additional symbol,
|
|
|
|
``typing.Doc`` documents the type alias itself. This would be useful if the
|
|
|
|
type alias is included on its own in documentation systems or if it's used directly
|
|
|
|
in some way, to show tooltips in editors.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
When a function's signature is captured by a :py:class:`~typing.ParamSpec`,
|
|
|
|
any documentation strings associated with the parameters should be retained.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Any tool processing ``typing.Doc`` objects should interpret the string as
|
|
|
|
a docstring, and therefore should normalize whitespace
|
|
|
|
as if ``inspect.cleandoc()`` were used.
|
|
|
|
|
|
|
|
The string passed to ``typing.Doc`` should be of the form that would be a
|
|
|
|
valid docstring.
|
|
|
|
This means that `f-strings`__ and string operations should not be used.
|
|
|
|
As this cannot be enforced by the Python runtime,
|
|
|
|
tools should not rely on this behavior.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
When tools providing **rendering** show the raw signature, they could allow
|
|
|
|
configuring if the whole raw :py:class:`~typing.Annotated` code should be displayed,
|
|
|
|
but they should default to not include :py:class:`~typing.Annotated` and its
|
|
|
|
internal code metadata, only the type of the symbols annotated. When those tools
|
|
|
|
support ``typing.Doc`` and rendering in other ways than just a raw signature,
|
|
|
|
they should show the string value passed to ``typing.Doc`` in a convenient way that
|
|
|
|
shows the relation between the documented symbol and the documentation string.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Tools providing **rendering** could allow ways to configure where to show the
|
|
|
|
parameter documentation and the prose docstring in different ways. Otherwise, they
|
|
|
|
could simply show the prose docstring first and then the parameter documentation second.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
__ https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals
|
|
|
|
|
|
|
|
|
|
|
|
Examples
|
|
|
|
--------
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Class attributes may be documented:
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
.. code:: python
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
from typing import Annotated, Doc
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
class User:
|
2023-10-12 10:16:44 -04:00
|
|
|
name: Annotated[str, Doc("The user's name")]
|
|
|
|
age: Annotated[int, Doc("The user's age")]
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
...
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
As can function or method parameters and return values:
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-09-08 01:48:58 -04:00
|
|
|
.. code:: python
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
from typing import Annotated, Doc
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
def create_user(
|
2023-10-12 10:16:44 -04:00
|
|
|
name: Annotated[str, Doc("The user's name")],
|
|
|
|
age: Annotated[int, Doc("The user's age")],
|
2023-10-03 09:01:11 -04:00
|
|
|
cursor: DatabaseConnection | None = None,
|
|
|
|
) -> Annotated[User, Doc("The created user after saving in the database")]:
|
|
|
|
"""Create a new user in the system.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
It needs the database connection to be already initialized.
|
|
|
|
"""
|
|
|
|
pass
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Backwards Compatibility
|
|
|
|
=======================
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
This proposal is fully backwards compatible with existing code and it doesn't
|
|
|
|
deprecate existing usage of docstring conventions.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
For developers that wish to adopt it before it is available in the standard library,
|
|
|
|
or to support older versions of Python, they can use ``typing_extensions`` and
|
|
|
|
import and use ``Doc`` from there.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
For example:
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-09-08 01:48:58 -04:00
|
|
|
.. code:: python
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
from typing import Annotated
|
|
|
|
from typing_extensions import Doc
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
class User:
|
2023-10-12 10:16:44 -04:00
|
|
|
name: Annotated[str, Doc("The user's name")]
|
|
|
|
age: Annotated[int, Doc("The user's age")]
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
...
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Security Implications
|
|
|
|
=====================
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
There are no known security implications.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
How to Teach This
|
|
|
|
=================
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
The main mechanism of documentation should continue to be standard docstrings for
|
|
|
|
prose information, this applies to modules, classes, functions and methods.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
For authors that want to adopt this proposal to add more granularity, they can use
|
|
|
|
``typing.Doc`` inside of :py:class:`~typing.Annotated` annotations for the symbols
|
|
|
|
that support it.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Library authors that wish to adopt this proposal while keeping backwards compatibility
|
|
|
|
with older versions of Python should use ``typing_extensions.Doc`` instead of
|
|
|
|
``typing.Doc``.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Reference Implementation
|
|
|
|
========================
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
``typing.Doc`` is implemented equivalently to:
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-09-08 01:48:58 -04:00
|
|
|
.. code:: python
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
class Doc:
|
|
|
|
def __init__(self, documentation: str, /):
|
|
|
|
self.documentation = documentation
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
It has been implemented in the `typing_extensions`__ package.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
__ https://pypi.org/project/typing-extensions/
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Survey of Other languages
|
|
|
|
=========================
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Here's a short survey of how other languages document their symbols.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-09-08 01:48:58 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Java
|
|
|
|
----
|
2023-09-08 01:48:58 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Java functions and their parameters are documented with
|
|
|
|
`Javadoc <https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html>`__,
|
|
|
|
a special format for comments put on top of the function definition. This would be
|
|
|
|
similar to Python current docstring microsyntax conventions (but only one).
|
2023-09-08 01:48:58 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
For example:
|
2023-09-08 01:48:58 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
.. code:: java
|
2023-09-08 01:48:58 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
/**
|
|
|
|
* Returns an Image object that can then be painted on the screen.
|
|
|
|
* The url argument must specify an absolute <a href="#{@link}">{@link URL}</a>. The name
|
|
|
|
* argument is a specifier that is relative to the url argument.
|
|
|
|
* <p>
|
|
|
|
* This method always returns immediately, whether or not the
|
|
|
|
* image exists. When this applet attempts to draw the image on
|
|
|
|
* the screen, the data will be loaded. The graphics primitives
|
|
|
|
* that draw the image will incrementally paint on the screen.
|
|
|
|
*
|
|
|
|
* @param url an absolute URL giving the base location of the image
|
|
|
|
* @param name the location of the image, relative to the url argument
|
|
|
|
* @return the image at the specified URL
|
|
|
|
* @see Image
|
|
|
|
*/
|
|
|
|
public Image getImage(URL url, String name) {
|
|
|
|
try {
|
|
|
|
return getImage(new URL(url, name));
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JavaScript
|
|
|
|
----------
|
|
|
|
|
|
|
|
Both JavaScript and TypeScript use a similar system to Javadoc.
|
|
|
|
|
|
|
|
JavaScript uses `JSDoc <https://jsdoc.app/>`__.
|
2023-09-08 01:48:58 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code:: javascript
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a book.
|
|
|
|
* @constructor
|
|
|
|
* @param {string} title - The title of the book.
|
|
|
|
* @param {string} author - The author of the book.
|
|
|
|
*/
|
|
|
|
function Book(title, author) {
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeScript
|
|
|
|
----------
|
2023-09-08 01:48:58 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
TypeScript has
|
|
|
|
`its own JSDoc reference <https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html>`__
|
|
|
|
with some variations.
|
2023-09-08 01:48:58 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
For example:
|
2023-09-08 01:48:58 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
.. code:: typescript
|
2023-09-08 01:48:58 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
// Parameters may be declared in a variety of syntactic forms
|
|
|
|
/**
|
|
|
|
* @param {string} p1 - A string param.
|
|
|
|
* @param {string=} p2 - An optional param (Google Closure syntax)
|
|
|
|
* @param {string} [p3] - Another optional param (JSDoc syntax).
|
|
|
|
* @param {string} [p4="test"] - An optional param with a default value
|
|
|
|
* @returns {string} This is the result
|
|
|
|
*/
|
|
|
|
function stringsStringStrings(p1, p2, p3, p4) {
|
|
|
|
// TODO
|
|
|
|
}
|
2023-09-08 01:48:58 -04:00
|
|
|
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Rust
|
|
|
|
----
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Rust uses another similar variation of a microsyntax in
|
|
|
|
`Doc comments <https://doc.rust-lang.org/rust-by-example/meta/doc.html#doc-comments>`__.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
But it doesn't have a particular well defined microsyntax structure to denote what
|
|
|
|
documentation refers to what symbol/parameter other than what can be inferred from
|
|
|
|
the pure Markdown.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
For example:
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
.. code:: rust
|
|
|
|
|
|
|
|
#![crate_name = "doc"]
|
|
|
|
|
|
|
|
/// A human being is represented here
|
|
|
|
pub struct Person {
|
|
|
|
/// A person must have a name, no matter how much Juliet may hate it
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Person {
|
|
|
|
/// Returns a person with the name given them
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `name` - A string slice that holds the name of the person
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// // You can have rust code between fences inside the comments
|
|
|
|
/// // If you pass --test to `rustdoc`, it will even test it for you!
|
|
|
|
/// use doc::Person;
|
|
|
|
/// let person = Person::new("name");
|
|
|
|
/// ```
|
|
|
|
pub fn new(name: &str) -> Person {
|
|
|
|
Person {
|
|
|
|
name: name.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gives a friendly hello!
|
|
|
|
///
|
|
|
|
/// Says "Hello, [name](Person::name)" to the `Person` it is called on.
|
|
|
|
pub fn hello(& self) {
|
|
|
|
println!("Hello, {}!", self.name);
|
|
|
|
}
|
|
|
|
}
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
fn main() {
|
|
|
|
let john = Person::new("John");
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
john.hello();
|
|
|
|
}
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Go Lang
|
|
|
|
-------
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Go also uses a form of `Doc Comments <https://go.dev/doc/comment>`__.
|
|
|
|
|
|
|
|
It doesn't have a well defined microsyntax structure to denote what documentation
|
|
|
|
refers to which symbol/parameter, but parameters can be referenced by name without
|
|
|
|
any special syntax or marker, this also means that ordinary words that could appear
|
|
|
|
in the documentation text should be avoided as parameter names.
|
|
|
|
|
|
|
|
.. code:: go
|
|
|
|
|
|
|
|
package strconv
|
|
|
|
|
|
|
|
// Quote returns a double-quoted Go string literal representing s.
|
|
|
|
// The returned string uses Go escape sequences (\t, \n, \xFF, \u0100)
|
|
|
|
// for control characters and non-printable characters as defined by IsPrint.
|
|
|
|
func Quote(s string) string {
|
|
|
|
...
|
|
|
|
}
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
Rejected Ideas
|
|
|
|
==============
|
|
|
|
|
|
|
|
|
|
|
|
Standardize Current Docstrings
|
|
|
|
------------------------------
|
|
|
|
|
|
|
|
A possible alternative would be to support and try to push as a standard one of the
|
|
|
|
existing docstring formats. But that would only solve the standardization.
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
It wouldn't solve any of the other problems derived from using a microsyntax inside
|
|
|
|
of a docstring instead of pure Python syntax, the same as described above in
|
|
|
|
the **Rationale - Summary**.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
Extra Metadata and Decorator
|
|
|
|
----------------------------
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Some ideas before this proposal included having a function ``doc()`` instead of
|
|
|
|
the single class ``Doc`` with several parameters to indicate whether
|
2023-08-28 15:58:03 -04:00
|
|
|
an object is discouraged from use, what exceptions it may raise, etc.
|
|
|
|
To allow also deprecating functions and classes, it was also expected
|
|
|
|
that ``doc()`` could be used as a decorator. But this functionality is covered
|
|
|
|
by ``typing.deprecated()`` in :pep:`702`, so it was dropped from this proposal.
|
|
|
|
|
|
|
|
A way to declare additional information could still be useful in the future,
|
2023-10-03 09:01:11 -04:00
|
|
|
but taking early feedback on this idea, all that was postponed to future
|
2023-08-28 15:58:03 -04:00
|
|
|
proposals.
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
This also shifted the focus from an all-encompassing function ``doc()``
|
|
|
|
with multiple parameters to a single ``Doc`` class to be used in
|
|
|
|
:py:class:`~typing.Annotated` in a way that could be composed with other
|
|
|
|
future proposals.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
This design change also allows better interoperability with other proposals
|
|
|
|
like ``typing.deprecated()``, as in the future it could be considered to
|
2023-10-03 09:01:11 -04:00
|
|
|
allow having ``typing.deprecated()`` also in :py:class:`~typing.Annotated` to deprecate
|
|
|
|
individual parameters, coexisting with ``Doc``.
|
|
|
|
|
|
|
|
|
|
|
|
String Under Definition
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
A proposed alternative in the discussion is declaring a string under the definition
|
|
|
|
of a symbol and providing runtime access to those values:
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
class User:
|
2023-10-12 10:16:44 -04:00
|
|
|
name: str
|
|
|
|
"The user's name"
|
|
|
|
age: int
|
|
|
|
"The user's age"
|
2023-10-03 09:01:11 -04:00
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
This was already proposed and rejected in :pep:`224`, mainly due to the ambiguity of
|
|
|
|
how is the string connected with the symbol it's documenting.
|
|
|
|
|
|
|
|
Additionally, there would be no way to provide runtime access to this value in previous
|
|
|
|
versions of Python.
|
|
|
|
|
|
|
|
|
|
|
|
Plain String in Annotated
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
In the discussion, it was also suggested to use a plain string inside of
|
|
|
|
:py:class:`~typing.Annotated`:
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
from typing import Annotated
|
|
|
|
|
|
|
|
class User:
|
2023-10-12 10:16:44 -04:00
|
|
|
name: Annotated[str, "The user's name"]
|
|
|
|
age: Annotated[int, "The user's age"]
|
2023-10-03 09:01:11 -04:00
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
But this would create a predefined meaning for any plain string inside of
|
|
|
|
:py:class:`~typing.Annotated`, and any tool that was using plain strings in them
|
|
|
|
for any other purpose, which is currently allowed, would now be invalid.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Having an explicit ``typing.Doc`` makes it compatible with current valid uses of
|
|
|
|
:py:class:`~typing.Annotated`.
|
|
|
|
|
|
|
|
|
|
|
|
Another Annotated-Like Type
|
|
|
|
---------------------------
|
|
|
|
|
|
|
|
In the discussion it was suggested to define a new type similar to
|
|
|
|
:py:class:`~typing.Annotated`, it would take the type and a parameter with the
|
|
|
|
documentation string:
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
from typing import Doc
|
|
|
|
|
|
|
|
class User:
|
2023-10-12 10:16:44 -04:00
|
|
|
name: Doc[str, "The user's name"]
|
|
|
|
age: Doc[int, "The user's age"]
|
2023-10-03 09:01:11 -04:00
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
This idea was rejected as it would only support that use case and would make it more
|
|
|
|
difficult to combine it with :py:class:`~typing.Annotated` for other purposes (
|
|
|
|
e.g. with FastAPI metadata, Pydantic fields, etc.) or adding additional metadata
|
|
|
|
apart from the documentation string (e.g. deprecation).
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
Open Issues
|
|
|
|
===========
|
|
|
|
|
|
|
|
|
|
|
|
Verbosity
|
|
|
|
---------
|
|
|
|
|
|
|
|
The main argument against this would be the increased verbosity.
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
If the signature was not viewed independently of the documentation and the body of the
|
|
|
|
function with the docstring was also measured, the total verbosity would be
|
|
|
|
somewhat similar, as what this proposal does is to move some of the contents
|
|
|
|
from the docstring in the body to the signature.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Considering the signature alone, without the body, they could be much longer than
|
|
|
|
they currently are, they could end up being more than one page long. In exchange,
|
|
|
|
the equivalent docstrings that currently are more than one page long would be
|
|
|
|
much shorter.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
When comparing the total verbosity, including the signature and the docstring,
|
|
|
|
the main additional verbosity added by this would be from using
|
|
|
|
:py:class:`~typing.Annotated` and ``typing.Doc``. If :py:class:`~typing.Annotated`
|
|
|
|
had more usage, it could make sense to have an improved shorter syntax for it and for
|
|
|
|
the type of metadata it would carry. But that would only make sense once
|
|
|
|
:py:class:`~typing.Annotated` is more widely used.
|
|
|
|
|
|
|
|
On the other hand, this verbosity would not affect end users as they would not see the
|
|
|
|
internal code using ``typing.Doc``. The majority of users would interact with
|
|
|
|
libraries through editors without looking at the internals, and if anything, they
|
|
|
|
would have tooltips from editors supporting this proposal.
|
|
|
|
|
|
|
|
The cost of dealing with the additional verbosity would mainly be carried
|
|
|
|
by those library maintainers that use this feature.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
This argument could be analogous to the argument against type annotations
|
|
|
|
in general, as they do indeed increase verbosity, in exchange for their
|
|
|
|
features. But again, as with type annotations, this would be optional and only
|
|
|
|
to be used by those that are willing to take the extra verbosity in exchange
|
|
|
|
for the benefits.
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Additionally, if type aliases were used, documentation could be put outside of the
|
|
|
|
signature and the docstring, reducing the total verbosity of the signature and the
|
|
|
|
function body.
|
|
|
|
|
|
|
|
Of course, more advanced users might want to look at the source code of the libraries
|
|
|
|
and if the authors of those libraries adopted this, those advanced users would end up
|
|
|
|
having to look at that code with additional signature verbosity instead of docstring
|
|
|
|
verbosity.
|
|
|
|
|
|
|
|
Any authors that decide not to adopt it should be free to continue using docstrings
|
|
|
|
with any particular format they decide, no docstrings at all, etc.
|
|
|
|
|
|
|
|
Still, there's a high chance that library authors could receive pressure to
|
|
|
|
adopt this if it became the blessed solution.
|
|
|
|
|
2023-08-28 15:58:03 -04:00
|
|
|
|
2023-09-08 01:48:58 -04:00
|
|
|
Documentation is not Typing
|
|
|
|
---------------------------
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
It could also be argued that documentation is not really part of typing, or that
|
|
|
|
it should live in a different module. Or that this information should not be part
|
|
|
|
of the signature but live in another place (like the docstring).
|
|
|
|
|
|
|
|
Nevertheless, type annotations in Python could already be considered, by default,
|
2023-10-03 09:01:11 -04:00
|
|
|
additional metadata: they carry additional information about variables,
|
|
|
|
parameters, return types, and by default they don't have any runtime behavior. And
|
|
|
|
this proposal would add one more type of metadata to them.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
It could be argued that this proposal extends the type of information that
|
|
|
|
type annotations carry, the same way as :pep:`702` extends them to include
|
|
|
|
deprecation information.
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
:py:class:`~typing.Annotated` was added to the standard library precisely to
|
|
|
|
support adding additional metadata to the annotations, and as the new proposed
|
|
|
|
``Doc`` class is tightly coupled to :py:class:`~typing.Annotated`, it makes
|
|
|
|
sense for it to live in the same module. If :py:class:`~typing.Annotated` was moved
|
|
|
|
to another module, it would make sense to move ``Doc`` with it.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
Multiple Standards
|
|
|
|
------------------
|
|
|
|
|
|
|
|
Another argument against this would be that it would create another standard,
|
2023-10-03 09:01:11 -04:00
|
|
|
and that there are already several conventions for docstrings. It could
|
2023-08-28 15:58:03 -04:00
|
|
|
seem better to formalize one of the currently existing standards.
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
Nevertheless, as stated above, none of those conventions cover the general
|
2023-08-28 15:58:03 -04:00
|
|
|
drawbacks of a doctsring-based approach that this proposal solves naturally.
|
|
|
|
|
2023-10-03 09:01:11 -04:00
|
|
|
To see a list of the drawbacks of a docstring-based approach, see the section above
|
|
|
|
in the **Rationale - Summary**.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
In the same way, it can be seen that, in many cases, a new standard that
|
|
|
|
takes advantage of new features and solves several problems from previous
|
|
|
|
methods can be worth having. As is the case with the new ``pyproject.toml``,
|
|
|
|
``dataclass_transform``, the new typing pipe/union (``|``) operator, and other cases.
|
|
|
|
|
|
|
|
|
|
|
|
Adoption
|
|
|
|
--------
|
|
|
|
|
|
|
|
As this is a new standard proposal, it would only make sense if it had
|
|
|
|
interest from the community.
|
|
|
|
|
|
|
|
Fortunately there's already interest from several mainstream libraries
|
|
|
|
from several developers and teams, including FastAPI, Typer, SQLModel,
|
2023-10-03 09:01:11 -04:00
|
|
|
Asyncer (from the author of this proposal), Pydantic, Strawberry (GraphQL), and
|
|
|
|
others.
|
2023-08-28 15:58:03 -04:00
|
|
|
|
|
|
|
There's also interest and support from documentation tools, like
|
|
|
|
`mkdocstrings <https://github.com/mkdocstrings/mkdocstrings>`__, which added
|
|
|
|
support even for an earlier version of this proposal.
|
|
|
|
|
|
|
|
All the CPython core developers contacted for early feedback (at least 4) have
|
|
|
|
shown interest and support for this proposal.
|
|
|
|
|
|
|
|
Editor developers (VS Code and PyCharm) have shown some interest, while showing
|
2023-10-03 09:01:11 -04:00
|
|
|
concerns about the signature verbosity of the proposal, although not about the
|
2023-08-28 15:58:03 -04:00
|
|
|
implementation (which is what would affect them the most). And they have shown
|
|
|
|
they would consider adding support for this if it were to become an
|
|
|
|
official standard. In that case, they would only need to add support for
|
|
|
|
rendering, as support for editing, which is normally non-existing for
|
|
|
|
other standards, is already there, as they already support editing standard
|
|
|
|
Python syntax.
|
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
|
|
|
=========
|
|
|
|
|
|
|
|
This document is placed in the public domain or under the
|
|
|
|
CC0-1.0-Universal license, whichever is more permissive.
|