Message355829
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. |
|
Date |
User |
Action |
Args |
2019-11-01 17:49:13 | rmlibre | set | recipients:
+ rmlibre, asvetlov, yselivanov |
2019-11-01 17:49:13 | rmlibre | set | messageid: <1572630553.03.0.31312836907.issue38664@roundup.psfhosted.org> |
2019-11-01 17:49:12 | rmlibre | link | issue38664 messages |
2019-11-01 17:49:12 | rmlibre | create | |
|