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

StarredDictElement can produce syntactically incorrect output #519

Open
graingert opened this issue Sep 2, 2021 · 2 comments
Open

StarredDictElement can produce syntactically incorrect output #519

graingert opened this issue Sep 2, 2021 · 2 comments
Labels
bug Something isn't working parsing Converting source code into CST nodes

Comments

@graingert
Copy link

graingert commented Sep 2, 2021

eg I expect the following to generate '{"ham": "spam", **({"x": x} if x else {})}'

from libcst import (
    Dict,
    DictElement,
    SimpleString,
    Name,
    LeftParen,
    RightParen,
    StarredDictElement,
    IfExp,
    Module,
    parse_expression,
)

d = Module(
    [
        Dict(
            elements=[
                DictElement(
                    key=SimpleString(value='"ham"'), value=SimpleString(value='"spam"')
                ),
                StarredDictElement(
                    value=IfExp(
                        test=Name(value="x"),
                        body=Dict(
                            elements=[
                                DictElement(
                                    key=SimpleString(value='"x"'), value=Name(value="x")
                                )
                            ]
                        ),
                        orelse=Dict(elements=[]),
                    )
                ),
            ]
        )
    ]
)
parse_expression(d.code)  # SyntaxError
assert d.code == '{"ham": "spam", **({"x": x} if x else {})}'

however I get '{"ham": "spam", **{"x": x} if x else {}}'

@graingert
Copy link
Author

I need to add an additional lpar=[LeftParen()], rpar=[RightParen()]:

Dict(
    elements=[
        DictElement(
            key=SimpleString(value='"ham"'), value=SimpleString(value='"spam"')
        ),
        StarredDictElement(
            value=IfExp(
                test=Name(value="x"),
                body=Dict(
                    elements=[
                        DictElement(
                            key=SimpleString(value='"x"'), value=Name(value="x")
                        )
                    ]
                ),
                orelse=Dict(elements=[]),
                lpar=[LeftParen()],
                rpar=[RightParen()],
            )
        ),
    ]
)

@graingert
Copy link
Author

I have the same problem with all of cst.IfExp, cst.BooleanOperation, cst.BinaryOperation, but I'm sure there's more

@zsol zsol added the bug Something isn't working label Sep 4, 2021
@zsol zsol added the parsing Converting source code into CST nodes label Jun 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parsing Converting source code into CST nodes
Projects
None yet
Development

No branches or pull requests

2 participants