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.0.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.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ast_predicates-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 30a1f614cf417d0132cf5951fccfecc950154fbf26fb891f7bde34dcf3caf00f
MD5 9a571a232e35aa5954b08a5b22bf95e3
BLAKE2b-256 ba4411d9ab8704130f0c2b11a99bd99bc87fca292382313eaec8175c90c34abf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ast_predicates-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cce6a40566e6cfacae45a538264b9f724364bcfdeb0dcb62542640991b643dd2
MD5 368a643be5a57dead624f0ba9b1e969e
BLAKE2b-256 69ac3f54a5573dc9b4d6767e8dc896f21d7af4b6500d438add5f828b56d9604e

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