Issue 28942: await expressions in f-strings (original) (raw)

Created on 2016-12-12 11:12 by Adam Gregory, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (7)
msg282980 - (view) Author: Adam Gregory (Adam Gregory) Date: 2016-12-12 11:12
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-literals
msg282981 - (view) Author: Adam Gregory (Adam Gregory) Date: 2016-12-12 11:20
Replicated in CPython 3.6.0rc1
msg283010 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-12-12 15:19
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"?)
msg283015 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2016-12-12 15:51
> 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.
msg308805 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2017-12-20 20:31
Yury, ping.
msg308806 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-12-20 20:34
Thanks, I'll take a look.
msg308836 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-12-21 00:06
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
History
Date User Action Args
2022-04-11 14:58:40 admin set github: 73128
2017-12-21 00:06:00 yselivanov set status: open -> closedtype: behaviormessages: + resolution: not a bugstage: resolved
2017-12-20 20:34:07 yselivanov set messages: +
2017-12-20 20:31:53 asvetlov set versions: + Python 3.7, - Python 3.6
2017-12-20 20:31:42 asvetlov set nosy: + asvetlovmessages: +
2016-12-12 15:51:56 yselivanov set assignee: docs@python -> yselivanovmessages: +
2016-12-12 15:19:54 gvanrossum set messages: +
2016-12-12 11:42:03 serhiy.storchaka set nosy: + eric.smith
2016-12-12 11:20:29 Adam Gregory set messages: +
2016-12-12 11:12:02 Adam Gregory create