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.

Author mbdevpl
Recipients mbdevpl
Date 2017-03-15.06:26:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1489559184.67.0.191870108589.issue29814@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2017-03-15 06:26:24mbdevplsetrecipients: + mbdevpl
2017-03-15 06:26:24mbdevplsetmessageid: <1489559184.67.0.191870108589.issue29814@psf.upfronthosting.co.za>
2017-03-15 06:26:24mbdevpllinkissue29814 messages
2017-03-15 06:26:23mbdevplcreate