{"id":6220,"library":"redbaron","title":"RedBaron","description":"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.","status":"maintenance","version":"0.9.2","language":"en","source_language":"en","source_url":"https://github.com/PyCQA/redbaron","tags":["AST","FST","refactoring","code analysis","code transformation","metaprogramming"],"install":[{"cmd":"pip install redbaron","lang":"bash","label":"Basic installation"},{"cmd":"pip install redbaron[pygments]","lang":"bash","label":"With syntax highlighting support"}],"dependencies":[{"reason":"RedBaron is built on top of Baron, which provides the Full Syntax Tree (FST) representation of Python code.","package":"baron","optional":false},{"reason":"Optional dependency for syntax highlighting in shell environments (e.g., IPython) and for certain `.help()` functionalities.","package":"pygments","optional":true}],"imports":[{"symbol":"RedBaron","correct":"from redbaron import RedBaron"}],"quickstart":{"code":"from redbaron import RedBaron\n\ncode = \"\"\"\ndef my_function(arg1, arg2):\n    value = 42\n    print(f\"Hello {value}\")\n\"\"\"\n\nred = RedBaron(code)\n\n# Find the function definition by name\nfunc_node = red.find(\"def\", name=\"my_function\")\n\n# Change the function name\nfunc_node.name.value = \"my_renamed_function\"\n\n# Find the variable assignment and change its value\nvalue_assignment_node = func_node.find(\"assignment\", target=\"value\")\nif value_assignment_node:\n    value_assignment_node.value.value = \"99\"\n\n# Add a new line (comment) at the end of the function body\n# Note: Raw strings might be needed for complex insertions\nfunc_node.value.append(\"    # Added by RedBaron\\n\")\n\n# Dump the modified code back to a string\nprint(red.dumps())\n","lang":"python","description":"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."},"warnings":[{"fix":"Update code to access annotations directly from argument nodes and remove references to `TypedNameNode`.","message":"In 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.","severity":"breaking","affected_versions":"0.9.0 and later"},{"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.","message":"In 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.","severity":"breaking","affected_versions":"0.6.0 and later"},{"fix":"Familiarize yourself with RedBaron's node structure and documentation for FST-specific properties and traversal methods (e.g., `value`, `dumps()`, `fst()`).","message":"RedBaron 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consider its 'alpha' status when planning for production use. Contribute bug reports or fixes to help stabilize the project.","message":"Despite 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.","severity":"gotcha","affected_versions":"All versions (as per current documentation)"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z","problems":[]}