Install & Compatibility
Where this runs
tested against v0.10.1 · 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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 2.966s · 18.4MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 2.648s · 19MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
parse
✓ from baron import parse
Used to convert a source code string into a Baron Full Syntax Tree (FST).
dumps
✓ from baron import dumps
Used to convert a Baron Full Syntax Tree (FST) back into a source code string.
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
Debug
Known issues
gotchaBaron provides a low-level Full Syntax Tree (FST). For most high-level refactoring and code manipulation tasks, it is strongly recommended to use `RedBaron` instead, which offers a more user-friendly and object-oriented API built on top of Baron.fixConsider `pip install redbaron` and `from redbaron import RedBaron` for common use cases.
affects: 0.1.0 and later
breakingIn version 0.9, the structure for annotations changed. Annotations are now members of `{def,list,dict}_argument` nodes to flatten the data structure, which may break code relying on the previous FST structure for annotated elements.fixReview and update code that accesses or modifies FST nodes related to function, list, or dictionary annotations to reflect the flattened structure.
affects: 0.9.0
breakingVersion 0.6 introduced significant FST structure modifications. Specifically, `def_argument_tuple` nodes were removed, and argument structures became more coherent; the `name` attribute of a `def_argument` node was renamed to `target`, which now points to a dictionary instead of a string.fixInspect and refactor code that directly manipulates argument nodes in the FST, particularly those handling tuples in function definitions, to conform to the new structure.
affects: 0.6.0
gotchaBaron officially supports Python 2 grammar and up to Python 3.7 grammar. Newer Python versions (3.8+) and their syntax features might not be fully supported, potentially leading to parsing errors or an incomplete FST.fixTest Baron thoroughly with Python code leveraging newer syntax features (e.g., assignment expressions, positional-only arguments). If issues arise, consider contributing to Baron's grammar updates or parsing your code with a compatible Python version.
affects: 0.1.0 and later when used with Python 3.8+
Errors
Common errors & fixes
baron.parser.ParsingError: Error, got an unexpected token
Baron's parser encounters valid Python syntax (or what should be valid) that it cannot correctly interpret, often due to limitations in its grammar or how it handles specific formatting and Python versions.
fixIf the code is valid Python, this indicates a bug in Baron. Report the specific code snippet and the full traceback to the Baron GitHub issue tracker.
ParsingError: Error, got an unexpected token DOUBLE_STAR here:
Baron fails to parse Python code containing nested dictionary unpacking using the double-star (**) operator, as seen in newer Python versions, which its parser may not fully support or correctly interpret.
fixTry simplifying the expression to avoid nested dictionary unpacking if possible. If the syntax is standard Python and still causes an error, report it as a bug to the Baron project.
ParsingError: Error, got an unexpected token ENDL here: 1 if 2 <---- here
Baron's parser can misinterpret simple Python keywords or incomplete expressions, incorrectly expecting an ENDL (newline) token in contexts where it's not syntactically expected for valid Python.
fixEnsure the input string provided to Baron is a complete and well-formed Python statement or module. If parsing a single keyword or fragment, provide it within a valid larger context. If it's a valid complete statement, report it as a bug.
ParsingError: Error, got an unexpected token INT here: 1 1+2
Baron's parser may fail to correctly handle Python code that includes leading whitespace before an expression, interpreting the leading space as part of an unexpected token sequence.
fixBefore passing the string to `baron.parse()`, explicitly `strip()` any leading or trailing whitespace from the input string. If the issue persists with trimmed, valid Python code, report it as a bug.
Upgrade
Version history
0.10.1latest on PyPI · released Dec 9, 2021
Audit
Dependencies
rplyrequiredCore parsing engine dependency for generating the Full Syntax Tree. It is often included implicitly or as a transitive dependency.