Skip to content
Merged
Changes from 1 commit
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
22 changes: 15 additions & 7 deletions docs/guides/writing_stubs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -772,18 +772,26 @@ all type checkers::
Using ``Any`` and ``object``
----------------------------

When adding type hints, avoid using the ``Any`` type when possible. Reserve
the use of ``Any`` for when:
When adding type hints, avoid using the :ref:`Any` type when possible. Reserve
the use of :ref:`Any` for when:

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

Note that ``Any`` is not the correct type to use if you want to indicate
Note that :ref:`Any` is not the correct type to use if you want to indicate
that some function can accept literally anything: in those cases use
``object`` instead.

When using ``Any``, document the reason for using it in a comment. Ideally,
document what types could be used.
:class:`object` instead.

When using :ref:`Any`, document the reason for using it in a comment, unless the
reason is obvious. Ideally, document what types could be used. Obvious
reasons can include:

* Using :ref:`Any` as a type argument for a generic to say "any object of this
type is allowed", e.g. ``type[Any]``.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should be type[object].

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Maybe use Future[Any] as example? This is what caused me to submit this PR, after all.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, that's a better example. In general you need this for invariant generics; for covariant ones object is fine.

* Using ``dict[str, Any]`` or ``Mapping[str, Any]`` when the value types
depends on the keys. But consider using :ref:`TypedDict` or
``dict[str, Incomplete]`` (temporarily) when the keys of the dictionary are
fixed.

The ``Any`` Trick
-----------------
Expand Down