Unlocking Python's AST: The Metaprogramming Superpower You Didn't Know You Had
summary
Builds a concrete linter that parses code with Python's ast module, walks nodes with ast.NodeVisitor, and decompiles nodes back to source using astor to detect calls to requests.get missing a timeout. It inspects Call nodes for Attribute funk values, checks .value.id == "requests" and .attr == "get", scans keyword args and treats keyword values of Constant(None) as invalid. Tested on a sample file and flags one invalid call at line 6, column 19.
AI-written summary · about
chapters (6)
- 0:00The problem definition
- 1:19Evaluating potential solutions
- 3:39Introduction to ASTs
- 7:53Traversing trees with visitors
- 11:54Implementing the solution
- 16:08Conclusion and resources
related talks
28mPractical Python parsing with the ast standard module
27mDigging into Abstract Syntax Trees for analyzing and modifying Python programs
36mHacking the Python AST
32mCode as Data_ A Practical Introduction to Python’s Abstract Syntax Tree
24mThe AST and Me
34mFinding and creating gambiarras with python ASTs
28mSafely eval Python syntax using the AST module
27mAST Meta-programming
