This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: await execution order leads to throw or bad syntax
Type: crash Stage: resolved
Components: asyncio Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, rmlibre, yselivanov
Priority: normal Keywords:

Created on 2019-11-01 17:49 by rmlibre, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg355829 - (view) Author: rmlibre (rmlibre) Date: 2019-11-01 17:49
If a coroutine returns a sequence, a user cannot write this clean code:

>>> await coro()[index]
Or,
>>> await coro()[slice]
Or,
>>> await coro()[key]

This raises a TypeError("'coroutine' object is not subscriptable"). To
solve this on the user side, one must add parentheses, leading to a 
convention of ugly syntax:

>>> (await coro())[...]

Or, hiding the parentesis away in a function

>>> async def index_coroutine(coro=None, index=None):
>>>     return (await coro)[index]
>>> await index_coroutine(coro(), slice(1, 5))

This is gross. It's a bug since it unnecessarily requires unpythonic 
code and throws pythonic code. My suggested patch would be to move the 
evaluation of the await statement higher up on the execution order so 
the former snippets would be valid python syntax. The await statement 
should imply parentheses when square brackets are used, since there is 
no other use case for indexing or subscripting a coroutine. This will
lead to more beautiful code.
msg355833 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2019-11-01 18:40
What patch are you talking about?
For me, patch means something special; e.g. working fork of CPython where the proposed idea is implemented.

I read await coro()[key] as 
1. Function named coro() returns a dict-like object
2. An element under key is selected from dict, which is expected to be an awaitable.
3. The awaitable is awaited.

Pretty clear and unambiguous.

Changing await priority depending on the presence or absence of brackets on the right makes Python syntax very complex if possible at all.

Sorry, the proposal has zero chance to be accepted.
History
Date User Action Args
2022-04-11 14:59:22adminsetgithub: 82845
2019-11-01 18:40:07asvetlovsetstatus: open -> closed
resolution: not a bug
messages: + msg355833

stage: resolved
2019-11-01 17:49:13rmlibrecreate