|
1 | 1 | # Typing syntax in docstrings |
2 | 2 |
|
3 | | -Docstub defines its own [grammar](../src/docstub/doctype.lark) to parse and transform type information in docstrings (doctypes) into valid Python type expressions. |
| 3 | +Docstub defines its own [grammar](../src/docstub/doctype.lark) to parse and transform type information in docstrings into valid Python type expressions. |
4 | 4 | This grammar fully supports [Python's conventional typing syntax](https://typing.python.org/en/latest/index.html). |
5 | | -So any type expression that is valid in Python, can be used in a docstrings as is. |
| 5 | +So any {term}`annotation expression` that is valid in Python, can be used in a docstrings as is. |
6 | 6 | In addition, docstub extends this syntax with several "natural language" expressions that are commonly used in the scientific Python ecosystem. |
7 | 7 |
|
8 | | -Docstrings should follow a form that is inspired by the NumPyDoc style: |
| 8 | +Docstrings should follow a form that is inspired by the [NumPyDoc style](https://numpydoc.readthedocs.io/en/latest/format.html): |
9 | 9 | ```none |
10 | | -Section name |
| 10 | +Section namew |
11 | 11 | ------------ |
12 | 12 | name : doctype, optional_info |
13 | 13 | Description. |
14 | 14 | ``` |
15 | 15 |
|
16 | 16 | - `name` might be the name of a parameter, attribute or similar. |
17 | | -- `doctype` contains the actual type information that will be transformed into a Python type (see also {term}`doctype`). |
| 17 | +- `doctype` contains the actual type information that will be transformed into an {term}`annotation expression`. |
| 18 | + Here you can use the "natural language" expressions that are documented below. |
18 | 19 | - `optional_info` is optional and captures anything after the first (top-level) comma. |
19 | 20 | It is useful to provide additional information for readers. |
20 | | - Its presence and content doesn't currently affect the generated {term}`annotation expression`. |
| 21 | + Its presence and content doesn't affect the generated {term}`annotation expression`. |
| 22 | + |
| 23 | +Combining multiple names that share a doctype and description is supported. |
| 24 | + For example `a, b : int` is equivalent to defining both separately. |
21 | 25 |
|
22 | 26 |
|
23 | 27 | ## Unions |
@@ -116,12 +120,13 @@ See [issue 47](https://github.com/scientific-python/docstub/issues/47) for more |
116 | 120 |
|
117 | 121 | ## reStructuredText role |
118 | 122 |
|
119 | | -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. |
120 | | -Docstub allows for this anywhere where a qualified name can be used. |
| 123 | +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). |
| 124 | +This is supported anywhere in where a {term}`type name` is used in a {term}`doctype`. |
121 | 125 |
|
122 | | -| Docstring type | Python type annotation | |
123 | | -|----------------------|------------------------| |
124 | | -| `` `X` `` | `X` | |
125 | | -| ``:ref:`X` `` | `X` | |
126 | | -| ``:class:`Y.X` `` | `Y.X` | |
127 | | -| ``:py:class:`Y.X` `` | `Y.X` | |
| 126 | +| Docstring type | Python type annotation | |
| 127 | +|-------------------------|------------------------| |
| 128 | +| `` `X` `` | `X` | |
| 129 | +| ``:ref:`X` `` | `X` | |
| 130 | +| ``:class:`X`[Y, ...] `` | `X[Y, ...]` | |
| 131 | +| ``:class:`Y.X` `` | `Y.X` | |
| 132 | +| ``:py:class:`Y.X` `` | `Y.X` | |
0 commit comments