Registry / testing / redbaron

redbaron

JSON →
library0.9.2pypypi✓ verified 87d ago

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 redbaron
INSTALL
IMPORT
SIG · REDBARON
R
redbaron
testingpythonv0.9.2
Install
1.7s avg
Import
2942ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.9.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.940 runs
installs and imports cleanly · install 0.0s · import 3.111s · 18.7MB
glibc
py 3.103.940 runs
installs and imports cleanly · install 1.7s · import 2.773s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RedBaron
from redbaron import RedBaron

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.

from redbaron import RedBaron code = """ def my_function(arg1, arg2): value = 42 print(f"Hello {value}") """ red = RedBaron(code) # Find the function definition by name func_node = red.find("def", name="my_function") # Change the function name func_node.name.value = "my_renamed_function" # Find the variable assignment and change its value value_assignment_node = func_node.find("assignment", target="value") if value_assignment_node: value_assignment_node.value.value = "99" # Add a new line (comment) at the end of the function body # Note: Raw strings might be needed for complex insertions func_node.value.append(" # Added by RedBaron\n") # Dump the modified code back to a string print(red.dumps())
Debug
Known issues
breakingIn version 0.9, the internal structure for annotations changed. Annotations are now members of `DefArgument`, `ListArgument`, and `DictArgument` nodes, and the `TypedNameNode` has been removed. Code directly accessing `TypedNameNode` or expecting the old annotation structure will break.
fix
Update code to access annotations directly from argument nodes and remove references to `TypedNameNode`.
affects: 0.9.0 and later
breakingIn version 0.6, `IntNode.value` changed from returning an `int` to a `string`. Use `IntNode.to_python()` for an evaluated integer. Additionally, the `node.find("name")` shortcut now *only* works with possible node identifiers, raising `AttributeError` otherwise.
fix
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.
affects: 0.6.0 and later
gotchaRedBaron operates on a Full Syntax Tree (FST) provided by Baron, which is a lossless representation of the source code. Unlike a traditional Abstract Syntax Tree (AST), an FST preserves all syntax information, including comments, formatting, and whitespace. While this is powerful for refactoring, users accustomed to ASTs should be aware of this difference in tree structure and node properties when navigating and modifying the tree.
fix
Familiarize yourself with RedBaron's node structure and documentation for FST-specific properties and traversal methods (e.g., `value`, `dumps()`, `fst()`).
affects: All versions
gotchaDespite its powerful capabilities, RedBaron is officially considered in 'alpha' status and 'not battle tested yet' according to its documentation. While the public documented API is intended to be retro-compatible until version 2.0, users should be aware that the project's core might still be 'rough' and continued contributions are welcomed.
fix
Consider its 'alpha' status when planning for production use. Contribute bug reports or fixes to help stabilize the project.
affects: All versions (as per current documentation)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'redbaron'
The redbaron library is not installed in the active Python environment.
fix
pip install redbaron
baron.parser.BaronSyntaxError: Invalid syntax
The string provided to the redbaron.RedBaron() constructor contains invalid Python syntax, which the underlying baron parser cannot process.
fix
Ensure the input string is valid Python code before passing it to RedBaron, or debug the code string being passed.
AttributeError: 'NoneType' object has no attribute 'value'
A search method like .find() returned None because no matching node was found, and a subsequent operation attempted to access an attribute (e.g., 'value', 'name', 'replace') on this None object.
fix
Always check if the result of a find() operation is not None before attempting to access its attributes or perform modifications.
AttributeError: 'RedBaronList' object has no attribute 'replace'
The replace() method (and similar modification methods like insert_before/after) is designed for single RedBaron nodes, not for RedBaronList objects which are returned by methods like find_all() or when accessing a node's children list.
fix
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.
Upgrade
Version history
0.9.2latest on PyPI · released Mar 17, 2019
Audit
Dependencies
baronrequiredRedBaron is built on top of Baron, which provides the Full Syntax Tree (FST) representation of Python code.
pygmentsoptionalOptional dependency for syntax highlighting in shell environments (e.g., IPython) and for certain `.help()` functionalities.
Agent activity
8 hits · last 30 days
node
6
Resources