[issue28942] await expressions in f-strings
New submission from Adam Gregory: Hi, I've been playing with f-strings, which seem like a great addition to the language. I noticed in the definition of f_expression that it can include any or_expr. As far as I understand, this includes "await" expressions, so I tried using await inside an f-string in a coroutine with CPython 3.6.0b4. This produces a SyntaxError. Should await be allowed in f-strings? I don't know if this is a bug or a documentation issue. Personally, I think it would be an occasionally useful feature - more so than yield, at least, which does work. Ref: https://docs.python.org/3.6/reference/lexical_analysis.html#formatted-string... ---------- assignee: docs@python components: Documentation, asyncio messages: 282980 nosy: Adam Gregory, docs@python, gvanrossum, yselivanov priority: normal severity: normal status: open title: await expressions in f-strings versions: Python 3.6 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28942> _______________________________________
Adam Gregory added the comment: Replicated in CPython 3.6.0rc1 ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28942> _______________________________________
Changes by Serhiy Storchaka <storchaka+cpython@gmail.com>: ---------- nosy: +eric.smith _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28942> _______________________________________
Guido van Rossum added the comment: I was going to say "no", but given that "yield" works, I think it is reasonable to allow "await" as well. (And what about "yield from"?) ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28942> _______________________________________
Yury Selivanov added the comment:
I was going to say "no", but given that "yield" works, I think it is reasonable to allow "await" as well. (And what about "yield from"?)
Agree. I suspect the reason is that async/await aren't proper keywords in 3.5/3.6, and the hacks we have in tokenizer to recognize them aren't working in f-strings. I'll assign this issue to myself to make sure it's resolved in 3.7 once we make async/await keywords. ---------- assignee: docs@python -> yselivanov _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28942> _______________________________________
Andrew Svetlov <andrew.svetlov@gmail.com> added the comment: Yury, ping. ---------- nosy: +asvetlov _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue28942> _______________________________________
Change by Andrew Svetlov <andrew.svetlov@gmail.com>: ---------- versions: +Python 3.7 -Python 3.6 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue28942> _______________________________________
Yury Selivanov <yselivanov@gmail.com> added the comment: Thanks, I'll take a look. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue28942> _______________________________________
Yury Selivanov <yselivanov@gmail.com> added the comment: Looks like it's working now: import asyncio async def foo(): return 32 async def bar(): print(f'{await foo()}') asyncio.run(bar()) Prints: 32 ---------- resolution: -> not a bug stage: -> resolved status: open -> closed type: -> behavior _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue28942> _______________________________________
participants (5)
-
Adam Gregory -
Andrew Svetlov -
Guido van Rossum -
Serhiy Storchaka -
Yury Selivanov