Awesome Python Code Formatters
A curated list of awesome Python code formatters
All-in formatters
Formatters that take care of all your code.
- autopep8: format Python code to conform to the PEP 8 style guide.
- black: uncompromising Python code formatter.
- yapf: yet another Python code formatter from Google.
UNIX-way formatters
Formatters that do only one job and do it well.
- add-trailing-comma: adds trailing commas to calls and literals.
- decrapify: some scripts that use pybowler.io for refactoring Python code.
- docformatter: formats docstrings to follow PEP 257.
- docstrfmt: a tool for automatically formatting reStructuredText in files and Python docstrings in a consistent way.
- eradicate: removes commented-out code from Python files.
- fix8: fixes some Python linting issues found by Flake8.
- flynt: converts old string literal formatting to f-strings.
- formate: a wrapper around
isort
andyapf
with a few custom rules. - kwonly-transformer: Opinionated tool to ensure functions with multiple parameters to have exclusively keyword only parameters.
- no-optional: Replace
Optional[T]
byUnion[T, None]
. - pybetter: fixes some trivial problems with your code.
- pydocstringformatter: Automatically format your Python docstrings to conform with PEP 8 and PEP 257.
- pyment: formats and generates docstrings.
- remove-print-statements: A tool (and pre-commit hook) to remove print statements from your Python project.
- ssort: sorts and groups classes, functions, and methods.
- Tornado Async Transformer: A libcst transformer for updating tornado
@gen.coroutine
syntax to python3.5+ nativeasync
/await
. - teyit: formats unittest assertions.
- unify: modifies strings to all use the same quote where possible.
Imports formatters
Formatters for import statements.
- absolufy-imports: converts relative imports to absolute ones.
- autoflake: removes unused imports and unused variables as reported by pyflakes.
- isort: sorts imports.
- pyall: keeps the
__all__
list always up to date. - pycln: removes unused imports.
- removestar: replaces
import *
in Python files with explicit imports. - reorder-python-imports: reorders imports.
- unimport: removes unused imports.
- usort: Safe, minimal import sorting for Python projects.
Upgrading tools
Tools to upgrade to newer versions of Python or a framework.
- 2to3: translates Python 2 to 3.
- auto-walrus: automatically use the walrus operator where possible.
- com2ann: translates type comments to type annotations.
- django-codemod: upgrades Django projects to newer version of the framework by automatically fixing deprecations.
- django-upgrade: upgrades Django projects.
- pyupgrade: upgrades syntax for newer versions of the language.
Improvements and wrappers
Wrappers for existing code formatters to make them more accessible.
- black-macchiato: runs
black
on parts of the code. - blacken-docs: runs
black
on python code blocks in documentation files. - brunette: wrapper around
black
with improvements. - formate-black: integrates
black
withformate
. - gray: wrapper around
isort
,pyupgrade
,add-trailing-comma
, andunify
. - jupyterlab-code-formatter: code formatter for JupyterLab.
- nbQA: run
isort
,pyupgrade
,mypy
,pylint
,flake8
, and more on Jupyter Notebooks. - pyformat: wrapper around
autopep8
,autoflake
,docformatter
, andunify
. - shed: wrapper around
autoflake
,black
,com2ann
,isort
,pybetter
,pyupgrade
, andteyit
.
Libraries and refactoring
If you need to write your own formatter, these are libraries for you.
- autotransform: framework for large-scale code modification.
- bowler: safe code refactoring for modern Python.
- fissix: backport of lib2to3, with enhancements.
- importlab: A library that automatically infers dependencies for Python files. Importlab's main use case is to work with static analysis tools that process one file at a time, ensuring that a file's dependencies are analysed before it is.
- libcst: parses Python code as a CST tree that keeps all formatting details (comments, whitespaces, parentheses, etc).
- massedit: edit text files with Python.
- refactor: AST-based fragmental source code refactoring toolkit.
- rope: refactoring library.
- semgrep: like grep but for code. Supports --autofix flag for simple replacement of matched code.
- comby: Comby is a tool for searching and changing code structure
Code generators
This list doesn't contain tools that generate code, type annotations, comments etc. The difference is that code formatters transform your code from one form into another (which should be safe if the tool is stable) while code generators bring something totally new. If you're looking for code generators, check out the following links:
- awesome-python-typing: tools to generate type annotations.
- awesome-python-testing: tools to generate tests.