When writing (or reviewing) code, you have better things to do than concern yourself with low-level details about coding style or other changes that are essentially mechanical in nature. Thankfully, the tooling ecosystem for doing these kind of boring changes to Python code has become much stronger in the past few years.
Below is my collection, with some alternatives and recommendations. These all go beyond being linters, which only report problems, to being able to fix your code automatically. Most of these work really well with tools like pre-commit so that by the time you come to code review, all the boring stuff is already fixed.
Formatting and coding style
Black is probably the most populate Python code formatter today.
YAPF is another with a similar ethos to Black, but less popular AFAIK, and I don’t use it.
I personally prefer the former, isort.
reorder_python_importshas a much more verbose style, resulting in many lines for imports. This is useful for reducing merge conflicts, but with the other tools listed here, I don’t find those much of a problem – if you aren’t sure which imports are still needed, include them all and let isort remove the duplicates, and autoflake remove the unneeded ones.
table-format makes it easy to have aligned columns in your Python source code.
The following tools will do upgrades on your code:
pyupgrade – moves code to the most modern Python idioms.
flynt – rewrites older string formatting code using
.formatand/or f-strings where possible.
setup-py-upgrade – upgrades your
pytype – this does type checking and produces
.pyifiles based on inference, and also includes a
merge-pyitool that can merge
autotyping – a tool to add type hints for various cases where this can be done automatically.
(As a comment, I’m not wild about some of these automated changes. Annotating
-> str, when it is required to be a str, seems like a failure of our static typing tools, and it adds a lot of noise.)
Many IDEs/editors provide a bunch of tools to rewrite Python code (for example doing renames), often by integrating with language servers.
In VSCode, the default is Pylance which is proprietary and can only be used with VSCode. However, pyright powers most of its functionality, and is Open Source. As well as being a command line static type checker, it also functions as a language server, and it’s the one I use from Emacs at the moment.
One of the issues I find is that these is that they can be hard to use from the command line, to be able to do more automated refactoring – in fact I haven’t found a good way to do so, other than scripting things using elisp.
So here are some other tools that are designed for more stand-alone use and have some refactoring features:
Write your own
Finally, there are great libraries like libCST that will help you to manipulate Python code but without losing comments etc., so that writing your own tool to do this is no longer a massive task.
Have fun writing code to fix your code!