Skip to content

Commit e9f787a

Browse files
committed
Add Type annotations developer guideline
1 parent 2adbd4f commit e9f787a

File tree

7 files changed

+233
-58
lines changed

7 files changed

+233
-58
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``.

mypy.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ exclude = (?x)(
1717
| ^setuptools/config/_validate_pyproject/ # Auto-generated
1818
| ^setuptools/tests/bdist_wheel_testdata/ # Duplicate module name
1919
)
20+
# Too many false-positives
21+
disable_error_code = overload-overlap
2022

2123
# Ignoring attr-defined because setuptools wraps a lot of distutils classes, adding new attributes,
2224
# w/o updating all the attributes and return types from the base classes for type-checkers to understand

0 commit comments

Comments
 (0)