RedBaron is a Python library built on top of Baron, providing a higher-level abstraction for working with Python source code as a Full Syntax Tree (FST). It aims to simplify tasks like refactoring, code analysis, and programmatic code modification while preserving all syntax information, including comments and formatting. The library is currently at version 0.9.2, with its last release in March 2019, indicating a maintenance or slow development cadence.
pip install redbaronVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse Python code into a RedBaron FST, find a function, modify its name and a variable's value within it, add a new line, and then convert the modified FST back into a Python string. RedBaron's API is designed to be intuitive for tree traversal and modification.
Update code to access annotations directly from argument nodes and remove references to `TypedNameNode`.
Replace `.value` access on `IntNode` with `.to_python()` when an integer is needed. Ensure `node.find()` is used with actual node identifiers or use more general searching methods for other attributes.
Familiarize yourself with RedBaron's node structure and documentation for FST-specific properties and traversal methods (e.g., `value`, `dumps()`, `fst()`).
Consider its 'alpha' status when planning for production use. Contribute bug reports or fixes to help stabilize the project.
pip install redbaron
Ensure the input string is valid Python code before passing it to RedBaron, or debug the code string being passed.
Always check if the result of a find() operation is not None before attempting to access its attributes or perform modifications.
Iterate over the RedBaronList and apply the modification method to individual nodes, or use list manipulation methods (e.g., slicing, append) on the RedBaronList itself if applicable.