New submission from rmlibre <rmli...@riseup.net>:

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.

----------
components: asyncio
messages: 355829
nosy: asvetlov, rmlibre, yselivanov
priority: normal
severity: normal
status: open
title: await execution order leads to throw or bad syntax
type: crash
versions: Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue38664>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to