Practical Refactoring with Syntax Trees

Laurent Direr

Programming & Software Engineering & Testing
Python Skill Intermediate
Domain Expertise Intermediate

Modern Python tooling relies heavily on syntax trees. In this talk, we take a practical look at Python's Abstract Syntax Tree (AST) and how Python code can be treated as structured data rather than plain text.

We'll start from first principles: how Python source code is parsed, what an AST represents, and how to reason about code as a tree. This builds a clear mental model that makes syntax-tree-based tooling easier to understand and work with.

From there, we'll explore how syntax trees enable automated refactoring across large codebases using scripts to rewrite code (sometimes called codemods). Using a realistic refactoring scenario, we'll implement a small refactoring tool using libCST.

The talk also shares practical tips from writing codemods. This includes how to use test-driven development when writing refactoring tools, where AI can help in refactoring tasks, and strategies for dealing with formatting.

Attendees will leave with a solid understanding of how syntax trees work in Python and a concrete starting point for writing their own automated refactoring tools.

Outline:

Minutes 0-5: Primer on Python syntax trees and the AST mental model Minutes 5-12: From syntax trees to codemods and automated refactoring Minutes 12-22: Implementing a refactoring codemod with libCST Minutes 22-27: Test-driven codemods, formatting strategies, and AI assistance Minutes 27-30: Conclusion

Laurent Direr

I'm a freelance web developer helping small teams ship reliable software. I've been working with Python for 10+ years and enjoy automating work for other developers.

These days I'm very interested in local-first software technologies. I attended the Recurse Center (a programming retreat) in 2018.

GitHub profile Blog