{"id":867,"library":"gast","title":"gast: Generic Python AST","description":"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.","status":"active","version":"0.7.0","language":"python","source_language":"en","source_url":"https://github.com/serge-sans-paille/gast/","tags":["AST","Python2","Python3","compatibility","parser","abstract syntax tree"],"install":[{"cmd":"pip install gast","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Core dependency for execution.","package":"Python","optional":false}],"imports":[{"symbol":"gast","correct":"import gast"},{"note":"gast provides all standard `ast` nodes, but some might be harmonized across versions (e.g., `gast.Constant`).","symbol":"Node","correct":"from gast import Name, Constant"},{"note":"gast.parse directly produces a GAST node, unlike ast.parse which produces a standard AST node.","wrong":"ast.parse(code) if expecting a GAST node","symbol":"parse","correct":"gast.parse(code)"}],"quickstart":{"code":"import gast\nimport ast\n\ncode = \"\"\"def foo(x):\n    return x + 1\"\"\"\n\n# Parse code into a GAST (Generic AST) node\ngast_tree = gast.parse(code)\nprint(\"GAST Tree (using gast.dump):\\n\" + gast.dump(gast_tree))\n\n# Convert the GAST tree to a standard AST tree for the current Python version\nstd_ast_tree = gast.gast_to_ast(gast_tree)\nprint(\"\\nStandard AST Tree (from GAST, using ast.dump):\\n\" + ast.dump(std_ast_tree))\n\n# Example of converting a standard AST to a GAST tree\nstd_ast_input = ast.parse(\"a = 'hello'\")\ngast_from_std = gast.ast_to_gast(std_ast_input)\nprint(\"\\nGAST Tree (from standard AST input, using gast.dump):\\n\" + gast.dump(gast_from_std))\n","lang":"python","description":"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."},"warnings":[{"fix":"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+.","message":"For Python 3.8 and later, several `ast` node types like `ast.Num`, `ast.Str`, `ast.Bytes`, `ast.Ellipsis`, and `ast.NamedConstant` are replaced by `gast.Constant` in `gast` ASTs (since `gast >= 0.3.0`). Code that directly inspects or creates these specific node types must be updated to use `gast.Constant` for cross-version compatibility.","severity":"breaking","affected_versions":"gast >= 0.3.0, Python >= 3.8"},{"fix":"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.","message":"The representation of certain AST nodes changes in Python 3.9+. For instance, `ast.arg` nodes are represented as `ast.Name` with an `ast.Param` context, and `ExceptHandler.name` is an `ast.Name` with an `ast.Store` context instead of a simple string.","severity":"gotcha","affected_versions":"gast, Python >= 3.9"},{"fix":"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`.","message":"The interpretation of literals like `None`, `True`, and `False` differs between Python 2 and Python 3. In Python 3, they are parsed as `gast.Constant`, while in Python 2, they are parsed as `gast.Name`.","severity":"gotcha","affected_versions":"gast, Python 2 vs Python 3"},{"fix":"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.","message":"While `gast` provides an API compatible with `ast`, directly importing or using classes from the standard `ast` module when you intend to work with `gast`'s cross-version AST can lead to unexpected behavior or `AttributeError` if the underlying `ast` module's structure for that node differs from `gast`'s harmonized representation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"The `_field_types` attribute, which exists for each AST class in standard `ast` modules, is always empty in `gast` for Python versions before 3.10. Relying on this field for introspection might yield incomplete information on older Python versions.","severity":"gotcha","affected_versions":"gast, Python < 3.10"}],"env_vars":null,"last_verified":"2026-05-12T20:32:51.358Z","next_check":"2026-06-27T00:00:00.000Z","problems":[{"fix":"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.","cause":"The 'gast' library is not installed in the current Python environment or the Python interpreter cannot locate it in its search path.","error":"ModuleNotFoundError: No module named 'gast'"},{"fix":"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.","cause":"This error typically occurs when a dependent library (like an older version of TensorFlow) expects specific AST nodes (like `Num`, `Str`, `Bytes`) that have been deprecated and replaced by `gast.Constant` in newer `gast` versions (e.g., gast >= 0.3.0 or >= 0.4.0).","error":"AttributeError: module 'gast' has no attribute 'Num'"},{"fix":"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.","cause":"Similar to the `Num` error, this indicates a breaking API change in 'gast' where the `Index` AST node (or its equivalent) was removed or modified in a newer version, while an older dependent package still tries to access it.","error":"AttributeError: module 'gast' has no attribute 'Index'"},{"fix":"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`).","cause":"This is a package management conflict, often encountered with system package managers (like `pacman` on Arch Linux), where different installed packages require mutually exclusive versions of 'gast' (e.g., one requires `python-gast` and another `python-gast03`).","error":"python-gast03 and python-gast are in conflict"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.7.0","cli_name":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"18.0M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"18.0M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"18M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"18M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":0.8,"disk_size":"19.9M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":0.8,"disk_size":"19.9M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.6,"import_time_s":0.02,"mem_mb":0.8,"disk_size":"20M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":0.8,"disk_size":"20M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":0.8,"disk_size":"11.8M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":0.8,"disk_size":"11.8M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":0.02,"mem_mb":0.8,"disk_size":"12M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":0.8,"disk_size":"12M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1,"disk_size":"11.5M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1,"disk_size":"11.4M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":0.02,"mem_mb":0.8,"disk_size":"12M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":0.8,"disk_size":"12M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"17.5M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"17.5M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.7,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"18M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"18M"}]},"quickstart_checks":{"last_tested":"2026-04-24","tag":null,"tag_description":null,"results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}