From 7ce9b00a54d05d5a58a8f0c2bf1847091ddfecce Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Mon, 17 Nov 2025 13:52:56 +0100 Subject: [PATCH] Best Practices: Split an example Split the text and examples for `Any` vs `object` into one for arguments and one for callback return types. This separated these two related, but separate suggestions. It should make the examples a bit clearer and less convoluted. --- docs/reference/best_practices.rst | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/reference/best_practices.rst b/docs/reference/best_practices.rst index a6585c07e..ebf58dfc9 100644 --- a/docs/reference/best_practices.rst +++ b/docs/reference/best_practices.rst @@ -52,12 +52,18 @@ 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``:: +type annotation:: - def call_cb_if_int(cb: Callable[[int], object], o: object) -> None: + def print_formatted(o: object) -> None: if isinstance(o, int): - cb(o) + o = f"{o:02}" + print(o) + +Similarly, if the return value of a callback is ignored, +annotate it with ``object``, not ``Any`` or ``None``:: + + def call_cb(cb: Callable[[int], object]) -> None: + cb(42) .. _argument-return-practices: