LibCST
LibCST copied to clipboard
A concrete syntax tree parser and serializer library for Python that preserves many aspects of Python's abstract syntax tree
In a local scope, e.g. inside a function, class, if condition. The comment is associated with the statement correctly. ``` In [10]: code = """ ...: def f(): ...: #...
Raise more specific errors when a statement is passed in to `parse_expression`, or multiple statements to `parse_statement`. ``` ParserError: Failed to parse expression - found statement tokens Caused by ParserSyntaxError:...
This is definietly not a realistic code (but it is still used in projects [like this](https://github.com/facebook/facebook-python-business-sdk/blob/95e64a10d987d7a53963d17036b6730d07f84ab5/facebook_business/test/integration_campaign.py#L44-L77)), though I think it might point to an underlying stack overflow or something like...
In fact this is captured as a test case in https://github.com/instagram/libcst/blob/f36eacb1327830fad51e08084b46601cad681ae0/libcst/_nodes/tests/test_import.py#L394-L401 ``` cst.ImportFrom( module=cst.Name("foo"), names=( cst.ImportAlias(cst.Name("bar"), comma=cst.Comma()), cst.ImportAlias(cst.Name("baz"), comma=cst.Comma()), ), ) ``` This node generates `from foo import bar,baz,` which...
```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!...
```python import libcst as cst code = """ from math import (nan as NaN) """ compile(code, "", "exec") # (no error) cst.parse_module(code) # ParserSyntaxError: Syntax Error @ 4:9. # Internal...
eg I expect the following to generate `'{"ham": "spam", **({"x": x} if x else {})}'` ```python from libcst import ( Dict, DictElement, SimpleString, Name, LeftParen, RightParen, StarredDictElement, IfExp, Module, parse_expression,...
For example it can be a `Subscript`, not just `Name | List | Tuple`. In fact I think there are two kinds of `AsName`s - the ones in imports and...
According to [the docs](https://libcst.readthedocs.io/en/latest/nodes.html#libcst.Assign.value) `Assign.value` is supposed to be a `BaseExpression`, but: ``` echo "f = *2" | python -m libcst.tool print - Module( body=[ SimpleStatementLine( body=[ Assign( targets=[ AssignTarget(...
The [fine docs](https://libcst.readthedocs.io/en/latest/nodes.html#libcst.GeneratorExp.elt) say that `elt` in the various comprehension nodes are of type `BaseAssignTargetExpression`. This is a lie. Observe: in the syntactically correct and semantically plausible expression `(i **...