Skip to content

Commit dbbfc06

Browse files
committed
Add recommendations on documenting generics.
1 parent 1f76c5d commit dbbfc06

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

stack/documenting-code-with-type-annotations.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,35 @@ Their docstrings should continue to include the types parenthetically::
111111
included in documentation *at all*, except for those on `~dataclasses.dataclass` types.
112112
Important instance attributes that cannot have a class-level default value should be made into properties so they can be documented.
113113

114+
Generics
115+
--------
116+
117+
Functions that use `generics`_ will appear in the documentation with the type variable as the type, but these do not resolve to any kind of link, and in
118+
nitpick mode Sphinx will warn about those broken links.
119+
These should be included in the ``nitpick-ignore`` section of ``documenteer.toml``:
120+
121+
.. code-block:: toml
122+
[sphinx]
123+
nitpick_ignore = [
124+
["py:class", "T"], # type variables don't resolve
125+
]
126+
127+
for e.g.::
128+
129+
def generic[T: int | float](value: T) -> T:
130+
"""Do something with a value.
131+
132+
Parameters
133+
----------
134+
value
135+
The given value (a `float` or `int`).
136+
"""
137+
138+
Since the automatic docstrings do not include any information about a bound on the type variable (i.e. ``int | float`` here), including that information in
139+
the description is recommended.
114140

115141
.. _`Documenteer 2.x`: https://documenteer.lsst.io
116142
.. _`sphinx-autodoc-typehints`: https://pypi.org/project/sphinx-autodoc-typehints/
117143
.. _`pipelines.lsst.io`: https://pipelines.lsst.io
118144
.. _`Sphinx target-resolution rules`: <https://www.sphinx-doc.org/en/master/usage/domains/python.html#target-resolution>`
145+
.. _`generics`: <https://docs.python.org/3/library/typing.html#generics>

0 commit comments

Comments
 (0)