Instagram / Instagram/LibCST

Invalid code generated by removing parens from `Call` node

Open
#531 1 comment 0 reactions 0 assignees View on GitHub
bug parsing
Dominant language
Python
Stars
1.9k
Forks
229
PR merge metrics
No merged PRs in 30d

Description

```python
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](https://github.com/Zac-HD/shed/blob/dec644c3394431bb20a917d423c67001402e0671/src/shed/_codemods.py#L94-L101) in `shed`, which removed *required* parentheses around ["fluent interface" call chains](https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.