add-trailing-comma
add-trailing-comma is a Python utility that automatically adds trailing commas to function calls and literal structures (like lists, tuples, dictionaries, sets) to improve diff clarity and make future additions easier. It is currently at version 4.0.0 and is actively maintained, with releases often tied to Python version updates.
Common errors
-
ModuleNotFoundError: No module named 'add_trailing_comma'
cause This library is a command-line utility and is not designed for programmatic 'import' into Python scripts.fixRun the tool directly from your terminal using `add-trailing-comma <files>` or `python -m add_trailing_comma <files>`. -
add-trailing-comma: command not found
cause The `add-trailing-comma` executable is not in your system's PATH, or the package was not successfully installed.fixEnsure the package is installed (`pip install add-trailing-comma`) and your Python environment's script directory is in your PATH. If using a virtual environment, make sure it's activated. Alternatively, run via `python -m add_trailing_comma <files>`. -
ERROR: add-trailing-comma requires Python '>=3.10' but the running Python is X.Y
cause You are attempting to install or run `add-trailing-comma` version 4.0.0+ with a Python interpreter older than 3.10.fixSwitch to a Python 3.10 or newer environment. If you absolutely need to use an older Python environment, you must install an older version of `add-trailing-comma` (e.g., `pip install 'add-trailing-comma<4.0.0'`) that is compatible with your Python version.
Warnings
- breaking As of version 4.0.0, the `add-trailing-comma` tool itself now requires Python 3.10 or newer to run. Attempting to install or execute it with older Python versions will result in an error.
- breaking The command-line flags `--py36-plus` and `--py310-plus` were removed in version 4.0.0. The tool now automatically infers the target Python version of the code it's formatting based on the Python environment it's running in.
- gotcha `add-trailing-comma` modifies files in-place without creating backups. It is strongly recommended to use this tool within a version-controlled repository (e.g., Git) to easily review and revert any unintended changes.
- gotcha When combining `add-trailing-comma` with other code formatters (e.g., Black, Ruff), it should generally be run *before* the other formatters in your pipeline (e.g., pre-commit hooks). Some opinionated formatters might remove trailing commas in certain contexts, potentially undoing `add-trailing-comma`'s work if run out of order.
Install
-
pip install add-trailing-comma
Imports
- add-trailing-comma (CLI)
import add_trailing_comma
add-trailing-comma <filenames>...
Quickstart
# Create a Python file, e.g., 'example.py'
# -- example.py BEFORE --
# def my_function(arg1, arg2):
# pass
#
# my_list = [
# 1,
# 2
# ]
#
# my_dict = {
# 'key1': 'value1',
# 'key2': 'value2'
# }
# Run the tool from your terminal:
# $ add-trailing-comma example.py
# -- example.py AFTER --
# def my_function(arg1, arg2,):
# pass
#
# my_list = [
# 1,
# 2,
# ]
#
# my_dict = {
# 'key1': 'value1',
# 'key2': 'value2',
# }