You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: stack/documenting-code-with-type-annotations.rst
+26-2Lines changed: 26 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -74,8 +74,7 @@ A simple return type does not need backticks to create a link, but backticks may
74
74
As always, types in docstrings do *not* respect imports in the file, and instead are resolved using the `Sphinx target-resolution rules`_.
75
75
See :ref:`rst-python-link` for details.
76
76
77
-
Functions that return multiple values via a tuple should just have multiple
78
-
entries::
77
+
Functions that return multiple values via a tuple should just have multiple entries::
79
78
80
79
def return_pair() -> tuple[str, int]:
81
80
"""Return a pair.
@@ -111,8 +110,33 @@ Their docstrings should continue to include the types parenthetically::
111
110
included in documentation *at all*, except for those on `~dataclasses.dataclass` types.
112
111
Important instance attributes that cannot have a class-level default value should be made into properties so they can be documented.
113
112
113
+
Generics
114
+
--------
115
+
116
+
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 nitpick mode Sphinx will warn about those broken links.
117
+
These should be included in the ``nitpick-ignore`` section of ``documenteer.toml``:
118
+
119
+
.. code-block:: toml
120
+
[sphinx]
121
+
nitpick_ignore = [
122
+
["py:class", "T"], # type variables don't resolve
123
+
]
124
+
125
+
for e.g.::
126
+
127
+
def generic[T: int | float](value: T) -> T:
128
+
"""Do something with a value.
129
+
130
+
Parameters
131
+
----------
132
+
value
133
+
The given value (a `float` or `int`).
134
+
"""
135
+
136
+
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 the description is recommended.
0 commit comments