Skip to content

Commit ef75648

Browse files
authored
Add some exceptions when commenting Any (#2000)
1 parent ed97c5b commit ef75648

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

docs/guides/writing_stubs.rst

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -772,18 +772,26 @@ all type checkers::
772772
Using ``Any`` and ``object``
773773
----------------------------
774774

775-
When adding type hints, avoid using the ``Any`` type when possible. Reserve
776-
the use of ``Any`` for when:
775+
When adding type hints, avoid using the :ref:`Any` type when possible. Reserve
776+
the use of :ref:`Any` for when:
777777

778778
* the correct type cannot be expressed in the current type system; and
779779
* to avoid union returns (see above).
780780

781-
Note that ``Any`` is not the correct type to use if you want to indicate
781+
Note that :ref:`Any` is not the correct type to use if you want to indicate
782782
that some function can accept literally anything: in those cases use
783-
``object`` instead.
784-
785-
When using ``Any``, document the reason for using it in a comment. Ideally,
786-
document what types could be used.
783+
:class:`object` instead.
784+
785+
When using :ref:`Any`, document the reason for using it in a comment, unless the
786+
reason is obvious. Ideally, document what types could be used. Obvious
787+
reasons can include:
788+
789+
* Using :ref:`Any` as a type argument for a generic with invariant type variables
790+
to say "any object of this type is allowed", e.g. ``Future[Any]``.
791+
* Using ``dict[str, Any]`` or ``Mapping[str, Any]`` when the value types
792+
depends on the keys. But consider using :ref:`TypedDict` or
793+
``dict[str, Incomplete]`` (temporarily) when the keys of the dictionary are
794+
fixed.
787795

788796
The ``Any`` Trick
789797
-----------------

0 commit comments

Comments
 (0)