Walks the entire AST tree, including calls to other functions.
Project description
Deep-AST
Parse the AST for any Call nodes encountered.
Report Bug
·
Request Feature
·
About The Project
deep-ast
is a Python library that attempts to parse all AST that would be encountered when invoking a callable in python. It does this by extending the ast.NodeVistor and ast.NodeTransformer classes, so that when an ast.Call node is encountered, it's source code is parsed into an ast.Node and then passes the node to the visit() method.
We say attempts because currently there are some limitations. It cant parse any arbitrary code like exec('rorrEeulaV esiar'[::-1])
which raises a ValueError, functions that aren't written in Python and all python internals like print()
.
If you have a way around these limitations then a PR would be greatly appreciated.
Getting Started
Prerequisites
deep-ast
requires python >= 3.7
Installation
deep-ast
is available as an easy to install pip package.
pip install deep-ast
Usage
deep-ast offers drop in replacements for the ast.NodeVistor and ast.NodeTransformer classes.
from deep_ast import DeepVisitor, DeepTransformer
To start the deep processing you the deep_visit()
method. Each function takes an optional module
arugment. This is used if the function/method is not defined in the same file that you calling deep_visit()
method in.
Example to get all exceptions
This example shows how you might deeply parse the AST to get all exceptions that might be thrown.
# Custom NodeVisitor to visit Raise nodes and record them
class ParseExceptions(DeepVisitor):
def __init__(self) -> None:
self.raw_exceptions = []
self.found_exceptions = []
super().__init__()
def _add_exception(self, name: str):
self.raw_exceptions.append(name)
if name not in self.found_exceptions:
self.found_exceptions.append(name)
def visit_Raise(self, node):
exception_obj = node.exc
if isinstance(exception_obj, (ast.Call, ast.Name):
name = (
exception_obj.id
if isinstance(exception_obj, ast.Name)
else exception_obj.func.id
)
self._add_exception(name)
return self.generic_visit(node)
self._add_exception("EmptyRaise")
return self.generic_visit(node)
# Test functions to visit
def foo():
bar()
raise TypeError()
def bar():
raise ValueError()
parser = ParseExceptions()
parser.deep_visit(foo)
print(parser.found_exceptions) # prints ['ValueError', 'TypeError']
Roadmap
-
Parsing of deeply nested attribute calls like
foo().bar().bazz()
-
Tracking of variable assignment:
print_func = print print_func()
See the open issues for a list of proposed features (and known issues).
Contributing
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
This project uses poetry to manage dependencies and pre-commit to run formatting, linting and tests. You will need to have both installed to your system as well as python 3.9.
-
Fork the Project
-
Setup the environment.
This project uses vscode devcontainer to provide a completly configured development environment. If you are using vscode and have the remote container extension installed, you should be asked to use the devcontainer when you open this project inside of vscode.If you are not using devcontainers then you will need to have python installed. Install the
poetry
,nox
,nox_poetry
andpre-commit
packages. Then runpoetry install
andpre-commit install
commands.Most of the steps can be found in the Dockerfile.
-
Create your Feature Branch (
git checkout -b feature/AmazingFeature
) -
Commit your Changes (
git commit -m 'Add some AmazingFeature'
) -
Push to the Branch (
git push origin feature/AmazingFeature
) -
Open a Pull Request
Contact
Levi - @shady_cuz
Acknowledgements
- Stackoverflow for getting me started down this road.
- @sethmlarson for asking me which exceptions can be raised.
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
File details
Details for the file deep-ast-0.0.1.tar.gz
.
File metadata
- Download URL: deep-ast-0.0.1.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.1 CPython/3.9.14 Linux/5.15.0-1020-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7aaaaab994c113bcdd294296ec3cfec2681a8f658f9d650dffc5ce295dfc6287 |
|
MD5 | 2019dc1a430e2ecc0917e529be6a419d |
|
BLAKE2b-256 | cf176002daf0b7ba9a6ef3b98c14027958c72714ceb89cf8492dd6c6960c8581 |
File details
Details for the file deep_ast-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: deep_ast-0.0.1-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.1 CPython/3.9.14 Linux/5.15.0-1020-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bea0333d77f6a6ccc3b4f83b21d63aa2d53baee1c695584fc4a8ead5635c3a5 |
|
MD5 | 7f42526664ca9ce68fc87471ebe8e3fd |
|
BLAKE2b-256 | 1be64974ee78005018c04fde53735d9f35322744a2cdb990e7df2ba0d75d1520 |