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: parsing f-strings -- opening brace of expression gets duplicated when preceeded by backslash
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Left bracket remains in format string result when '\' preceeds it
View: 29104
Assigned To: Nosy List: mbdevpl, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-03-15 06:26 by mbdevpl, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg289642 - (view) Author: Mateusz Bysiek (mbdevpl) Date: 2017-03-15 06:26
with Python 3.6.0 and the following script:

```
#!/usr/bin/env python3.6

import ast

code1 = '''"\\{x}"'''
code2 = '''f"\\{x}"'''

tree1 = ast.parse(code1, mode='eval')
print(ast.dump(tree1))
tree2 = ast.parse(code2, mode='eval')
print(ast.dump(tree2))
```

I get the following output:

```
Expression(body=Str(s='\\{x}'))
Expression(body=JoinedStr(values=[Str(s='\\{'), FormattedValue(value=Name(id='x', ctx=Load()), conversion=-1, format_spec=None)]))
```

Therefore, the normal string is `'\\{x}'`.
But the f-string has two parts: `'\\{'` and an expression `Name(id='x', ctx=Load())`.

Where does the `{` in the string part of f-string come from? I can't believe this is the intended behavior... Or, is it?

When I escape the backslash once like above, what gets parsed is actually unescaped backslash. So this might just boil down to inconsistency in parsing `\{` in normal vs. f-strings.

I originally discovered this in typed_ast https://github.com/python/typed_ast/issues/34 but the behaviour of ast is identical and since developers of typed_ast aim at compatibility with ast, I bring this issue here.
msg289644 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-15 07:03
This is a duplicate of issue29104.
History
Date User Action Args
2022-04-11 14:58:44adminsetgithub: 74000
2017-03-15 07:03:43serhiy.storchakasetstatus: open -> closed

superseder: Left bracket remains in format string result when '\' preceeds it

nosy: + serhiy.storchaka
messages: + msg289644
resolution: duplicate
stage: resolved
2017-03-15 06:26:24mbdevplcreate