Questions tagged [libcst]

`libcst` is a Concrete Syntax Tree (CST) parser and serializer library for Python.

Links

11 questions
5
votes
1 answer

LibCST: Converting arbitrary nodes to code

Is it possible to dump an arbitrary LibCST node into Python code? My use case is that I want to extract the code for functions that match a specific naming scheme. I can extract the FunctionDef nodes that I need, but I don't seem to find a way to…
Georgios Gousios
  • 2,405
  • 1
  • 24
  • 34
4
votes
1 answer

Python Libcst: Unable to generate code from the node in visitor class

I am searching for a way to get code from the node in the visitor. Example: import libcst code_example = """ from ast import parse threshold = 1 print(threshold) """ class CodeVisitor(libcst.CSTVisitor): def visit_Assign(self, node:…
Nomiluks
  • 2,052
  • 5
  • 31
  • 53
2
votes
0 answers

How can I write a Lint rule to prevent Global Variable Mutation in Python?

Global Variables are evil, we all know that (by global variables, I mean module level variables). I want to write a custom lint rule to protect its updation. For example, GLOBAL_DICT = { 'a': 1, 'b': 2, } def func(): var_1 =…
2
votes
2 answers

Find if-nodes that are immediately followed by a raise-node in Python with libcst

right now I am working on a project for a university course. I got some random functions and most of them have a if-raise-statement in the code somewhere. I try to find those, but only those 1 or 2 lines. I transform the functions into an AST and…
Marco
  • 33
  • 4
1
vote
0 answers

Is there a decode function in libcst to convert a tree to python source code?

I got a piece of output from a sample libcst.parse_module program like: Module( body=[ SimpleStatementLine( body=[ Assign( targets=[ AssignTarget( …
shanzhuer
  • 11
  • 1
1
vote
2 answers

libcst: Inserting new node adds inline code and a semicolon

I am trying to introduce a new node (as a new line of code), exactly before an Assign node. The issue occurs when using FlattenSentinel to introduce the new node as I want the node to be separate, but libcst concatenates them using a semicolon (;),…
user10728141
  • 59
  • 1
  • 8
0
votes
0 answers

Apply libcst codemod and skip test files

I am writing a codemod with libcst which inherits from VisitorBasedCodemodCommand. It works fine but is rather slow. One simple trick would be to skip all test files which start with test_ by convention. However I haven't been able to find a place…
Martin Faucheux
  • 884
  • 9
  • 26
0
votes
1 answer

How do I distinguish a variable passed as input to a function that's hardcoded, vs a locally defined variable?

If I have: def my_func(x: str) -> void: // def my_caller_func()-> void local_var = "xyz" local_var_2 = "zzz" my_func(x=local_var) I am trying to write a libcst visitor that will detect that the input value passed into my_func's x input…
user1008636
  • 2,989
  • 11
  • 31
  • 45
0
votes
1 answer

Rust compilation errors on installaton of libcst on alpine python

trying to install python modules on alpine linux using 3.7.9 image of alpine-python Not able to find any solution on how to get the below issues resolved, any assistance would help a lot. error[E0658]: or-patterns syntax is…
Kiran
  • 21
  • 5
0
votes
1 answer

Replacing all functions with 'pass', syncing private to public github repo

I'd like to create a repository for a proprietary python module, similarly how this python package mlfinlabs. They have emptied out all functions like this: def bet_size_dynamic(current_pos, max_pos, market_price, forecast_price,…
semyd
  • 430
  • 4
  • 17
-6
votes
1 answer

How to determine whether a line of Python has a comment, and split the line into `[code, comment]`

I'm wondering how to determine (True / False) whether a line of Python code has a comment split the line into code, comment Something such as: loc_1 = "print('hello') # this is a comment" Is very straightforward, but something such as: loc_2 =…
baxx
  • 3,956
  • 6
  • 37
  • 75