Offline

Process, Analyze, and Transform Python Code with ASTs

Track:
Python Core, Internals, Extensions
Type:
Tutorial
Level:
intermediate
Duration:
180 minutes
View in the schedule

Abstract

You’ve likely used a tool like black, flake8, or ruff to lint or format your code, or a tool like sphinx to document it, but you probably do not know how they accomplish their tasks. These tools and many more use Abstract Syntax Trees (ASTs) to analyze and extract information from Python code. An AST is a representation of your code's structure that enables you to access and manipulate its different components, which is what makes it possible to automate tasks like code migrations, linting, and docstring extraction.

In this workshop, you’ll learn how to use the Python standard library’s ast module to parse and analyze code. Using just the standard library, we will implement a couple of common checks from scratch, which will give you an idea of how these tools work and help you build the skills and confidence to use ASTs in your own projects.