Tool for exploring the AST of given Python source code.
Project description
ast-explore
Tool for exploring the AST of given Python source code.
Installation
This package is available on PyPI. To use it, you can either install with pip:
pip install ast-explore
or use it with uv directly:
uvx ast-explore --help
Usage
At a minimum, you must provide a Python file to analyze:
# previously-installed with pip:
ast-explore source_code.py
# with uv:
uvx ast-explore source_code.py
This will extract all possible data points from every node in the AST generated from that source code and print it to the screen. You may wish to pipe the results to a file or less to process.
Here's a partial example of the output:
๐ Reading Python source code from /.../source_code.py...
๐ Parsing into a Python 3.14 AST...
โ
Ready to explore the AST! Starting depth-first traversal...
********************************************************************************
** 1. Module (https://docs.python.org/3.14/library/ast.html#ast.Module) **
********************************************************************************
๐ฒ Path to this node from the root node of the AST:
Module
๐ This node type does not have any line number information.
๐ Docstring is missing.
โจ AST node-specific fields and their values:
- body : [FunctionDef(n...ype_params=[])]
- type_ignores : []
********************************************************************************
2. FunctionDef (https://docs.python.org/3.14/library/ast.html#ast.FunctionDef)
********************************************************************************
๐ฒ Path to this node from the root node of the AST:
Module -> FunctionDef
๐ Source code represented by the node:
1 | def strip_password(x: dict[str, str]) -> None:
2 | try:
3 | del x['password']
4 | except KeyError:
5 | pass
๐ Location in the source code:
- lineno : 1
- end_lineno : 5
- col_offset : 0
- end_col_offset : 12
๐ Docstring is missing.
โจ AST node-specific fields and their values:
- name : 'strip_password'
- args : arguments(pos..., defaults=[])
- body : [Try(body=[Del... finalbody=[])]
- decorator_list : []
- returns : Constant(value...ne, kind=None)
- type_comment : None
- type_params : []
********************************************************************************
3. arguments (https://docs.python.org/3.14/library/ast.html#ast.arguments)
********************************************************************************
๐ฒ Path to this node from the root node of the AST:
Module -> FunctionDef -> arguments
๐ This node type does not have any line number information.
โจ AST node-specific fields and their values:
- posonlyargs : []
- args : [arg(arg='x', ..._comment=None)]
- vararg : None
- kwonlyargs : []
- kw_defaults : []
- kwarg : None
- defaults : []
********************************************************************************
** 4. arg (https://docs.python.org/3.14/library/ast.html#ast.arg) **
********************************************************************************
๐ฒ Path to this node from the root node of the AST:
Module -> FunctionDef -> arguments -> arg
๐ Source code represented by the node:
1 | def strip_password(x: dict[str, str]) -> None:
| ^^^^^^^^^^^^^^^^^
๐ Location in the source code:
- lineno : 1
- end_lineno : 1
- col_offset : 19
- end_col_offset : 36
โจ AST node-specific fields and their values:
- arg : 'x'
- annotation : Subscript(val...), ctx=Load())
- type_comment : None
Specifying nodes of interest
Use the --types argument to list the nodes you want to explore and only information about those nodes will be shown. Here, we only care about function definition nodes:
# previously-installed with pip:
ast-explore source_code.py --types FunctionDef AsyncFunctionDef
# with uv:
uvx ast-explore source_code.py --types FunctionDef AsyncFunctionDef
Here's an example of the result (there are no async functions in the file analyzed):
๐ Reading Python source code from /.../source-code.py...
๐ Parsing into a Python 3.14 AST...
โ
Ready to explore the AST! Starting depth-first traversal...
********************************************************************************
1. FunctionDef (https://docs.python.org/3.14/library/ast.html#ast.FunctionDef)
********************************************************************************
๐ฒ Path to this node from the root node of the AST:
Module -> FunctionDef
๐ Source code represented by the node:
1 | def strip_password(x: dict[str, str]) -> None:
2 | try:
3 | del x['password']
4 | except KeyError:
5 | pass
๐ Location in the source code:
- lineno : 1
- end_lineno : 5
- col_offset : 0
- end_col_offset : 12
๐ Docstring is missing.
โจ AST node-specific fields and their values:
- name : 'strip_password'
- args : arguments(pos..., defaults=[])
- body : [Try(body=[Del... finalbody=[])]
- decorator_list : []
- returns : Constant(value...ne, kind=None)
- type_comment : None
- type_params : []
********************************************************************************
๐ Traversal completed!
Interactive mode
Use interactive mode to step through the AST one node at a time:
# previously-installed with pip:
ast-explore source_code.py --interactive
# with uv:
uvx ast-explore source_code.py --interactive
Here's what that looks like:
๐ Reading Python source code from /.../source_code.py...
๐ Parsing into a Python 3.14 AST...
โ
Ready to explore the AST! Starting depth-first traversal...
********************************************************************************
** 1. Module (https://docs.python.org/3.14/library/ast.html#ast.Module) **
********************************************************************************
๐ฒ Path to this node from the root node of the AST:
Module
๐ This node type does not have any line number information.
โ Do you want more information on this node? [y]es [n]o [q]uit:
Of course, you can combine --interactive with --types to customize your exploration. Here we interactively visit all try blocks:
# previously-installed with pip:
ast-explore source_code.py --interactive --types Try
# with uv:
uvx ast-explore source_code.py --interactive --types Try
Note that the first node we encounter is now the ast.Try node and not the ast.Module node we saw without specifying --types:
๐ Reading Python source code from /.../source_code.py...
๐ Parsing into a Python 3.14 AST...
โ
Ready to explore the AST! Starting depth-first traversal...
********************************************************************************
** 1. Try (https://docs.python.org/3.14/library/ast.html#ast.Try) **
********************************************************************************
๐ฒ Path to this node from the root node of the AST:
Module -> FunctionDef -> Try
๐ Source code represented by the node:
2 | try:
3 | del x['password']
4 | except KeyError:
5 | pass
โ Show location fields? [y]es [n]o [q]uit: y
๐ Location in the source code:
- lineno : 2
- end_lineno : 5
- col_offset : 4
- end_col_offset : 12
โ Do you want more information on this node? [y]es [n]o [q]uit: y
โจ AST node-specific fields and their values:
- body : [Delete(target..., ctx=Del())])]
- handlers : [ExceptHandler...body=[Pass()])]
- orelse : []
- finalbody : []
โ Continue traversal? [y]es [n]o [q]uit:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ast_explore-0.1.0.tar.gz.
File metadata
- Download URL: ast_explore-0.1.0.tar.gz
- Upload date:
- Size: 39.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e656a02a293c79525fea1aa0f29fc4affc07796ff41a3c979caf7511403f2a3
|
|
| MD5 |
b21b3fd91bb1bbcec3908e80272cf0a9
|
|
| BLAKE2b-256 |
556a71f75fb8518faec18cf7330b99ed1023f5e87a72f3bbf07b8a0d5d05645d
|
Provenance
The following attestation bundles were made for ast_explore-0.1.0.tar.gz:
Publisher:
pypi-publish.yml on stefmolin/ast-explore
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ast_explore-0.1.0.tar.gz -
Subject digest:
7e656a02a293c79525fea1aa0f29fc4affc07796ff41a3c979caf7511403f2a3 - Sigstore transparency entry: 1960394428
- Sigstore integration time:
-
Permalink:
stefmolin/ast-explore@2925eb56742cb49f2be40767d6ba0fb6d85bff09 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/stefmolin
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@2925eb56742cb49f2be40767d6ba0fb6d85bff09 -
Trigger Event:
push
-
Statement type:
File details
Details for the file ast_explore-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ast_explore-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d1277d39ac69b86f4bc157e0f7fd74def871bded1b7763a783d9c7506ae5c29
|
|
| MD5 |
8d14144f8816c2b255837c485a45cdb3
|
|
| BLAKE2b-256 |
d8fd20b3a37078118686bd4b8f727296b2f9078509e0b49e34a6cce3d704cc6e
|
Provenance
The following attestation bundles were made for ast_explore-0.1.0-py3-none-any.whl:
Publisher:
pypi-publish.yml on stefmolin/ast-explore
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ast_explore-0.1.0-py3-none-any.whl -
Subject digest:
2d1277d39ac69b86f4bc157e0f7fd74def871bded1b7763a783d9c7506ae5c29 - Sigstore transparency entry: 1960394538
- Sigstore integration time:
-
Permalink:
stefmolin/ast-explore@2925eb56742cb49f2be40767d6ba0fb6d85bff09 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/stefmolin
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@2925eb56742cb49f2be40767d6ba0fb6d85bff09 -
Trigger Event:
push
-
Statement type: