Skip to content

Commit

Permalink
Best Practices: Add note about object/Any (#1198)
Browse files Browse the repository at this point in the history
Part of #851

Co-authored-by: Jelle Zijlstra <[email protected]>
  • Loading branch information
srittau and JelleZijlstra authored Jun 2, 2022
1 parent 4bdf0db commit 45dee4f
Showing 1 changed file with 11 additions and 2 deletions.
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

0 comments on commit 45dee4f

Please sign in to comment.