Skip to content

Commit 60e7873

Browse files
committed
Add section on docs with type annotations.
1 parent 23ac4df commit 60e7873

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
.. _stack-documentation-code-with-type-hints:
2+
3+
################################
4+
Documenting code with type hints
5+
################################
6+
7+
Many DM packages (especially the middleware suite) use type hints for static analysis, which often duplicates the type information included in docstrings.
8+
Documentation built with `Documenteer 2.x`_ can often leave this information out, because the `sphinx-autodoc-typehints`_ extension (included automatically) will parse the annotations and include type information in the docs automatically.
9+
10+
.. note::
11+
`pipelines.lsst.io`_ is currently still built with Documenteer 1.x, but is expected to transition soon.
12+
While some :ref:`package doc builds <build-package-docs>` have already been upgraded in anticipation of this transition, their documentation content needs to remain compatible with Documenteer 1.x for now.
13+
14+
To document the parameters to a function or method declared with type hints,
15+
use regular numpydoc style without the colon or the type information that follows it::
16+
17+
def run_thing(self, x: int, *args: int, name: str = "", **kwargs: str) -> None:
18+
"""Run the thing.
19+
20+
Parameters
21+
----------
22+
x
23+
X coordinate.
24+
*args
25+
Some other coordinates.
26+
name
27+
The name of the thing.
28+
**kwargs
29+
Names of other things.
30+
"""
31+
32+
Note that ``, optional`` is also unnecessary, as are defaults; default values are automatically pulled from the real function signature.
33+
34+
Return types work automatically when they are not documented at all::
35+
36+
def return_it() -> str:
37+
"""Return the thing."""
38+
return ""
39+
40+
This is a reasonable approach when there is nothing else to document about the returned object and the return type is annotated.
41+
When the returned object does additional documentation, the type does unfortunately need to be written out (duplicating the annotation), but the returned object should not be named::
42+
43+
def return_it() -> str:
44+
"""Return the thing.
45+
46+
Returns
47+
-------
48+
str
49+
The thing.
50+
"""
51+
return ""
52+
53+
The return type does not need backticks to create a link, but backticks may be used, e.g. for a generic type:
54+
55+
from collections.abc import Sequence
56+
57+
def return_stuff() -> Sequence[str]:
58+
"""Return some stuff.
59+
60+
Returns
61+
-------
62+
`~collections.abc.Sequence` [`str`]
63+
The stuff.
64+
"""
65+
return []
66+
67+
.. note::
68+
As always, types in docstrings do *not* respect imports in the file, and instead are resolved using `Sphinx's own target-resolution rules <https://www.sphinx-doc.org/en/master/usage/domains/python.html#target-resolution>``__. See :ref:`rst-python-link` for details.
69+
70+
Functions that return multiple values via a tuple should have their return types documented just like parameters, i.e. with labels and no types::
71+
72+
def return_pair() -> tuple[str, int]:
73+
"""Return a pair.
74+
75+
Returns
76+
-------
77+
name
78+
The name.
79+
id
80+
The ID.
81+
"""
82+
return ("", 0)
83+
84+
.. _`Documenteer 2`: https://documenteer.lsst.io
85+
.. _`sphinx-autodoc-typehints`: https://pypi.org/project/sphinx-autodoc-typehints/
86+
.. _`pipelines.lsst.io`: https://pipelines.lsst.io

stack/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ DM Stack guides
1212
- :doc:`documentation-system-overview`
1313
- :doc:`layout-of-doc-directory`
1414
- :doc:`package-documentation-topic-types`
15+
- :doc:`documenting-code-with-type-annotations`
1516
- :doc:`add-a-package-to-pipelines-lsst-io`
1617
- :doc:`building-single-package-docs`
1718
- :doc:`building-pipelines-lsst-io-locally`

0 commit comments

Comments
 (0)