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_poetryandpre-commitpackages. Then runpoetry installandpre-commit installcommands.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.
Metadata
Release files for deep-ast 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| deep-ast-0.0.1.tar.gz | 8.5 kB | Without provenance | Details for deep-ast-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| deep_ast-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for deep_ast-0.0.1-py3-none-any.whl |
Total release size: 16.1 kB
Release files / deep-ast-0.0.1.tar.gz
| Download URL | deep-ast-0.0.1.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7aaaaab994c113bcdd294296ec3cfec2681a8f658f9d650dffc5ce295dfc6287
|
|
BLAKE2b-256 checksum How to use checksums |
cf176002daf0b7ba9a6ef3b98c14027958c72714ceb89cf8492dd6c6960c8581
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.1 CPython/3.9.14 Linux/5.15.0-1020-azure
|
Release files / deep_ast-0.0.1-py3-none-any.whl
| Download URL | deep_ast-0.0.1-py3-none-any.whl |
|---|---|
| Size | 7.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8bea0333d77f6a6ccc3b4f83b21d63aa2d53baee1c695584fc4a8ead5635c3a5
|
|
BLAKE2b-256 checksum How to use checksums |
1be64974ee78005018c04fde53735d9f35322744a2cdb990e7df2ba0d75d1520
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.1 CPython/3.9.14 Linux/5.15.0-1020-azure
|
Security for deep-ast 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.