Skip to content

Commit 59de2cf

Browse files
committed
Add Type annotations developer guideline
1 parent 3accd5c commit 59de2cf

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

docs/development/developer-guide.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,35 @@ simple Python script ``tools/vendor.py``.
131131
To refresh the dependencies, run the following command::
132132

133133
$ tox -e vendor
134+
135+
----------------
136+
Type annotations
137+
----------------
138+
139+
Most standards and best practices are enforced by
140+
[Ruff](https://docs.astral.sh/ruff/rules/)'s ``ANN2``, ``FA``, ``PYI``, ``UP``
141+
and ``YTT`` rules.
142+
143+
Explicit return types have to be added for typed public functions whose
144+
parameters are *all* annotated. This is enforced by ``ANN2``, but it's worth noting
145+
that this is due to mypy inferring ``Any`` even for simple return types. Mypy also
146+
doesn't count functions with missing parameter annotations as "typed". (see
147+
[python/mypy#4409](https://github.com/python/mypy/issues/4409),
148+
[python/mypy#10149](https://github.com/python/mypy/issues/10149) and
149+
[python/mypy#6646](https://github.com/python/mypy/issues/6646)).
150+
Otherwise, return annotations can be omitted to reduce verbosity,
151+
especially for complex return types.
152+
153+
Instead of typing an explicit return type annotation as
154+
``Generator[..., None, None]``, we'll prefer using an ``Iterator`` as it is more
155+
concise and conceptually easier to deal with. Returning a ``Generator`` with no
156+
``yield`` type or ``send`` type can sometimes be considered as exposing
157+
implementation details. See
158+
[Y058](https://github.com/PyCQA/flake8-pyi/blob/main/ERRORCODES.md#Y058).
159+
160+
Avoid importing private type-checking-only symbols. These are often
161+
[typeshed](https://github.com/python/typeshed) internal details and are not
162+
guaranteed to be stable.
163+
Importing from ``_typeshed`` or ``typing_extensions`` is fine, but if you find
164+
yourself importing the same symbol in ``TYPE_CHECKING`` blocks a lot, consider
165+
implementing an alias directly in ``setuptools``.

0 commit comments

Comments
 (0)