Baron: Full Syntax Tree for Python

0.10.1 · active · verified Tue Apr 14

Baron is a Full Syntax Tree (FST) library for Python, designed for refactoring and detailed code analysis. Unlike an Abstract Syntax Tree (AST) which discards some syntax information, Baron's FST preserves all original formatting, including comments, empty lines, and spacing, ensuring that converting code to an FST and back exactly reproduces the original source code. The current version is 0.10.1, released in December 2021. Development is active with a focus on bug fixes, minor feature additions, and performance improvements, indicating a measured release cadence.

Warnings

Install

Imports

Quickstart

Demonstrates the core `parse()` and `dumps()` functions to convert Python source code to its FST representation and back. For practical refactoring, the documentation recommends using the higher-level `RedBaron` library which is built upon Baron.

from baron import parse, dumps

source_code = "def example_func(x):\n    return x + 1 # A simple function"
fst = parse(source_code)

# You would typically manipulate the `fst` object here.
# For most refactoring tasks, it's recommended to use RedBaron,
# which provides a higher-level API built on top of Baron.
# For example:
# from redbaron import RedBaron
# red = RedBaron(source_code)
# red.find_node('name', value='example_func').value = 'new_name'
# modified_code = red.dumps()

regenerated_code = dumps(fst)
print("Original code:\n" + source_code)
print("\nRegenerated code from FST:\n" + regenerated_code)
assert source_code == regenerated_code

view raw JSON →