ast-grep-py
Structural Search and Rewrite code at large scale using precise AST pattern.
What it is and what it does
ast-grep-py exposes ast-grep's code search and rewriting engine to Python programs. It lets you define patterns that match code structure—not just text—and then find, analyze, or transform matching code at scale. The package is a native Rust binding compiled to wheels, so it has no Python runtime dependencies and runs efficiently on modern Python versions.
You would use this when you need to perform structural code analysis or refactoring that text regex cannot handle: finding all calls to a deprecated function across a codebase, rewriting patterns to use a new API, or enforcing code style rules based on syntax tree shape. The API is programmatic, so you integrate it into scripts, linters, or CI pipelines rather than using a command-line tool.
Use it for:
- Search for and refactor all uses of a deprecated API across a large codebase in one pass.
- Enforce custom code style rules by matching specific AST patterns and flagging violations.
- Automate codemod tasks—e.g., migrate from one library to another—by pattern-matching and rewriting code structure.
- Build custom linters that detect anti-patterns or security issues based on syntax tree shape.
- Analyze code structure programmatically to gather metrics or generate reports on code patterns.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
ast-grep-py is a Python binding for ast-grep, a tool that searches, lints, and rewrites code using precise abstract syntax tree patterns.
Yes, if you need structural code search or rewriting beyond regex. The package is actively maintained, has no security vulnerabilities, uses a permissive MIT license, and is well-distributed across platforms. Medium install friction is acceptable for a compiled tool. Start with the official API guide to confirm the pattern syntax and API surface fit your use case.
Install
ast-grep-py on PyPI
pip
pip install ast-grep-pyuv
uv add ast-grep-pypoetry
poetry add ast-grep-pyInstalling ast-grep-py
Before you install
Medium install friction due to compiled wheels, but well-supported across multiple Python versions on Linux, macOS, and Windows. Active maintenance with a release 7 days old and 15516 repository stars.
License in practice
MIT license is permissive; you may use, modify, and distribute this package freely in commercial or private projects with minimal restrictions.
Quickstart
pip install ast-grep-py
# See https://ast-grep.github.io/guide/api-usage.html for pattern and search examples
Requires Python 3.8 or later; prebuilt wheels are available for common platforms, but installation may require a Rust toolchain if building from source.
Verify before relying
- Whether the package provides a high-level convenience API or requires deep familiarity with AST and pattern syntax to be productive.
- Performance characteristics when searching or rewriting large codebases.
- Availability of pattern libraries or templates for common refactoring tasks.
- Concrete example of how to invoke the binding's main entry points after import.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | medium — platform-specific wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 7 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 1,163,264/month — #4,281 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: ast_grep_py-0.45.1-cp310-cp310-manylinux_2_28_aarch64.whl; ast_grep_py-0.45.1-cp310-cp310-manylinux_2_28_x86_64.whl; ast_grep_py-0.45.1-cp310-cp310-win32.whl; ast_grep_py-0.45.1-cp310-cp310-win_amd64.whl; ast_grep_py-0.45.1-cp311-cp311-macosx_10_12_x86_64.whl; ast_grep_py-0.45.1-cp311-cp311-macosx_11_0_arm64.whl; ast_grep_py-0.45.1-cp311-cp311-manylinux_2_28_aarch64.whl; ast_grep_py-0.45.1-cp311-cp311-manylinux_2_28_x86_64.whl; ast_grep_py-0.45.1-cp311-cp311-win32.whl; ast_grep_py-0.45.1-cp311-cp311-win_amd64.whl; ast_grep_py-0.45.1-cp312-cp312-macosx_10_12_x86_64.whl; ast_grep_py-0.45.1-cp312-cp312-macosx_11_0_arm64.whl; ast_grep_py-0.45.1-cp312-cp312-manylinux_2_28_aarch64.whl; ast_grep_py-0.45.1-cp312-cp312-manylinux_2_28_x86_64.whl; ast_grep_py-0.45.1-cp312-cp312-win32.whl; ast_grep_py-0.45.1-cp312-cp312-win_amd64.whl; ast_grep_py-0.45.1-cp313-cp313-macosx_10_12_x86_64.whl; ast_grep_py-0.45.1-cp313-cp313-macosx_11_0_arm64.whl; ast_grep_py-0.45.1-cp313-cp313-manylinux_2_28_aarch64.whl; ast_grep_py-0.45.1-cp313-cp313-manylinux_2_28_x86_64.whl
Keywords: ast, pattern, codemod, structural search, rewrite
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
ast-grep-cliast-grep-cli is a command-line tool for…
permissive · top 5,000 on PyPI
gritqlGritQL is a Python binding for a declarative…
permissive · top 15,000 on PyPI
tensor-grepA high-performance grep-compatible command-line…
unclear · top 15,000 on PyPI
ast-serializeParses Python source files and serializes their…
permissive · top 1,000 on PyPI
grep-astSearch source code files by pattern and display…
permissive · top 5,000 on PyPI
libcstlibcst parses Python source code into a…
permissive · top 1,000 on PyPI
ignore-pythonProvides fast recursive directory traversal…
unclear · top 15,000 on PyPI
ast-decompilerConverts Python abstract syntax trees (ASTs)…
permissive · top 15,000 on PyPI
astorastor converts Python abstract syntax trees…
permissive · top 5,000 on PyPI
rtestA Python test runner built in Rust that uses…
permissive · top 15,000 on PyPI