Back to Talks
Practical Refactoring with Syntax Trees

Practical Refactoring with Syntax Trees

Laurent Direr

Date
Wednesday, April 15, 2026
Time
4:15 PM - 4:45 PM
Room
Ferrum [2nd Floor]
Talk PyCon: Programming & Software Engineering & Testing
Transcription

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

EDIT: - Slides - Code for example