Package: deep-ast
Deep-ASTParse the AST for anyCallnodes encountered.Report Bug·Request Feature·About The Projectdeep-astis a Python library that attempts to parse all AST that would be encountered when invoking a callable in python. It does this by extending theast.NodeVistorandast.NodeTransformerclasses, so that when anast.Callnode is encountered, it's source code is parsed into anast.Nodeand then passes the node to thevisit()method.We say attempts because currently there are some limitations. It cant parse any arbitrary code likeexec('rorrEeulaV esiar'[::-1])which raises a ValueError, functions that aren't written in Python and all python internals likeprint().If you have a way around these limitations then a PR would be greatly appreciated.Getting StartedPrerequisitesdeep-astrequires python >= 3.7Installationdeep-astis available as an easy to install pip package.pipinstalldeep-astUsagedeep-ast offers drop in replacements for theast.NodeVistorandast.NodeTransformerclasses.fromdeep_astimportDeepVisitor,DeepTransformerTo start the deep processing you thedeep_visit()method. Each function takes an optionalmodulearugment. This is used if the function/method is not defined in the same file that you callingdeep_visit()method in.Example to get all exceptionsThis 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 themclassParseExceptions(DeepVisitor):def__init__(self)->None:self.raw_exceptions=[]self.found_exceptions=[]super().__init__()def_add_exception(self,name:str):self.raw_exceptions.append(name)ifnamenotinself.found_exceptions:self.found_exceptions.append(name)defvisit_Raise(self,node):exception_obj=node.excifisinstance(exception_obj,(ast.Call,ast.Name):name=(exception_obj.idifisinstance(exception_obj,ast.Name)elseexception_obj.func.id)self._add_exception(name)returnself.generic_visit(node)self._add_exception("EmptyRaise")returnself.generic_visit(node)# Test functions to visitdeffoo():bar()raiseTypeError()defbar():raiseValueError()parser=ParseExceptions()parser.deep_visit(foo)print(parser.found_exceptions)# prints ['ValueError', 'TypeError']RoadmapParsing of deeply nested attribute calls likefoo().bar().bazz()Tracking of variable assignment:print_func=printprint_func()See theopen issuesfor a list of proposed features (and known issues).ContributingContributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make aregreatly 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 ProjectSetup 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 thepoetry,nox,nox_poetryandpre-commitpackages. Then runpoetry installandpre-commit installcommands.Most of the steps can be found in theDockerfile.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 RequestContactLevi -@shady_cuzAcknowledgementsStackoverflow for getting me starteddown this road.@sethmlarson for asking me whichexceptionscan be raised.