pyast

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 24 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compile

func Compile(source []byte, filename string) error

Compile compiles source bytes like compile(source, filename, "exec") and returns the error it raises

func ConstantRepr

func ConstantRepr(v any) (string, error)

ConstantRepr renders a constant value with repr, raising the int digit limit error CPython raises

func LiteralEval

func LiteralEval(source string) (any, error)

LiteralEval evaluates a Python literal expression from source text like ast.literal_eval(str)

func LiteralEvalNode

func LiteralEvalNode(n *Node) (v any, err error)

LiteralEvalNode evaluates an expression node, or the body of an Expression, like ast.literal_eval(node)

func LiteralStrList

func LiteralStrList(text string) ([]string, bool, error)

Items of a list literal rendered with str, false on ValueError, SyntaxError or a non list, other exceptions returned

func ParseWith

func ParseWith(source string, opts Options) (*Node, []Warning, error)

ParseWith parses source text like ast.parse with options, returning the tree and the warnings it emitted

func Unparse

func Unparse(n *Node) (s string, err error)

Unparse renders a tree back to source text exactly as ast.unparse

Types

type Complex

type Complex struct{ Real, Imag float64 }

Complex is a complex constant such as an imaginary literal

func (Complex) PyRepr

func (c Complex) PyRepr() string

PyRepr renders the number as complex.__repr__

func (Complex) PyTypeName

func (Complex) PyTypeName() string

PyTypeName names the Python class

type Dict

type Dict struct{ Keys, Values []any }

Dict is a literal dict keeping insertion order, its keys compared with Python equality

func (*Dict) PyItems

func (d *Dict) PyItems() iter.Seq2[any, any]

PyItems yields the pairs in insertion order for pyrepr

func (*Dict) PyTypeName

func (*Dict) PyTypeName() string

PyTypeName names the Python type for pyrepr

type Ellipsis

type Ellipsis struct{}

Ellipsis is the ... constant

func (Ellipsis) PyRepr

func (Ellipsis) PyRepr() string

PyRepr renders the constant as Python prints it

func (Ellipsis) PyTypeName

func (Ellipsis) PyTypeName() string

PyTypeName names the Python class

type IntDigitsError

type IntDigitsError struct{}

IntDigitsError is the ValueError CPython raises converting an int over 4300 decimal digits to text

func (*IntDigitsError) Error

func (e *IntDigitsError) Error() string

type LimitError

type LimitError struct{ Class, Msg string }

LimitError is the MemoryError raised when the source nests deeper than the parser stack allows

func (*LimitError) Error

func (e *LimitError) Error() string

type LiteralError

type LiteralError struct{ Class, Msg string }

LiteralError is the ValueError or TypeError literal_eval raises for a node that is not a literal

func (*LiteralError) Error

func (e *LiteralError) Error() string

type Node

type Node struct {
	Type                                       string
	Lineno, ColOffset, EndLineno, EndColOffset int
	// contains filtered or unexported fields
}

Node is one ast.AST instance with its fields in _fields order and its location attributes

func IterChildNodes

func IterChildNodes(n *Node) []*Node

IterChildNodes yields direct child nodes in field order like ast.iter_child_nodes

func NewNode

func NewNode(typ string, vals ...any) *Node

NewNode builds a node of the named class from field values given in _fields order

func Parse

func Parse(source string) (*Node, error)

Parse parses source text like ast.parse(source) in exec mode

func ParseBytes

func ParseBytes(source []byte) (*Node, error)

ParseBytes parses encoded source like ast.parse(bytes), honouring a UTF-8 BOM and a PEP 263 coding declaration

func ParseExpression

func ParseExpression(source string) (*Node, error)

ParseExpression parses source in eval mode like ast.parse(source, mode='eval'), returning an Expression

func Walk

func Walk(n *Node) []*Node

Walk yields every node of the tree breadth first like ast.walk

func (*Node) Child

func (n *Node) Child(name string) *Node

Child returns a node valued field or nil

func (*Node) Children

func (n *Node) Children(name string) []*Node

Children returns a list of nodes field or nil

func (*Node) Fields

func (n *Node) Fields() []string

Fields lists the _fields names in order

func (*Node) Get

func (n *Node) Get(name string) any

Get returns a field value, nil when the class has no such field or the field holds None

func (*Node) HasLocation

func (n *Node) HasLocation() bool

HasLocation reports whether the class carries lineno and col_offset attributes

func (*Node) Int

func (n *Node) Int(name string) (int, bool)

Int returns an int valued field and whether it is set

func (*Node) Is

func (n *Node) Is(types ...string) bool

Is reports whether the node exists and has one of the class names

func (*Node) Names

func (n *Node) Names(name string) []string

Names returns an identifier list field

func (*Node) Repr

func (n *Node) Repr() (string, error)

Repr renders the node as the 3.14 AST repr, three levels deep with long lists elided

func (*Node) Set

func (n *Node) Set(name string, v any)

Set replaces a field value

func (*Node) Str

func (n *Node) Str(name string) (string, bool)

Str returns an identifier or string field and whether it is set

type Options

type Options struct {
	Filename       string
	Mode           string
	TypeComments   bool
	FeatureVersion *int
	// FeatureMajor is the major of a (major, minor) feature_version tuple whose minor is FeatureVersion
	FeatureMajor     *int
	Optimize         int
	WarningsAsErrors bool
	// contains filtered or unexported fields
}

Options holds the ast.parse arguments filename, mode, type_comments, feature_version and optimize and the warnings error filter

type Set

type Set []any

Set is a literal set holding distinct members in insertion order

func (Set) PyRepr

func (s Set) PyRepr() string

PyRepr renders the set as set.__repr__ does for its members

func (Set) PyTypeName

func (Set) PyTypeName() string

PyTypeName names the Python type for pyrepr

type SyntaxError

type SyntaxError struct {
	Class                                string
	Msg                                  string
	Lineno, Offset, EndLineno, EndOffset int
	Text                                 string
	HasLineno, HasText, HasEnd           bool
	Filename                             string
}

SyntaxError is the SyntaxError, IndentationError or TabError ast.parse raises

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

Error renders str(exc) with the <unknown> filename ast.parse passes

type UnparseError

type UnparseError struct{ Msg string }

UnparseError is the ValueError ast.unparse raises for a node it cannot render

func (*UnparseError) Error

func (e *UnparseError) Error() string

type Warning

type Warning struct {
	Category string
	Msg      string
	Filename string
	Lineno   int
}

Warning is a SyntaxWarning or DeprecationWarning ast.parse passes to warnings.warn_explicit

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL