[RFC] Add `.code` to statements and expressions
Related to https://github.com/Instagram/LibCST/issues/16
While debugging I often find myself dumping generated code. This can be accomplished by using:
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
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?
👍 on this -- would be quite helpful. Could submit a PR at some point but I haven't dug into the code enough yet.
That said, I was first looking for a workaround and your initial solution is pretty good :)
I'm happy to accept a PR for this. Please make sure to document the limitations (inability to configure how whitespace is rendered) and the workaround (make a Module object with the desired properties and call code_for_node on that)