Skip to content

ast.unparse can't roundtrip some f-strings #72189

Closed

Description

@ericvsmith
BPO 28002
Nosy @simonpercivall, @ericvsmith, @berkerpeksag, @serhiy-storchaka, @pablogsal, @miss-islington, @remilapeyre, @isidentical, @loganasherjones, @hauntsaninja
PRs
  • bpo-28002: Roundtrip f-strings with ast.unparse better #19612
  • [3.9] bpo-28002: Roundtrip f-strings with ast.unparse better (GH-19612) #23430
  • 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 = 'https://github.com/ericvsmith'
    closed_at = <Date 2020-11-20.21:53:16.672>
    created_at = <Date 2016-09-07.17:18:18.601>
    labels = ['type-bug', 'library', '3.9']
    title = "ast.unparse can't roundtrip some f-strings"
    updated_at = <Date 2020-11-20.21:55:43.133>
    user = 'https://github.com/ericvsmith'

    bugs.python.org fields:

    activity = <Date 2020-11-20.21:55:43.133>
    actor = 'eric.smith'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2020-11-20.21:53:16.672>
    closer = 'BTaskaya'
    components = ['Library (Lib)']
    creation = <Date 2016-09-07.17:18:18.601>
    creator = 'eric.smith'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 28002
    keywords = ['patch']
    message_count = 12.0
    messages = ['274841', '341502', '341512', '341633', '366481', '366796', '366800', '374724', '378530', '381507', '381509', '381510']
    nosy_count = 10.0
    nosy_names = ['simon.percivall', 'eric.smith', 'berker.peksag', 'serhiy.storchaka', 'pablogsal', 'miss-islington', 'remi.lapeyre', 'BTaskaya', 'loganasherjones', 'hauntsaninja']
    pr_nums = ['19612', '23430']
    priority = 'low'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue28002'
    versions = ['Python 3.9']

    Activity

    1. ericvsmith commented on Sep 7, 2016

      @ericvsmith
      MemberAuthor

      The problem relates to how f-strings are re-created.

      For the input file foo.txt containing this one line:
      f'''{"'"}'''

      Run:
      ./python Tools/parser/test_unparse.py foo.txt

      Gives this output:
      f'{"\'"}'

      This result is not a valid f-string, since it contains a backslash inside the expression part of the f-string.

      The input string is a valid f-string:
      >>> f'''{"'"}'''
      "'"
    2. added
      type-bug
      on Sep 7, 2016
    3. changed the title [-]f-strings do not round trip through Tools/parser/test_unparse.py[/-] [+]Some f-strings do not round trip through Tools/parser/test_unparse.py[/+] on Sep 7, 2016
    4. added
      3.7 (EOL)
      3.8 (EOL)
      on May 6, 2019
    5. serhiy-storchaka commented on May 6, 2019

      @serhiy-storchaka
      Member

      There is the same problem with annotations.

      >>> from __future__ import annotations
      >>> def f() -> X[f'''{"'"}''']: pass
      ... 
      >>> print(f.__annotations__['return'])
      X[f'{"\'"}']

      You can look at https://github.com/berkerpeksag/astor. Maybe this problem is solved in that project.

    6. remilapeyre commented on May 6, 2019

      remilapeyremannequin
      Mannequin

      The issue is also present in Astor:

      Python 3.7.3 (default, Mar 27 2019, 09:23:15) 
      [Clang 10.0.1 (clang-1001.0.46.3)] on darwin
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import ast
      >>> tree = ast.parse("""
      ... f'''{"'"}'''
      ... """)
      >>> import astor
      >>> astor.to_source(tree)
      'f"""{"\'"}"""\n'
      >>> f"""{"\'"}"""
        File "<stdin>", line 1
      SyntaxError: f-string expression part cannot include a backslash

      The issue comes too from calling repr to format the inner string as it does not try to minimize the number of \.

    7. loganasherjones commented on May 6, 2019

      loganasherjonesmannequin
      Mannequin

      After speaking with Lukasz about this, it seems like the unparser is using the normal unicode repr to determine what should be returned. The default unicode repr will escape quotes if necessary. This is not allowed for f-strings and is the root cause of the problem.

      One way to solve this is to add a flag to the unicode_repr function to determine whether or not we need to allow triple quotes in the output of the repr. By default this will be false and will use backslashes, but the ast_unparse will use true for this.

    8. added
      stdlib
      3.9 (EOL)
      and removed
      3.7 (EOL)
      3.8 (EOL)
      on Jan 8, 2020
    9. changed the title [-]Some f-strings do not round trip through Tools/parser/test_unparse.py[/-] [+]ast.unparse can't roundtrip some f-strings[/+] on Jan 8, 2020
    10. hauntsaninja commented on Apr 15, 2020

      @hauntsaninja
      Contributor

      Now that ast.unparse is being added to stdlib, I thought I'd bump this thread. The third party library astunparse (which attempts to support multiple Python versions while staying very close to Tools/parser/unparse.py) has an implementation that works for complex cases.
      If that sounds good, I can submit a PR and inform the original author (astunparse is license is based on PSF's, so should be okay if the original author doesn't respond).

      https://github.com/simonpercivall/astunparse/blob/master/lib/astunparse/unparser.py#L461

    11. isidentical commented on Apr 19, 2020

      @isidentical
      SponsorMember

      If that sounds good, I can submit a PR and inform the original author

      I can't say anything before seeing the approach but fixing the problem definitely sounds good.

    12. simonpercivall commented on Apr 19, 2020

      simonpercivallmannequin
      Mannequin

      Any and all code from astunparse is certainly available for inclusion. Go ahead.

    13. hauntsaninja commented on Aug 3, 2020

      @hauntsaninja
      Contributor

      Just bumping this issue, as per dev guide, since #19612 has been ready for about two months. Would be grateful for review :-)

    14. hauntsaninja commented on Oct 12, 2020

      @hauntsaninja
      Contributor

      Bumping again!

    15. isidentical commented on Nov 20, 2020

      @isidentical
      SponsorMember

      New changeset a993e90 by Shantanu in branch 'master':
      bpo-28002: Roundtrip f-strings with ast.unparse better (bpo-19612)
      a993e90

    16. isidentical commented on Nov 20, 2020

      @isidentical
      SponsorMember

      New changeset 3763cc1 by Miss Islington (bot) in branch '3.9':
      bpo-28002: Roundtrip f-strings with ast.unparse better (GH-19612) (GH-23430)
      3763cc1

    17. isidentical commented on Nov 20, 2020

      @isidentical
      SponsorMember

      Thanks for your efforts Shantanu!

    18. 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

    Labels

    3.9 (EOL)stdlibtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions