Skip to main content

Add and remove trailing commas in Python source

Project description

trailing-comma

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trailing_comma-0.1.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trailing_comma-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file trailing_comma-0.1.0.tar.gz.

File metadata

  • Download URL: trailing_comma-0.1.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0rc3

File hashes

Hashes for trailing_comma-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5f353ba5a1175c818479b82e4db3f416ccfb67b8b2d24a91de0cf65525566d78
MD5 3677c957c81048f1ff6032b3bd2a9bd8
BLAKE2b-256 fffad567a8bec62e3c7f28ed92b4018414e993b08ec25a845c9bbe036243eef5

See more details on using hashes here.

File details

Details for the file trailing_comma-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: trailing_comma-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0rc3

File hashes

Hashes for trailing_comma-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7c1759b5226c0e6d2f99d221fb32930ef387e61de33e55d2f9deb765dd57449c
MD5 6dca70d6c30b5bdb3654ae68833ba659
BLAKE2b-256 27c97924f9d6a2ea70720a35138773f22ac1cd65b4735beef70829e70c91d99a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page