Closed straz closed 2 months ago
Well first off, libcst doesn't seem to do any children-node validation upon on node initialization which is why the constructor is valid at runtime, although at type check time, tools like pyright or mypy definitely do raise an error (I strongly recommend using a type checker with libcst). Essentially what you are doing right now is wrap a SimpleStatementLine
node in another SimpleStatementLine
, which given pythons grammar, is not a valid CST. Could you elaborate on what your intention is?
Also, if you want to generate code for a single node without any context in regards to the module it was defined in and the default whitespace config, you can instantiate the module like this cst.Module(body=())
instead of cst.parse_module("")
.
Thank you. I didn't realize I was nesting two SimpleStatementLine
s. Removing the outer one solves my problem.
Thank you for the cst.Module(body=())
tip.
My last question: is there any documentation for constructor parameters? It would be very helpful!
I don't see constructor parameters anywhere in the documentation - where are these documented?
Are you referring to the node constructor or what kind of parameters the codegen
implementation takes?
For the former, nodes are nothing more than a dataclass, which means all the attributes you see listed on the docs page you linked can (usually) also be passed to the node constructor.
For the latter: No, I don't believe they are documented explicitly since any _codegen
or _codegen_impl
method seems to be internal to libcst (as one can tell by their underscore prefix)
I seem to get an error rendering a
SimpleStatementLine
. Its body seems like it's fine, and I seem to be able to create an instance ofSimpleStatementLine
without any errors (seessl
below).When I try to render the instance, that's when I get the error.
codegen
seems to be calling it with an unexpected keyword argumentdefault_semicolon
.Am I missing a constructor parameter here?
I don't see constructor parameters anywhere in the documentation - where are these documented?