skillfed

ast-grep-py

Structural Search and Rewrite code at large scale using precise AST pattern.

ast-grep-py v0.45.1 1.2M downloads/30d#4,281 on PyPI15,516
Permissive license MIT Active released

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-py

uv

uv add ast-grep-py

poetry

poetry add ast-grep-py

Installing 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

Development Status :: 3 - AlphaEnvironment :: ConsoleIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: RustTopic :: SecurityTopic :: Software DevelopmentTopic :: Software Development :: Quality AssuranceTopic :: Text Processing

Tags

ast pattern matching code searchstructural code search and rewriteast-based codemod toolcode linting with ast patternslarge-scale code refactoringsyntax tree pattern matchingprogrammatic code analysis
code-searchast-analysiscodemod

More Software Development packages