Andrew Svetlov <[email protected]> added the comment:
I think `func.__awaitable__ = True` can serve all possible situations.
We need it for async mocks (now the library use flawless `_is_coroutine`
approach.
`__awaitable__` can also cover cases where a regular function returns awaitable
object or there is a class with `def __await__(self)` method,
`functools.partial` -- all of them are awaitable.
Async functions can be modified to provide the property out of the box.
Regarding the question "is a check for awaitableness a good design or not"?
I agree that a check for awaitableness is useless *just before* calling the
function.
There is another very useful case where the check is important: registering a
callback for later usage.
For example, we have a web server framework. It is built around a mapping of
URL paths to async functions, e.g. path('/hello', say_hello) in Django style.
It's crucial to check if say_hello() is an async function *on the application
configuration stage*, not on viewing the particular http://localhost/hello page
in a browser.
So, I think an official protocol makes a lot of sense, let's implement it in 3.9
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue38225>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com