ast-grep-py provides Python bindings for ast-grep, a powerful structural code search and rewrite engine. It enables developers to query and transform code using abstract syntax tree (AST) patterns, supporting multiple programming languages. The current version is 0.42.1, and the library maintains a rapid release cadence with frequent minor and patch updates, often multiple times a month.
pip install ast-grep-pyVerified import paths — ran on the pinned version, not inferred.
Initialize an `SgRoot` with Python code, find patterns like function definitions, and replace `print()` calls with `logger.info()` using ast-grep's pattern matching syntax. Note that `replace()` returns a new `SgRoot` object.
Refer to the official ast-grep pattern documentation at https://ast-grep.github.io/ast-grep/guide/pattern.html
Always pass the `lang` argument for non-Python code to ensure correct parsing and pattern matching.
Always assign the result of `replace()` or similar modification methods to a new variable or reassign to the original variable to capture the changes.
Benchmark critical operations on representative codebases. Consider direct use of the ast-grep CLI for maximum performance if the Python binding overhead is unacceptable.
Install the package using pip: 'pip install ast-grep-py'.
Ensure that 'SgNode' is correctly defined in the 'ast_grep' module or check for typos in the import statement.
Provide the 'pattern' argument when calling 'find', e.g., 'node.find(pattern="print($A)")'.
Ensure that you are calling 'find' on an instance of 'SgNode' or 'SgRoot', not directly on the 'ast_grep' module.
Use a supported language, such as 'python' or 'javascript', when initializing 'SgRoot'.
No dependency data recorded yet.