classification
Title: await expressions in f-strings
Type: behavior Stage: resolved
Components: asyncio, Documentation Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: yselivanov Nosy List: Adam Gregory, asvetlov, docs@python, eric.smith, gvanrossum, yselivanov
Priority: normal Keywords:

Created on 2016-12-12 11:12 by Adam Gregory, last changed 2017-12-21 00:06 by yselivanov. 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
2017-12-21 00:06:00yselivanovsetstatus: open -> closed
type: behavior
messages: + msg308836

resolution: not a bug
stage: resolved
2017-12-20 20:34:07yselivanovsetmessages: + msg308806
2017-12-20 20:31:53asvetlovsetversions: + Python 3.7, - Python 3.6
2017-12-20 20:31:42asvetlovsetnosy: + asvetlov
messages: + msg308805
2016-12-12 15:51:56yselivanovsetassignee: docs@python -> yselivanov
messages: + msg283015
2016-12-12 15:19:54gvanrossumsetmessages: + msg283010
2016-12-12 11:42:03serhiy.storchakasetnosy: + eric.smith
2016-12-12 11:20:29Adam Gregorysetmessages: + msg282981
2016-12-12 11:12:02Adam Gregorycreate