This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Change f-literal grammar so that escaping isn’t possible or necessary
Type: behavior Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, flying sheep, r.david.murray
Priority: normal Keywords:

Created on 2016-04-08 14:53 by flying sheep, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg263026 - (view) Author: (flying sheep) * Date: 2016-04-08 14:53
code inside of the braces of an f-literal should have the exact same lexing rules than outside *except* for an otherwise unparsable !, :, or } signifying the end of the replacement field

1. every other language with template literals has it that way
2. it makes sense that the content of the f-literal is a “hole” in which normal code goes until a !, : or } signifies its end
3. escaping code that will be evaluated reeks of “eval” even though it isn’t

as it is now, it’s very confusing as the contents are neither code nor string content.

that might be one reason why many people get it wrong and think it can be stored unevaluatedly and thus provides a security risk (which is obv. wrong)

the whole section after “A consequence of sharing the same syntax as regular string literals is…” has to be removed and made unnecessary by allowing everything otherwise legal inside.

e.g. f'spam{(lambda: 1)():<4}' would be legal and be exactly the same as '{:<4}'.format((lambda: 1)())
msg263027 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-04-08 15:27
This is not a topic appropriate for the bug tracker.  Please bring it up on python-ideas, after reviewing the PEP (which probably addresses the reason the design is the way it is, but there may be additional concerns that arise from practical experience with the results).
msg263029 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2016-04-08 17:28
I agree with David here, this isn't a bug tracker level issue.

But, to the specifics of your example, it already works:

Python 3.6.0a0 (default:9095a5787a82+, Feb  5 2016, 18:24:55) 
[GCC 5.2.1 20151010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> f'spam{(lambda: 1)():<4}'
'spam1   '
>>> 

The section of the docs you cited is only talking about single quotes, double quotes, escaped quotes, triple quotes and the like.

So I'm not clear what changes you'd be proposing. If you bring this up on python-ideas, please be specific about what you'd change.

Thanks!
msg263030 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-04-08 17:40
If it turns out it is just a doc issue we can reopen this to address that.
History
Date User Action Args
2022-04-11 14:58:29adminsetgithub: 70900
2016-04-08 17:40:20r.david.murraysetmessages: + msg263030
2016-04-08 17:28:24eric.smithsetmessages: + msg263029
2016-04-08 15:27:37r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg263027

resolution: not a bug
stage: resolved
2016-04-08 14:54:29berker.peksagsetnosy: + eric.smith
2016-04-08 14:54:09flying sheepsettype: behavior
versions: + Python 3.6
2016-04-08 14:53:50flying sheepcreate