Skip to main content

ast

Build, inspect, and modify Python ASTs. Powers linters, codemods, notebooks, and the compiler front-end.

Source-of-record: Lib/ast.py, Parser/Python.asdl, ast docs.

Entry points

FunctionReturns
parse(source, filename='<unknown>', mode='exec', *, type_comments=False, feature_version=None, optimize=-1)Module / Expression.
dump(node, annotate_fields=True, include_attributes=False, *, indent=None)Debug string.
unparse(ast_obj) (3.9+)Source text.
literal_eval(node_or_string)Safe constant eval.
walk(node)Pre-order iterator.
iter_fields(node) / iter_child_nodes(node)Field iterators.
fix_missing_locations(node)Propagate lineno/col_offset.
increment_lineno(node, n=1) / copy_location(new, old)Adjust positions.
get_docstring(node, clean=True) / get_source_segment(source, node, *, padded=False)Helpers.
compare(a, b, *, compare_attributes=False) (3.14+)Structural equality.

Visitors

NodeVisitor, NodeTransformer. NodeVisitor.generic_visit(node) recurses by default; visit_<ClassName> dispatches.

Node hierarchy

Root: AST. Major branches:

BranchExamples
modModule, Interactive, Expression, FunctionType.
stmtFunctionDef, AsyncFunctionDef, ClassDef, Return, Delete, Assign, AugAssign, AnnAssign, For, AsyncFor, While, If, With, AsyncWith, Match, Raise, Try, TryStar, Assert, Import, ImportFrom, Global, Nonlocal, Expr, Pass, Break, Continue, TypeAlias (3.12+).
exprBoolOp, NamedExpr, BinOp, UnaryOp, Lambda, IfExp, Dict, Set, ListComp, SetComp, DictComp, GeneratorExp, Await, Yield, YieldFrom, Compare, Call, FormattedValue, JoinedStr, Constant, Attribute, Subscript, Starred, Name, List, Tuple, Slice.
patternMatchValue, MatchSingleton, MatchSequence, MatchMapping, MatchClass, MatchStar, MatchAs, MatchOr.
type_param (3.12+)TypeVar, ParamSpec, TypeVarTuple.

Position attributes

lineno, col_offset, end_lineno (3.8+), end_col_offset (3.8+).

Gopy status

AreaState
parse, dump, unparseComplete.
NodeVisitor, NodeTransformerComplete.
Match patterns, TryStar, TypeAliasComplete.
literal_evalComplete.

Reference

  • CPython 3.14: ast.
  • Lib/ast.py, Parser/Python.asdl.
  • module/ast/. gopy port.