Skip to content

Commit 953e24d

Browse files
authored
Best Practices: Split an example (#2114)
Split the text and examples for `Any` vs `object` into one for arguments and one for callback return types. This separates these two related, but different suggestions. It should make the examples a bit clearer and less convoluted.
1 parent 89c74e3 commit 953e24d

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

docs/reference/best_practices.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,18 @@ with the current type system or using the correct type is unergonomic.
5252

5353
If a function accepts every possible object as an argument, for example
5454
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``::
55+
type annotation::
5756

58-
def call_cb_if_int(cb: Callable[[int], object], o: object) -> None:
57+
def print_formatted(o: object) -> None:
5958
if isinstance(o, int):
60-
cb(o)
59+
o = f"{o:02}"
60+
print(o)
61+
62+
Similarly, if the return value of a callback is ignored,
63+
annotate it with ``object``, not ``Any`` or ``None``::
64+
65+
def call_cb(cb: Callable[[int], object]) -> None:
66+
cb(42)
6167

6268
.. _argument-return-practices:
6369

0 commit comments

Comments
 (0)