Skip to content
This repository was archived by the owner on Jul 5, 2023. It is now read-only.
This repository was 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

Description

@mbdevpl

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?

Activity

  1. 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[/+] on Mar 14, 2017
  2. 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[/+] on Mar 14, 2017
  3. 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[/+] on Mar 14, 2017
  4. gvanrossum commented on Mar 14, 2017

    @gvanrossum
    Member

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

  5. refi64 commented on Mar 14, 2017

    @refi64

    This happens also with built-in ast.

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

  6. mbdevpl commented on Mar 14, 2017

    @mbdevpl
    Author

    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.

  7. gvanrossum commented on Mar 14, 2017

    @gvanrossum
    Member

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

  8. ddfisher commented on Mar 14, 2017

    @ddfisher
    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.

  9. mbdevpl commented on Mar 15, 2017

    @mbdevpl
    Author

    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

  10. gvanrossum commented on Mar 15, 2017

    @gvanrossum
    Member

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

  11. added a commit that references this issue on Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions