Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Type annotations developer guideline #4397

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docs/development/developer-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,35 @@ simple Python script ``tools/vendor.py``.
To refresh the dependencies, run the following command::

$ tox -e vendor

----------------
Type annotations
----------------

Most standards and best practices are enforced by
`Ruff <https://docs.astral.sh/ruff/rules/>`_'s ``ANN2``, ``FA``, ``PYI``, ``UP``
and ``YTT`` rules.

Explicit return types have to be added for typed public functions whose
parameters are *all* annotated. This is enforced by ``ANN2``, but it's worth noting
that this is due to mypy inferring ``Any`` even for simple return types. Mypy also
doesn't count functions with missing parameter annotations as "typed". (see
`python/mypy#4409 <https://github.com/python/mypy/issues/4409>`_,
`python/mypy#10149 <https://github.com/python/mypy/issues/10149>`_ and
`python/mypy#6646 <https://github.com/python/mypy/issues/6646>`_).
Otherwise, return annotations can be omitted to reduce verbosity,
especially for complex return types.

Instead of typing an explicit return type annotation as
``Generator[..., None, None]``, we'll prefer using an ``Iterator`` as it is more
concise and conceptually easier to deal with. Returning a ``Generator`` with no
``yield`` type or ``send`` type can sometimes be considered as exposing
implementation details. See
`Y058 <https://github.com/PyCQA/flake8-pyi/blob/main/ERRORCODES.md#Y058>`_.

Avoid importing private type-checking-only symbols. These are often
`typeshed <https://github.com/python/typeshed>`_ internal details and are not
guaranteed to be stable.
Importing from ``_typeshed`` or ``typing_extensions`` is fine, but if you find
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's annoying, I can't find how to create a newline w/o creating a new paragraph in RST.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah \n is threated in RST as a space and \n\n creates a new paragraph, no other option.

yourself importing the same symbol in ``TYPE_CHECKING`` blocks a lot, consider
implementing an alias directly in ``setuptools``.
Loading