PyCon DE & PyData 2026

Practical Refactoring with Syntax Trees
, Ferrum [2nd Floor]

The Python Abstract Syntax Tree powers tools like pytest, linters, and automatic refactoring.
In this talk, we'll approach syntax trees from first principles and see how Python code can be treated as structured data.

We'll then explore how syntax trees can be used to automate refactoring across large codebases.
Using a real-world example and the libCST library, we'll build a small refactoring tool and share practical advice for writing and applying automated refactorings.

You'll leave with a clear mental model of syntax trees and a solid starting point for writing your own refactoring tools.


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


Expected audience expertise in your talk's domain:: Intermediate Expected audience expertise in Python:: Intermediate
See also:

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