skillfed

gast

Python AST that abstracts the underlying Python version

gast v0.7.0 20.1M downloads/30d#1,044 on PyPI147
Permissive license BSD 3-Clause Active released

What it is and what it does

GAST is a compatibility layer that normalizes Python's Abstract Syntax Tree across different Python versions. It converts version-specific AST output from the standard ast module into a unified representation, then back again if needed. This solves the problem of AST structure differences between Python 2.7 and Python 3.x versions—for instance, how function arguments, exception handlers, and string literals are represented differently across versions.

The package provides the same API as the standard ast module, with three key additions: gast.parse() produces a normalized tree directly, gast.dump() shows the unified representation, and gast.ast_to_gast() / gast.gast_to_ast() handle bidirectional conversion. It's tested across Python 2.7 through 3.11, making it suitable for tools that need to analyze or transform code written for multiple Python versions without rewriting logic for each version's AST quirks.

Use it for:

  • Building code analysis or refactoring tools that must work with codebases targeting multiple Python versions
  • Writing AST-based linters or formatters that need a single code path for Python 2 and 3 syntax
  • Implementing code generation or transpilation that normalizes input AST before processing
  • Analyzing legacy Python 2 code alongside modern Python 3 code in a unified framework

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Provides a unified Abstract Syntax Tree representation that works across Python 2 and Python 3 versions, converting between the standard ast module's version-specific trees and a normalized GAST format.

Yes. GAST is a low-friction, actively maintained utility with no dependencies, a permissive license, and zero known vulnerabilities. Install it if you're building tools that parse or analyze Python code across multiple versions and need to avoid version-specific AST branching logic.

Install

gast on PyPI

pip

pip install gast

uv

uv add gast

poetry

poetry add gast

Installing gast

Before you install

Installation is straightforward with no runtime dependencies. The package is actively maintained with a recent release and steady commit activity, making it reliable for production use.

License in practice

Licensed under BSD 3-Clause, a permissive license that allows free use, modification, and distribution with minimal restrictions, suitable for both open-source and commercial projects.

Quickstart

import ast
import gast

code = "x = 1 + 2"
tree = ast.parse(code)
gtree = gast.ast_to_gast(tree)
# Process gtree with version-independent AST
original_tree = gast.gast_to_ast(gtree)

Verify before relying

  • Whether the package is actively used by major tools or frameworks that depend on cross-version AST compatibility
  • Performance characteristics when converting large or deeply nested ASTs

Package facts

License BSD 3-Clause (permissive)
Python support supports the current Python release (!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7)
Install friction low — pure-Python wheel
Runtime dependencies none
Maintenance actively maintained — 258 days since the last release
Last repo commit
First released
Downloads 20,135,601/month — #1,044 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: gast-0.7.0-py3-none-any.whl

Development Status :: 4 - BetaEnvironment :: ConsoleIntended Audience :: DevelopersLicense :: OSI Approved :: BSD LicenseNatural Language :: EnglishProgramming Language :: Python :: 2Programming Language :: Python :: 2.7Programming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.4Programming Language :: Python :: 3.5Programming Language :: Python :: 3.6Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9

Tags

python ast compatibility layercross-version abstract syntax treeast normalization python 2 3unified ast representationast conversion between python versionsgeneric ast parserpython version-agnostic ast
ast-parsingcode-analysispython-compatibility

More Software Development packages