Notifications
Clear all

What are the best Claude Code skills for refactoring legacy Python?

3 Posts
4 Users
0 Reactions
101 Views
0
Topic starter

What are the best Claude Code skills or specific commands I should be using for refactoring a massive pile of legacy Python because I am seriously losing my mind here. I am stuck in this tiny office in Chicago trying to fix a logistics tracking system that was written back when people still thought global variables were a good idea and I have exactly three weeks before our legacy server gets turned off for good and everything goes dark. It is a complete mess of Python 2.7 logic barely holding on with duct tape and some 3.x patches that dont even make sense.

Ive been playing around with the new Claude Code CLI tool and honestly its making me more frustrated than anything right now. I read online that I should just use the /refactor command or maybe set up a custom MCP server to help it see the file structure better but the advice is so conflicting. Some people on Reddit say you should just let it read everything at once while others say thats how you get massive hallucinations where it starts making up library functions that dont exist. When I tried the basic refactor it kept trying to use modern Pydantic models on stuff thats basically just dictionaries and it broke the whole data pipeline.

My main issues right now:

  • It keeps missing these weird circular imports that are scattered everywhere
  • It doesnt seem to realize how the old SQLAlchemy calls are structured
  • I cant figure out if I should be using the edit tool for small chunks or if there is a better way to have it analyze the whole dependency graph first

I dont have a massive budget for this, just my standard subscription and maybe fifty bucks in extra credits if I can justify it to my boss who barely knows what an API is. Is there a specific workflow or a set of tool-use patterns that actually works for this kind of archaeology coding? Every time I think I have a prompt that works it fails to see a file three folders up and the whole thing crashes. How do I get it to actually understand the context of 15-year-old spaghetti code without it just guessing...


3 Answers
12

Regarding what #2 said about "Honestly, I've tried many ways to fix legacy...", i totally agree. I saved a fortune on credits by using Pylint Static Analysis Tool to find circular imports myself before even opening the CLI. It's so much more reliable.

  • run local linting first
  • feed specific error lines only
  • avoid whole-file uploads Im very satisfied with this approach since it keeps my api bill under fifty bucks easily.


11

In my experience, feeding Anthropic Claude 3.5 Sonnet API tiny chunks works best. Run the /compact command often so it doesn't get confused by those old circular imports and waste your budget.


1

Honestly, I've tried many ways to fix legacy garbage and the /refactor command is usually a trap. It over-promises and makes a mess of old SQLAlchemy sessions. In my experience, you gotta treat it like a junior dev who hasn't seen old code before.

  • Use the grep tool to find every instance of a global before you touch a file.
  • Dont let it guess your schema. Open your main model file first so its in the context window.
  • If youre losing your mind over missing files, try Cursor Code Editor Pro Subscription instead of just the CLI tool. It handles the codebase indexing way better than Claude Code does natively right now. That fifty bucks will go further if you use Anthropic Claude 3.5 Haiku API for the basic where is this defined tasks. Its way cheaper than Sonnet and plenty fast for archaeological mapping. Just keep the context tight and dont let it hallucinate modern libraries into your 2.7 logic.


Share: