|
| 1 | +# Annotated Doc |
| 2 | + |
| 3 | +Document parameters, class attributes, return types, and variables inline, with `Annotated`. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +pip install annotated-doc |
| 9 | +``` |
| 10 | + |
| 11 | +Or with `uv`: |
| 12 | + |
| 13 | +```Python |
| 14 | +uv add annotated-doc |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +Import `Doc` and pass a single literal string with the documentation for the specific parameter, class attribute, return type, or variable. |
| 20 | + |
| 21 | +For example, to document a parameter `name` in a function `hi` you could do: |
| 22 | + |
| 23 | +```Python |
| 24 | +from typing import Annotated |
| 25 | + |
| 26 | +from annotated_doc import Doc |
| 27 | + |
| 28 | +def hi(name: Annotated[str, Doc("Who to say hi to")]) -> None: |
| 29 | + print(f"Hi, {name}!") |
| 30 | +``` |
| 31 | + |
| 32 | +You can also use it to document class attributes: |
| 33 | + |
| 34 | +```Python |
| 35 | +from typing import Annotated |
| 36 | + |
| 37 | +from annotated_doc import Doc |
| 38 | + |
| 39 | +class User: |
| 40 | + name: Annotated[str, Doc("The user's name")] |
| 41 | + age: Annotated[int, Doc("The user's age")] |
| 42 | +``` |
| 43 | + |
| 44 | +## Who Uses This |
| 45 | + |
| 46 | +This was made for: |
| 47 | + |
| 48 | +* [FastAPI](https://fastapi.tiangolo.com/) |
| 49 | +* [Typer](https://typer.tiangolo.com/) |
| 50 | +* [SQLModel](https://sqlmodel.tiangolo.com/) |
| 51 | +* [Asyncer](https://asyncer.tiangolo.com/) |
| 52 | + |
| 53 | +## Reasons not to use `annotated-doc` |
| 54 | + |
| 55 | +You are already comfortable with one of the existing docstring formats, like: |
| 56 | + |
| 57 | +* Sphinx |
| 58 | +* numpydoc |
| 59 | +* Google |
| 60 | +* Keras |
| 61 | + |
| 62 | +Your team is already comfortable using them. |
| 63 | + |
| 64 | +You prefer having the documentation about parameters all together in a docstring, separated from the code defining them. |
| 65 | + |
| 66 | +You care about a specific set of users, using one specific editor, and that editor already has support for the specific docstring format you use. |
| 67 | + |
| 68 | +## Reasons to use `annotated-doc` |
| 69 | + |
| 70 | +* No micro-syntax to learn for newcomers, it’s **just Python** syntax. |
| 71 | +* **Editing** would be already fully supported by default by any editor (current or future) supporting Python syntax, including syntax errors, syntax highlighting, etc. |
| 72 | +* **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. |
| 73 | +* **Deduplication of information**: the name of a parameter would be defined in a single place, not duplicated inside of a docstring. |
| 74 | +* **Elimination** of the possibility of having **inconsistencies** when removing a parameter or class variable and **forgetting to remove** its documentation. |
| 75 | +* **Minimization** of the probability of adding a new parameter or class variable and **forgetting to add its documentation**. |
| 76 | +* **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. |
| 77 | +* **Access** to the documentation string for each symbol at **runtime**, including existing (older) Python versions. |
| 78 | +* A more formalized way to document other symbols, like type aliases, that could use Annotated. |
| 79 | +* **Support** for apps using FastAPI, Typer and others. |
| 80 | +* **AI Accessibility**: AI tools will have an easier way understanding each parameter as the distance from documentation to parameter is much closer. |
| 81 | + |
| 82 | +## History |
| 83 | + |
| 84 | +I ([@tiangolo](https://github.com/tiangolo)) originally wanted for this to be part of the Python standard library (in [PEP 727](https://peps.python.org/pep-0727/)), but the proposal was withdrawn as there was a fair amount of negative feedback and opposition. |
| 85 | + |
| 86 | +The conclusion was that this was better done as an external effort, in a third-party library. |
| 87 | + |
| 88 | +So, here it is, with a simpler approach, as a third-party library, in a way that can be used by others, starting with FastAPI and friends, and its users. |
| 89 | + |
| 90 | +## License |
| 91 | + |
| 92 | +This project is licensed under the terms of the MIT license. |
0 commit comments