What refactoring tools do you use for Python?

PythonRefactoring

Python Problem Overview


I have a bunch of classes I want to rename. Some of them have names that are small and that name is reused in other class names, where I don't want that name changed. Most of this lives in Python code, but we also have some XML code that references class names.

Simple search and replace only gets me so far. In my case, I want to rename AdminAction to AdminActionPlug and AdminActionLogger to AdminActionLoggerPlug, so the first one's search-and-replace would also hit the second, wrongly.

Does anyone have experience with Python refactoring tools ? Bonus points if they can fix class names in the XML documents too.

Python Solutions


Solution 1 - Python

In the meantime, I've tried it two tools that have some sort of integration with vim.

The first is Rope, a python refactoring library that comes with a Vim (and emacs) plug-in. I tried it for a few renames, and that definitely worked as expected. It allowed me to preview the refactoring as a diff, which is nice. It is a bit text-driven, but that's alright for me, just takes longer to learn.

The second is Bicycle Repair Man which I guess wins points on name. Also plugs into vim and emacs. Haven't played much with it yet, but I remember trying it a long time ago.

Haven't played with both enough yet, or tried more types of refactoring, but I will do some more hacking with them.

Solution 2 - Python

I would strongly recommend PyCharm - not just for refactorings. Since the first PyCharm answer was posted here a few years ago the refactoring support in PyCharm has improved significantly.

Python Refactorings available in PyCharm (last checked 2016/07/27 in PyCharm 2016.2)

  • Change Signature
  • Convert to Python Package/Module
  • Copy
  • Extract Refactorings
  • Inline
  • Invert Boolean
  • Make Top-Level Function
  • Move Refactorings
  • Push Members down
  • Pull Members up
  • Rename Refactorings
  • Safe Delete

XML refactorings (I checked in context menu in an XML file):

  • Rename
  • Move
  • Copy
  • Extract Subquery as CTE
  • Inline

Javascript refactorings:

  • Extract Parameter in JavaScript
  • Change Signature in JavaScript
  • Extract Variable in JavaScript

Solution 3 - Python

WingIDE 4.0 (WingIDE is my python IDE of choice) will support a few refactorings, but I just tried out the latest beta, beta6, and... there's still work to be done. Retract Method works nicely, but Rename Symbol does not.

Update: The 4.0 release has fixed all of the refactoring tools. They work great now.

Solution 4 - Python

I would take a look at Bowler (https://pybowler.io).

It's better suited for use directly from the command-line than rope and encourages scripting (one-off scripts).

Solution 5 - Python

Your IDE can support refactorings !! Check it Eric, Eclipse, WingIDE have build in tools for refactorings (Rename including). And that are very safe refactorings - if something can go wrong IDE wont do ref.

Also consider adding few unit test to ensure your code did not suffer during refactorings.

Solution 6 - Python

PyCharm have some refactoring features.

> ### PYTHON REFACTORING > > Rename refactoring allows to perform global code changes safely and instantly. Local changes within a file are performed in-place. Refactorings work in plain Python and Django projects. > > Use Introduce Variable/Field/Constant and Inline Local for improving the code structure within a method, Extract Method to break up longer methods, Extract Superclass, Push Up, Pull Down and Move to move the methods and classes.

Solution 7 - Python

You can use sed to perform this. The trick is to recall that regular expressions can recognize word boundaries. This works on all platforms provided you get the tools, which on Windows is Cygwin, Mac OS may require installing the dev tools, I'm not sure, and Linux has this out of the box. So grep, xargs, and sed should do the trick, after 12 hours of reading man pages and trial and error ;)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThomas Vander SticheleView Question on Stackoverflow
Solution 1 - PythonThomas Vander SticheleView Answer on Stackoverflow
Solution 2 - PythonOliver BestwalterView Answer on Stackoverflow
Solution 3 - PythonMichael KentView Answer on Stackoverflow
Solution 4 - PythonqffView Answer on Stackoverflow
Solution 5 - Pythonprzemo_liView Answer on Stackoverflow
Solution 6 - PythonBenoit BlanchonView Answer on Stackoverflow
Solution 7 - PythonDavid PokornyView Answer on Stackoverflow