Skip to content

A curated list of awesome Python code formatters

License

Notifications You must be signed in to change notification settings

life4/awesome-python-code-formatters

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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.
  • ruff: fast Rust-powered linter and code formatter, for Python. The formatter is 100% compatible with Black.
  • 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 and yapf 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] by Union[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.
  • ssort: sorts and groups classes, functions, and methods.
  • Tornado Async Transformer: A libcst transformer for updating tornado @gen.coroutine syntax to python3.5+ native async/await.
  • teyit: formats unittest assertions.
  • unify: modifies strings to all use the same quote where possible.

Imports formatters

Formatters for import statements.

  • 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 with formate.
  • gray: wrapper around isort, pyupgrade, add-trailing-comma, and unify.
  • 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, 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.
  • 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: