awesome-python-code-formatters icon indicating copy to clipboard operation
awesome-python-code-formatters copied to clipboard

A curated list of awesome Python code formatters

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.
  • prettier: opinionated code formatter, not only for Python.
  • 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.
  • 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 and yapf with a few custom rules.
  • 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.
  • reindent.py: changes Python files to use 4-space indents and no hard tab characters.
  • ssort: sorts and groups classes, functions, and methods.
  • teyit: formats unittest assertions.
  • unify: modifies strings to all use the same quote where possible.

Imports formatters

Formatters for import statements.

Upgrading tools

Tools to upgrade to newer versions of Python or a framework.

  • 2to3: translates Python 2 to 3.
  • 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.
  • pep585-upgrade: upgrades type hints to the new native types implemented in PEP 585.
  • 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 with formate.
  • gray: wrapper around isort, pyupgrade, add-trailing-comma, and unify.
  • jupyterlab-code-formatter: code formatter for JupyterLab.
  • pyformat: wrapper around autopep8, autoflake, docformatter, and unify.
  • shed: wrapper around autoflake, black, com2ann, isort, pybetter, pyupgrade, and teyit.

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.
  • 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.
  • undebt: tool for performing massive, automated Python code refactoring.

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: