Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

parsing f-strings -- opening brace gets duplicated when backslash is followed by an expression #34

Closed
mbdevpl opened this issue Mar 14, 2017 · 7 comments

Comments

@mbdevpl
Copy link

mbdevpl commented Mar 14, 2017

with Python 3.6.0 and typed-ast 1.0.2, I get the following:

#!/usr/bin/env python3.6

import typed_ast.ast3 as 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))

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? This happens also with built-in ast. I can't believe this is the intended behavior... Is it?

@mbdevpl mbdevpl changed the title parsing f-string -- opening brace gets duplicated when escaped backslash is followed by an expression f-strings -- opening brace gets duplicated when escaped backslash is followed by an expression Mar 14, 2017
@mbdevpl mbdevpl changed the title f-strings -- opening brace gets duplicated when escaped backslash is followed by an expression parsing f-strings -- opening brace gets duplicated when escaped backslash is followed by an expression Mar 14, 2017
@mbdevpl mbdevpl changed the title parsing f-strings -- opening brace gets duplicated when escaped backslash is followed by an expression parsing f-strings -- opening brace gets duplicated when backslash is followed by an expression Mar 14, 2017
@gvanrossum
Copy link
Member

What does the stdlib ast module print for the same inputs?

@refi64
Copy link

refi64 commented Mar 14, 2017

This happens also with built-in ast.

Sounds like you might have hit a CPython bug...

@mbdevpl
Copy link
Author

mbdevpl commented Mar 14, 2017

When parsing using built-in ast, the exact same thing happens:

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

Also, I just noticed that when I escape the backslash once, what gets parsed is actually unescaped backslash. So this might just boil down to inconsistency in parsing \{ in normal vs. f-strings.

@gvanrossum
Copy link
Member

Check the PEP, there's a rule against backslashes before curly braces.

@ddfisher
Copy link
Collaborator

It looks like the PEP only disallows backslashes within fstrings. @mbdevpl: this seems like a CPython bug -- could you file it in the Python bug tracker?

typed_ast's is intended to match the implicit spec that is CPython, so until this behavior is fixed upstream, typed_ast is behaving correctly.

@mbdevpl
Copy link
Author

mbdevpl commented Mar 15, 2017

Sure, thanks for taking a look! I had yet another careful read of the PEP, and I also cannot find anything about backslashes being disallowed outside of expressions. The only mention of \{ seems to be in discussion about possible alternate expression opening/closing delimiters.

I reported this issue here: https://bugs.python.org/issue29814

@gvanrossum
Copy link
Member

Where it was deemed a dupe of https://bugs.python.org/issue29104.

tbbharaj pushed a commit to tbbharaj/typed_ast that referenced this issue Dec 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants