Skip to content

ast.unparse: visually better code generation #88308

Closed

Description

@isidentical
BPO 44142
Nosy @pablogsal, @isidentical
PRs
  • bpo-44142: drop redundant parantheses when unparsing tuples as assignment targets #26156
  • 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/isidentical'
    closed_at = None
    created_at = <Date 2021-05-15.13:29:46.966>
    labels = ['type-feature', 'library', '3.11']
    title = 'ast.unparse: visually better code generation'
    updated_at = <Date 2021-05-15.23:26:57.680>
    user = 'https://github.com/isidentical'

    bugs.python.org fields:

    activity = <Date 2021-05-15.23:26:57.680>
    actor = 'BTaskaya'
    assignee = 'BTaskaya'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2021-05-15.13:29:46.966>
    creator = 'BTaskaya'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44142
    keywords = ['patch']
    message_count = 1.0
    messages = ['393714']
    nosy_count = 2.0
    nosy_names = ['pablogsal', 'BTaskaya']
    pr_nums = ['26156']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue44142'
    versions = ['Python 3.11']

    Activity

    1. isidentical commented on May 15, 2021

      @isidentical
      SponsorMemberAuthor

      This issue is for tracking possible places where we could generate better code on ast.unparse (better as in more closely to what people are actually writing than our naive implementation).

      Examples;
      >>> import ast
      >>> ast.unparse(ast.parse('a, b, c = [1,2,3]'))
      '(a, b, c) = [1, 2, 3]'
      
      could be
      >>> ast.unparse(ast.parse('a, b, c = [1,2,3]'))
      'a, b, c = [1, 2, 3]'
      
      OR
      >>> print(ast.unparse(ast.parse('if value := d.get("something"): print(value)')))
      if (value := d.get('something')):
          print(value)
      
      could be
      >>> print(ast.unparse(ast.parse('if value := d.get("something"): print(value)')))
      if value := d.get('something'):
          print(value)

      We could even go further with the long line unpacking (which would definitely require some sort of clever algorithm for nested structures).

      >>> source = '[\n' + '\tsomething,\n' * 20 + ']'
      >>> print(source)
      [
              something,
              something,
              something,
              something,
              something,
              something,
              something,
              something,
              something,
              something,
              something,
              something,
              something,
              something,
              something,
              something,
              something,
              something,
              something,
              something,
      ]
      >>> print(ast.unparse(ast.parse(source)))
      [something, something, something, something, something, something, something, something, something, something, something, something, something, something, something, something, something, something, something, something]
    2. self-assigned this
      on May 15, 2021
    3. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      3.11only security fixes
      on May 15, 2021
    4. self-assigned this
      on May 15, 2021
    5. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on May 15, 2021
    6. transferred this issue fromon Apr 10, 2022
    7. furkanonder commented on May 9, 2023

      @furkanonder
      Contributor

      @isidentical The issue seems to be solved. We can close the issue.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    3.11only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions