Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast.unparse: visually better code generation #88308

Closed
isidentical opened this issue May 15, 2021 · 2 comments
Closed

ast.unparse: visually better code generation #88308

isidentical opened this issue May 15, 2021 · 2 comments
Assignees
Labels
3.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@isidentical
Copy link
Sponsor Member

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']

    @isidentical
    Copy link
    Sponsor Member Author

    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]

    @isidentical isidentical added the 3.11 only security fixes label May 15, 2021
    @isidentical isidentical self-assigned this May 15, 2021
    @isidentical isidentical added stdlib Python modules in the Lib dir type-feature A feature request or enhancement 3.11 only security fixes labels May 15, 2021
    @isidentical isidentical self-assigned this May 15, 2021
    @isidentical isidentical added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels May 15, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @furkanonder
    Copy link
    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
    Labels
    3.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants