classification
Title: Fix converting AST expression to string and optimize parenthesis
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: lukasz.langa, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2018-05-12 22:22 by serhiy.storchaka, last changed 2018-05-23 05:18 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 6774 merged serhiy.storchaka, 2018-05-12 22:31
PR 6927 merged serhiy.storchaka, 2018-05-17 06:45
Messages (4)
msg316439 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-12 22:22
The proposed patch fixes bugs in ast_unparse.c, makes it generating cleaner string representation, and makes the code cleaner.

Known fixed bugs:

1. Extended slices crash Python. "s[a, b:c]".

2. 1-tuples produce illegal syntax: "(a,)" => "(, a)".

3. Lambdas in f-strings produce illegal syntax. "f'{(lambda x: x)}'" => "f'{lambda x: x}'".

4. Some expressions that need parenthesis don't have them. E.g. "lambda x: (x, x)" => "lambda x: x, x".

5. Generators and yield expression always must be surrounded with parenthesis. "(x for x in y)" => "x for x in y", "(yield)" => "yield".

6. Top-level tuples must be surrounded with parenthesis. "(a, b)" => "a, b".

Produced string representation is now more clean and almost not contains redundant parenthesis.

"(a + b) * (c + d)" => "(a + b) * (c + d)"
"(a * b) + (c * d)" => "a * b + c * d"
"(a * b) * (c * d)" => "a * b * (c * d)"
"(a ** b) ** (c ** d)" => "(a ** b) ** c ** d"
"[(a + b)]" => "[a + b]"
"[(i ** 2) for i in range(1, (a + 1))]" => "[i ** 2 for i in range(1, a + 1)]"

Maybe other bugs were fixed in process.

And finally I use macros for simple repeated fragments of code like

        if (-1 == append_charp(writer, str)) {
            return -1;
        }

This made the meaningful code much shorter and saved around 250 lines of code.
msg316881 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2018-05-17 03:17
New changeset 64fddc423fcbe90b8088446c63385ec0aaf3077c by Łukasz Langa (Serhiy Storchaka) in branch 'master':
bpo-33475: Fix and improve converting annotations to strings. (GH-6774)
https://github.com/python/cpython/commit/64fddc423fcbe90b8088446c63385ec0aaf3077c
msg316887 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-17 07:03
There are still problems with f-strings like f'''{"'"}''' => f'{"\'"}' (SyntaxError: f-string expression part cannot include a backslash). But this is a separate hard issue (issue33552).
msg317177 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-20 15:06
New changeset b32f8897eafcf335f9e9899c5e622a0b27679437 by Serhiy Storchaka in branch '3.7':
[3.7] bpo-33475: Fix and improve converting annotations to strings. (GH-6774). (GH-6927)
https://github.com/python/cpython/commit/b32f8897eafcf335f9e9899c5e622a0b27679437
History
Date User Action Args
2018-05-23 05:18:22serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-05-20 15:06:10serhiy.storchakasetmessages: + msg317177
2018-05-17 07:03:45serhiy.storchakasetmessages: + msg316887
2018-05-17 06:45:07serhiy.storchakasetpull_requests: + pull_request6598
2018-05-17 03:17:53lukasz.langasetmessages: + msg316881
2018-05-14 20:20:14gvanrossumsetnosy: - gvanrossum
2018-05-12 22:38:25serhiy.storchakasetnosy: + gvanrossum
2018-05-12 22:31:49serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request6462
2018-05-12 22:22:34serhiy.storchakacreate