[New-bugs-announce] [issue34380] Relax Restrictions on await?

Steven Silvester report at bugs.python.org
Sat Aug 11 05:22:13 EDT 2018


New submission from Steven Silvester <steven.silvester at gmail.com>:

When writing an `async` function, we often want to `await` a method call that may or may not be `async`.  For instance, it may be synchronous in the base class, but asynchronous in the subclass on the instance we have been given.  It would be nice for `await foo` to be a no-op if `foo` does not have an `__await__` method.  For instance, the following works in EMCAScript 2017:  `async function foo () { let bar = await 1; console.log(bar); }`.  As a workaround, we have been using `await force_async(foo.bar)`, where `force_async` is the following:

```python
async def force_async(obj):
    """Force an object to be asynchronous"""
    if hasattr(obj, '__await__'):
        return await obj
    async def inner():
        return obj
    return await inner()
```

This functionality is roughly equivalent to `gen.maybe_future` for coroutines, that is now deprecated in Tornado.  cf http://www.tornadoweb.org/en/stable/gen.html#tornado.gen.maybe_future

----------
components: asyncio
messages: 323410
nosy: Steven Silvester, asvetlov, yselivanov
priority: normal
severity: normal
status: open
title: Relax Restrictions on await?
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34380>
_______________________________________


More information about the New-bugs-announce mailing list