8000 Best Practices: Split an example by srittau · Pull Request #2114 · python/typing · GitHub
[go: up one dir, main page]

Skip to content
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. 8000
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions docs/reference/best_practices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
0