[RFC] Add `.code` to statements and expressions
- Dominant language
- Python
- Stars
- 1.9k
- Forks
- 229
- PR merge metrics
- No merged PRs in 30d
Description
Related to https://github.com/Instagram/LibCST/issues/16
While debugging I often find myself dumping generated code. This can be accomplished by using:
```Python
Module([]).code_for_node(node)
```
This is hardly elegant. While I understand that Annotations cannot have code generated without context, I don't see any reason not to add a `.code` to statements and expressions. The fact that white space might not be rendered in the same way as it would be in a module doesn't seem worse than information being lost by `parse_statement`. It is already the case that
```Python
src = 'def f(x):\n print(x)\n'
assert Module([]).code_for_node(parse_statement(src)) != src
```
So I wouldn't find it particularly surprising that `.code` on statements and expressions just used reasonable defaults (i.e. `default_indent=' '*4, default_newline='\n'`)
@jimmylai would you accept a PR adding `.code` to `BaseExpression`, `BaseSmallStatement`, and `BaseCompoundStatement`?
Contributor guide
Assessment
This issue has not been assessed yet.