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

Uploaded Python 3

File details

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

File metadata

  • Download URL: ast_predicates-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 a1864b599f9660bee80433b976904dca28bb7aea45f5133fa71cb6b785907d64
MD5 635f8e75d095169844447b15c5a9f841
BLAKE2b-256 c1ee0452356d5fb3d630ddb19dfd3a4c8dc5d9eee92aa178a7ad480ae33a341f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ast_predicates-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b86883536b0bcff349298f5bb9bb398bbed5837cc1cb19b93c19f429b0014f6b
MD5 2fd59a4df51b28a33eb5a3448ddb3c40
BLAKE2b-256 efcdd4e4e061ccb6efd07359ceb34cb18dd1576aa2395f5c231d8440582ad0ab

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