gast provides a compatibility layer between the Abstract Syntax Trees (AST) of various Python versions (including Python 2 and Python 3), offering a homogeneous API compatible with the standard library `ast` module. It abstracts away version-specific differences in the AST structure, making it easier to write tools that work across multiple Python versions. The library is currently at version 0.7.0 and remains actively maintained.
pip install gastVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse Python code into a `gast` AST, convert a `gast` AST to a standard `ast` module AST, and convert a standard `ast` to a `gast` AST. The `gast.dump` function is used for easy inspection of the GAST structure.
Replace usages of `gast.Num`, `gast.Str`, etc., with `gast.Constant(value=..., kind=None)` when working with GAST trees, especially when targeting Python 3.8+.
When traversing or manipulating GASTs, be aware of these structural changes and use appropriate attribute access or type checks, or rely on `gast`'s conversion functions (`gast.gast_to_ast`, `gast.ast_to_gast`) to handle discrepancies.
When writing tools that target both Python 2 and Python 3 via `gast`, ensure your code can handle these literals potentially being either `gast.Constant` or `gast.Name`.
Always import and use node types directly from `gast` (e.g., `gast.Name`, `gast.Constant`) when building or manipulating GAST trees to ensure full compatibility and abstraction across Python versions.
Avoid relying on `_field_types` for critical logic if your tool needs to function correctly across a wide range of Python versions. Consider alternative introspection methods or conditional logic based on Python version.
Install the 'gast' library using pip: `pip install gast` or ensure your environment variables (like PYTHONPATH) are correctly set if it's installed in a non-standard location.
Downgrade 'gast' to an older version compatible with the dependent library (e.g., `pip install gast==0.2.2` or `pip install gast==0.3.3`), or upgrade the dependent library to one that supports newer `gast` versions.
Downgrade 'gast' to a version compatible with the problematic dependent library (e.g., `pip install gast==0.3.3`) or update the dependent library to one that supports the current 'gast' version.
Identify which packages require conflicting 'gast' versions. You may need to remove one of the conflicting packages, or use a virtual environment to isolate the dependencies, or check for updated packages that resolve the conflict (e.g., `sudo pacman -R python-gast` and then `sudo pacman -Syu` to allow the update to install `python-gast03`).