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

Invalid code generated by removing parens from Call node #531

Open
Zac-HD opened this issue Oct 11, 2021 · 1 comment
Open

Invalid code generated by removing parens from Call node #531

Zac-HD opened this issue Oct 11, 2021 · 1 comment
Labels
bug Something isn't working parsing Converting source code into CST nodes

Comments

@Zac-HD
Copy link
Contributor

Zac-HD commented Oct 11, 2021

from libcst import *

attr = Attribute(
    value=List(elements=[]), attr=Name(value="append"),
    dot=Dot(whitespace_before=ParenthesizedWhitespace(
        first_line=TrailingWhitespace(newline=Newline()))
    ),
)
node = Call(func=attr, args=[Arg(value=Integer(value="1"))])

code = Module([]).code_for_node(node)
assert code == "[]\n.append(1)"
parse_module(code)  # error, invalid syntax!

I discovered this via an (inadequately tested) codemod in shed, which removed required parentheses around "fluent interface" call chains (as preferred by Black).

I can work around this in shed just by checking that the proposed replacement compiles, but it would be nice if (a) this was an immediate error, and (b) parens could take a MaybeSentinel to handle it automatically. Related to #287, #341.

@zsol zsol added the bug Something isn't working label Oct 16, 2021
@zsol
Copy link
Member

zsol commented Oct 16, 2021

This is a tricky one - it's unclear which node's responsibility should include validating the presence of these required parens.

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