Refactoring Python with Tree-sitter and Jedi

The author shares their experience of refactoring hundreds of files to rename instances of a pytest fixture from ‘database’ to ‘db’ across their entire repo. They explored using Tree-Sitter to parse nodes and Jedi to rename identifiers, finding both tools relatively easy to learn and useful when traditional methods like grep + sed fall short. While the author did not end up merging the changes, they viewed the exercise as a fun learning opportunity. They expressed a wish for Tree-Sitter to have a direct AST manipulation mechanism and acknowledged the potential for optimization in the re-parsing process. The article closes with a video showcasing the refactor in action.

https://jackevans.bearblog.dev/refactoring-python-with-tree-sitter-jedi/

To top