Description
This talk presents a large-scale refactoring effort of Sentry’s backend analytics system, transforming a fragile, stringly typed event tracking API into a type-safe, maintainable solution. The original system lacked static validation, relied on manual event registration, and frequently caused subtle runtime bugs due to mismatched attributes and missing imports.
The team introduced a new approach using Python’s dataclasses, a custom @eventclass decorator, and a stricter analytics.record API, enabling full mypy support and simplifying inheritance. However, migrating hundreds of event classes, call sites, and heavily mocked tests posed significant challenges.
Initially, AI coding assistants like Copilot and Cursor were expected to automate the migration. In practice, they produced unreliable and inconsistent results. Instead, the team leveraged codemods built with libcst, with AI assisting in generating the migration tooling rather than performing the refactor directly. The migration was then split into smaller, team-owned pull requests to ensure reviewability.
Attendees will gain insights into:
- Designing type-safe replacements for legacy, stringly typed APIs
- Building reliable Python codemods with libcst
- Strategies for breaking down and managing large-scale migrations
- Where AI can - and cannot - help in real-world refactoring efforts
This session provides practical lessons for anyone facing the daunting task of modernizing legacy Python systems.