Skip to content

Commit 45dee4f

Browse files
Best Practices: Add note about object/Any (#1198)
Part of #851 Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 4bdf0db commit 45dee4f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

docs/source/best_practices.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,21 @@ No::
4444
Ergonomic Practices
4545
===================
4646

47-
Using `Any`
48-
-----------
47+
Using ``Any`` and ``object``
48+
----------------------------
4949

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

53+
If a function accepts every possible object as an argument, for example
54+
because it's only passed to ``str()``, use ``object`` instead of ``Any`` as
55+
type annotation. Similarly, if the return value of a callback is ignored,
56+
annotate it with ``object``::
57+
58+
def call_cb_if_int(cb: Callable[[int], object], o: object) -> None:
59+
if isinstance(o, int):
60+
cb(o)
61+
5362
Arguments and Return Types
5463
--------------------------
5564

0 commit comments

Comments
 (0)