|
| 1 | +# Typing syntax in docstrings |
| 2 | + |
| 3 | +> [!NOTE] In early development! |
| 4 | +> Expect bugs, missing features, and incomplete documentation. |
| 5 | +> Docstub is still evaluating which features it needs to support as the community gives feedback. |
| 6 | +> Several features are experimental and included to make adoption of docstub easier. |
| 7 | +> Long-term, some of these might be discouraged or removed as docstub matures. |
| 8 | +
|
| 9 | +Docstub defines its own [grammar](../src/docstub/doctype.lark) to parse and transform type information in docstrings into valid type annotations. |
| 10 | +This grammar fully supports [Python's conventional typing syntax](https://typing.python.org/en/latest/index.html). |
| 11 | +So any type annotation that is valid in Python, can be used in a docstrings as is. |
| 12 | +In addition, docstub extends this syntax with several "natural language" expressions that are commonly used in the scientific Python ecosystem. |
| 13 | + |
| 14 | +Docstrings are expected to follow the NumPyDoc style: |
| 15 | +``` |
| 16 | +Section name |
| 17 | +------------ |
| 18 | +name : annotation, optional, extra_info |
| 19 | + Description. |
| 20 | +``` |
| 21 | + |
| 22 | +- `name` might be the name of a parameter or attribute. |
| 23 | + Other sections like "Returns" or "Yields" are supported. |
| 24 | +- `annotation` the actual type information that will be transformed into the type annotation. |
| 25 | +- `optional` and `extra_info` can be appended to provide additional information. |
| 26 | + Their presence and content doesn't currently affect the resulting type annotation. |
| 27 | + |
| 28 | + |
| 29 | +## Unions |
| 30 | + |
| 31 | +In addition to Python's conventional shorthand `|` syntax for [union types](https://typing.python.org/en/latest/spec/concepts.html#union-types), you can use `or` to join types. |
| 32 | + |
| 33 | +| Docstring type | Python type annotation | |
| 34 | +|----------------|------------------------| |
| 35 | +| `X or Y` | `X \| Y` | |
| 36 | +| `int or float` | `int \| float` | |
| 37 | + |
| 38 | + |
| 39 | +## Containers |
| 40 | + |
| 41 | +The content of containers can be typed using a `CONTAINER of X` like form. |
| 42 | +This extends the basic subscription syntax for [generics](https://typing.python.org/en/latest/spec/generics.html#generics). |
| 43 | + |
| 44 | +| Docstring type | Python type annotation | |
| 45 | +|-------------------------|------------------------| |
| 46 | +| `CONTAINER of X` | `CONTAINER[X]` | |
| 47 | +| `CONTAINER of (X or Y)` | `CONTAINER[X \| Y]` | |
| 48 | + |
| 49 | +For the simple case `CONTAINER of X`, where `X` is a name, you can append `(s)` to indicate the plural form. |
| 50 | +E.g., `list of float(s)`. |
| 51 | + |
| 52 | +Variants of for [**tuples**](https://typing.python.org/en/latest/spec/tuples.html) |
| 53 | + |
| 54 | +| Docstring type | Python type annotation | |
| 55 | +|---------------------|------------------------| |
| 56 | +| `tuple of (X, Y)` | `tuple[X, Y]` | |
| 57 | +| `tuple of (X, ...)` | `tuple[X, ...]` | |
| 58 | + |
| 59 | +and **mappings** exist. |
| 60 | + |
| 61 | +| Docstring type | Python type annotation | |
| 62 | +|----------------------|------------------------| |
| 63 | +| `MAPPING of {X: Y}` | `MAPPING[X, Y]` | |
| 64 | +| `dict of {str: int}` | `dict[str, int]` | |
| 65 | + |
| 66 | + |
| 67 | +> [!TIP] |
| 68 | +> While it is possible to nest these variants repeatedly, it is discouraged to do so to keep type descriptions readable. |
| 69 | +> For complex annotations with nested containers, consider using Python's conventional syntax. |
| 70 | +> In the future, docstub may warn against or disallow nesting these natural language variants. |
| 71 | +
|
| 72 | + |
| 73 | +## Shape and dtype syntax for arrays |
| 74 | + |
| 75 | +This expression allows adding shape and datatype information for data structures like [NumPy arrays](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html). |
| 76 | + |
| 77 | +`array` and `ndarray`, and `array-like` and `array_like` can be used interchange-ably. |
| 78 | + |
| 79 | +| Docstring type | Python type annotation | |
| 80 | +|-----------------------------|------------------------| |
| 81 | +| `array of DTYPE` | `ndarray[DTYPE]` | |
| 82 | +| `ndarray of dtype DTYPE` | `ndarray[DTYPE]` | |
| 83 | +| `array-like of DTYPE` | `ArrayLike[DTYPE]` | |
| 84 | +| `array_like of dtype DTYPE` | `ArrayLike[DTYPE]` | |
| 85 | + |
| 86 | +> [!NOTE] |
| 87 | +> Noting the **shape** of an array in the docstring is supported. |
| 88 | +> However, Python's typing system is not yet able to express this information. |
| 89 | +> It is therefore not included in the resulting type annotation. |
| 90 | +
|
| 91 | +| Docstring type | Python type annotation | |
| 92 | +|--------------------------|------------------------| |
| 93 | +| `(3,) array of DTYPE` | `ndarray[DTYPE]` | |
| 94 | +| `(X, Y) array of DTYPE` | `ndarray[DTYPE]` | |
| 95 | +| `([P,] M, N) array-like` | `ArrayLike` | |
| 96 | +| `(M, ...) ndarray` | `ArrayLike` | |
| 97 | + |
| 98 | + |
| 99 | +## Literals |
| 100 | + |
| 101 | +[Literals](https://typing.python.org/en/latest/spec/literal.html#literals) indicate a concrete value instead of type. |
| 102 | +Instead of using [`typing.Literal`](https://docs.python.org/3/library/typing.html#typing.Literal), you can enclose literal values in `{...}` in docstrings. |
| 103 | + |
| 104 | +| Docstring type | Python type annotation | |
| 105 | +|----------------|------------------------| |
| 106 | +| `{1, 2, 3}` | `Literal[1, 2, 3]` | |
| 107 | +| `{1, 2, 3}` | `Literal[1, 2, 3]` | |
| 108 | + |
| 109 | +> [!TIP] |
| 110 | +> Enclosing a single value `{X}` is currently allowed but discouraged. |
| 111 | +> Instead consider the more explicit `Literal[X]`. |
| 112 | +
|
| 113 | + |
| 114 | +## reStructuredText role |
| 115 | + |
| 116 | +Since docstrings are also used to generate documentation with Sphinx, you may want to use [restructuredText roles](https://docutils.sourceforge.io/docs/ref/rst/roles.html) in your type annotations. |
| 117 | +Docstub allows for this anywhere where a qualified name can be used. |
| 118 | + |
| 119 | +| Docstring type | Python type annotation | |
| 120 | +|----------------------|------------------------| |
| 121 | +| `` `X` `` | `X` | |
| 122 | +| ``:ref:`X` `` | `X` | |
| 123 | +| ``:class:`Y.X` `` | `Y.X` | |
| 124 | +| ``:py:class:`Y.X` `` | `Y.X` | |
0 commit comments