Skip to content

parsing f-strings -- opening brace of expression gets duplicated when preceeded by backslash #74000

Closed

Description

@mbdevpl
mannequin
BPO 29814
Nosy @serhiy-storchaka, @mbdevpl
Superseder
  • bpo-29104: Left bracket remains in format string result when '' preceeds it
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-03-15.07:03:43.409>
    created_at = <Date 2017-03-15.06:26:24.640>
    labels = ['type-bug', 'library']
    title = 'parsing f-strings -- opening brace of expression gets duplicated when preceeded by backslash'
    updated_at = <Date 2017-03-15.07:03:43.407>
    user = 'https://github.com/mbdevpl'

    bugs.python.org fields:

    activity = <Date 2017-03-15.07:03:43.407>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-03-15.07:03:43.409>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2017-03-15.06:26:24.640>
    creator = 'mbdevpl'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 29814
    keywords = []
    message_count = 2.0
    messages = ['289642', '289644']
    nosy_count = 2.0
    nosy_names = ['serhiy.storchaka', 'mbdevpl']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '29104'
    type = 'behavior'
    url = 'https://bugs.python.org/issue29814'
    versions = ['Python 3.6']

    Activity

    1. mbdevpl commented on Mar 15, 2017

      mbdevplmannequin
      MannequinAuthor
      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 python/typed_ast#34 but the behaviour of ast is identical and since developers of typed_ast aim at compatibility with ast, I bring this issue here.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Mar 15, 2017
    3. serhiy-storchaka commented on Mar 15, 2017

      @serhiy-storchaka
      Member

      This is a duplicate of bpo-29104.

    4. transferred this issue fromon Apr 10, 2022
    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

      stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions