Skip to content

Best Practices: Add note about object/Any #1198

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

Merged
merged 3 commits into from
Jun 2, 2022
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
13 changes: 11 additions & 2 deletions docs/source/best_practices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,21 @@ No::
Ergonomic Practices
===================

Using `Any`
-----------
Using ``Any`` and ``object``
----------------------------

Generally, use ``Any`` when a type cannot be expressed appropriately
with the current type system or using the correct type is unergonomic.

If a function accepts every possible object as an argument, for example
because it's only passed to ``str()``, use ``object`` instead of ``Any`` as
type annotation. Similarly, if the return value of a callback is ignored,
annotate it with ``object``::

def call_cb_if_int(cb: Callable[[int], object], o: object) -> None:
if isinstance(o, int):
cb(o)

Arguments and Return Types
--------------------------

Expand Down