Skip to main content

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!

[!TIP] To exclude specific node types instead (for example, if you aren't sure yet, which type you want), use --skip instead of --types.

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/--skip 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:

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ast_explore-0.2.0.tar.gz (41.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ast_explore-0.2.0-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file ast_explore-0.2.0.tar.gz.

File metadata

  • Download URL: ast_explore-0.2.0.tar.gz
  • Upload date:
  • Size: 41.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for ast_explore-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b181c3863308a72d5bc6bb3fbe49c0f87f2d5f615e2855ca728af2a3b58b26a1
MD5 e4601eee2f91e00c47f1df988599aa86
BLAKE2b-256 c825155959787710e9b34954911f75827b3ddfc844e0ba38dd69948022852d9f

See more details on using hashes here.

Provenance

The following attestation bundles were made for ast_explore-0.2.0.tar.gz:

Publisher: pypi-publish.yml on stefmolin/ast-explore

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ast_explore-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: ast_explore-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for ast_explore-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e1cb6c8560eaddf0b9c0c62bfb57e8d96a8a8b0ec198eec0aec65a0e70c7cbb
MD5 5eea69640bc52bd1895695c9f2e27e01
BLAKE2b-256 c1ceca12ffb85d46d746a3e05caf04b9bf76397433e0fef7caef0617c601fe94

See more details on using hashes here.

Provenance

The following attestation bundles were made for ast_explore-0.2.0-py3-none-any.whl:

Publisher: pypi-publish.yml on stefmolin/ast-explore

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.2.1

2 files

This release

0.2.0 This release

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page