Skip to main content

trailing-comma

PyPI

Add and remove trailing commas in Python source files.

A minimal replacement for add-trailing-comma, built on tokenize-rt. No AST — walks the token stream directly.

The original only exposes file and stdin interfaces — no way to work on strings directly. Exposing fix_trailing_commas(source) as the core API makes it usable as a library and directly testable, which revealed a latent bug in the original's unhug re-indentation logic.

Install

pip install trailing-comma

For development (includes build and twine for publishing):

pip install -e ".[dev]"

Usage

Rewrite files in place:

trailing-comma file1.py file2.py

From stdin (no arguments):

echo "foo(
    a, b
)" | trailing-comma

As a library:

from trailing_comma import fix_trailing_commas

result = fix_trailing_commas(source)

What it does

  • Adds trailing commas to multi-line calls, defs, imports, lists, dicts, sets, and tuples
  • Removes trailing commas from single-line constructs (preserves single-element tuples)
  • Unhugs content from braces and fixes closing brace indentation

What it skips

Generators, comprehensions, class bases, subscripts, parenthesized expressions.

Release files for trailing-comma 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for trailing-comma 0.1.1
File Size Uploaded
trailing_comma-0.1.1.tar.gz 10.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for trailing-comma 0.1.1
File Interpreter ABI Platform
trailing_comma-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.2 kB

Release files / trailing_comma-0.1.1.tar.gz

Download URL trailing_comma-0.1.1.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
05b4780b1247bb5bf1dd7cddef14d2b402b0041751362c63d8fd002f705e0092
BLAKE2b-256 checksum
How to use checksums
edd6a28643cc43183d06f1991b3b732ea8fce4cb3854b8f44a75799c7d0bdebb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0rc3

Release files / trailing_comma-0.1.1-py3-none-any.whl

Download URL trailing_comma-0.1.1-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb5036d592821e7efa97b2e8b995c48a54eae9840aeff870ff7d34884cc21f33
BLAKE2b-256 checksum
How to use checksums
98ccfd0329f53ab33e86b6637498f2b5bffdcf5328498449c22133b06df4dfab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0rc3

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page