npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ast-search-python

v1.6.1

Published

Python language plugin for ast-search

Readme

ast-search-python

npm

Python language plugin for ast-search-js. Adds .py / .pyw file support using tree-sitter S-expression queries.

Table of Contents

Installation

npm install -g ast-search-js
npm install -g ast-search-python

Usage

Pass --plugin ast-search-python to enable Python file support:

ast-search <query> --plugin ast-search-python [--dir <path>] [--format <fmt>] [--lang python]

| Argument | Description | Default | | -------------- | -------------------------------------------------------------- | ----------- | | <query> | Shorthand or tree-sitter S-expression (see Query Syntax below) | required | | -d, --dir | Root directory to search | current dir | | -f, --format | Output format: text, json, files, or count | text | | -l, --lang | Restrict search to python only (useful in mixed-language repos) | all languages | | -p, --plugin | ast-search-python | required | | -C, --context | Show N lines of context around each match (like grep -C) | 0 | | --ast | Print AST for a code snippet or --file; use with --lang python for Python ASTs | off |

Example

Find all functions that raise an exception:

ast-search '(function_definition body: (block (raise_statement) @r) @b) @fn' --plugin ast-search-python

Using the shorthand:

ast-search 'raise' --plugin ast-search-python

Query syntax

Python queries use tree-sitter S-expression syntax. A few examples:

# Find all function definitions (shorthand)
ast-search 'fn' --plugin ast-search-python

# Find all class definitions (raw S-expression)
ast-search '(class_definition) @cls' --plugin ast-search-python

# Find all calls to a specific function by name
ast-search '(call function: (identifier) @n (#eq? @n "my_func")) @c' --plugin ast-search-python

# Find calls to any function matching a regex (using #match? predicate)
ast-search '(call function: (identifier) @n (#match? @n "^(get|post|put|delete)_")) @c' --plugin ast-search-python

# Restrict to only Python files in a mixed-language repo
ast-search 'fn' --lang python --plugin ast-search-python

Raw S-expression queries must include at least one @capture_name — tree-sitter requires it to return results. Shorthands include @_ automatically.

Named captures (@name, excluding @_) appear in the captures field of each match. All captures from a single pattern application are grouped on one result. In text output they appear after |; in JSON they're in a captures object.

# Find logging calls — capture the method name and string argument
ast-search '(call function: [(identifier)(attribute)] @fn (#match? @fn "^(log|info|warn|error)") arguments: (argument_list (string) @msg)) @call' --plugin ast-search-python
# text output: src/app.py:5:0: logging.info("msg") | fn=logging.info msg="msg" call=logging.info("msg")

Shorthands

| Shorthand | Expands to | | ----------- | ----------------------------------- | | fn | (function_definition) @_ | | call | (call) @_ | | class | (class_definition) @_ | | assign | (assignment) @_ | | return | (return_statement) @_ | | await | (await) @_ | | yield | (yield) @_ | | import | (import_statement) @_ | | from | (import_from_statement) @_ | | if | (if_statement) @_ | | for | (for_statement) @_ | | while | (while_statement) @_ | | raise | (raise_statement) @_ | | with | (with_statement) @_ | | lambda | (lambda) @_ | | decorator | (decorator) @_ | | augassign | (augmented_assignment) @_ | | comp | (list_comprehension) @_ | | dictcomp | (dictionary_comprehension) @_ | | setcomp | (set_comprehension) @_ | | genexp | (generator_expression) @_ | | assert | (assert_statement) @_ | | delete | (delete_statement) @_ | | global | (global_statement) @_ | | nonlocal | (nonlocal_statement) @_ | | decorated | (decorated_definition) @_ |

Note: In tree-sitter-python 0.21+, async def functions are typed as function_definition (no separate async_function_definition node). The fn shorthand matches both sync and async functions. To match only async functions, use a predicate query.

Supported file types

.py .pyw

Plugin API

This package implements the LanguageBackend interface from ast-search-js/plugin. It registers the python language backend automatically when loaded via --plugin ast-search-python, or programmatically:

import { defaultRegistry } from 'ast-search-js/plugin';
const { register } = await import('ast-search-python');
register(defaultRegistry);