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

Fix converting AST expression to string and optimize parenthesis #77656

Closed
serhiy-storchaka opened this issue May 12, 2018 · 4 comments
Closed
Labels
3.7 (EOL) end of life 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

BPO 33475
Nosy @ambv, @serhiy-storchaka
PRs
  • bpo-33475: Fix and improve converting annotations to strings. #6774
  • [3.7] bpo-33475: Fix and improve converting annotations to strings. (GH-6774). #6927
  • 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 2018-05-23.05:18:22.807>
    created_at = <Date 2018-05-12.22:22:34.763>
    labels = ['interpreter-core', '3.8', 'type-bug', '3.7']
    title = 'Fix converting AST expression to string and optimize parenthesis'
    updated_at = <Date 2018-05-23.05:18:22.806>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2018-05-23.05:18:22.806>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-05-23.05:18:22.807>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2018-05-12.22:22:34.763>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33475
    keywords = ['patch']
    message_count = 4.0
    messages = ['316439', '316881', '316887', '317177']
    nosy_count = 2.0
    nosy_names = ['lukasz.langa', 'serhiy.storchaka']
    pr_nums = ['6774', '6927']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue33475'
    versions = ['Python 3.7', 'Python 3.8']

    @serhiy-storchaka
    Copy link
    Member Author

    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.

    @serhiy-storchaka serhiy-storchaka added 3.7 (EOL) end of life 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels May 12, 2018
    @ambv
    Copy link
    Contributor

    ambv commented May 17, 2018

    New changeset 64fddc4 by Łukasz Langa (Serhiy Storchaka) in branch 'master':
    bpo-33475: Fix and improve converting annotations to strings. (GH-6774)
    64fddc4

    @serhiy-storchaka
    Copy link
    Member Author

    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 (bpo-33552).

    @serhiy-storchaka
    Copy link
    Member Author

    New changeset b32f889 by Serhiy Storchaka in branch '3.7':
    [3.7] bpo-33475: Fix and improve converting annotations to strings. (GH-6774). (GH-6927)
    b32f889

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants