Skip to main content

A Python library for matching and querying Python AST nodes

Project description

AST Predicates

A Python library for matching and querying Python AST nodes, inspired by libcst's matcher API but designed to work with Python's built-in ast module.

Overview

AST Predicates provides a declarative way to search for and match patterns in Python Abstract Syntax Trees using the standard library's ast module. It offers a more ergonomic alternative to manual AST traversal and isinstance checks.

Features

  • Declarative matching: Define patterns using a clean, composable API
  • Type-safe matchers: Match AST node types with optional attribute constraints
  • Flexible queries: Support for wildcards, sequences, and logical operators
  • Built on stdlib: Works with Python's built-in ast module - no additional dependencies
  • Familiar API: Inspired by libcst matchers

Installation

pip install ast-predicates

Quick Start

import ast
from ast_predicates import matches, Call, Name, Attribute

# Parse some code
tree = ast.parse("foo.bar(42)")

# Find function calls to methods named 'bar'
pattern = Call(func=Attribute(attr="bar"))

# Check if pattern matches
if matches(tree.body[0].value, pattern):
    print("Found a call to a 'bar' method!")

Basic Usage

Matching Node Types

from ast_predicates import matches, FunctionDef

# Match any function definition
pattern = FunctionDef()

# Match function with specific name
pattern = FunctionDef(name="my_function")

Wildcards and Sequences

from pyast_matchers import DoNotCare, OneOf, AllOf

# Match any value (wildcard)
pattern = Call(func=Name(id=DoNotCare()))

# Match one of several possibilities
pattern = BinOp(op=OneOf(Add(), Sub()))

# Match all conditions
pattern = AllOf(
    FunctionDef(name="process"),
    FunctionDef(decorator_list=[...])
)

Finding Matches in a Tree

from pyast_matchers import find_all

tree = ast.parse(source_code)
pattern = Call(func=Name(id="print"))

# Find all matching nodes
matches = find_all(tree, pattern)

License

MIT License - see LICENSE file for details.

Roadmap

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ast_predicates-1.0.2.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ast_predicates-1.0.2-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file ast_predicates-1.0.2.tar.gz.

File metadata

  • Download URL: ast_predicates-1.0.2.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for ast_predicates-1.0.2.tar.gz
Algorithm Hash digest
SHA256 c5390e2950336edb16f71d6c618a260648477fd268ddaa98bfd8b986babf78be
MD5 a5a02b33a689f41319a84ae6370bc02d
BLAKE2b-256 84f8e19ea889f6aaad233b21c670612b080d39bbdfea974574b813d7330298d7

See more details on using hashes here.

File details

Details for the file ast_predicates-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for ast_predicates-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2e279cf61105db8fbba1b3eacab5be964cbd974e1d4e6f8d2d83b0eb5b1b2ca1
MD5 cc261f0ff16ee415484b815508aba912
BLAKE2b-256 e4142c30020862f4fea752c3fecdbe375315471b1c27dda900631e828c03c2af

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page