Skip to main content

Add and remove trailing commas in Python source

Project description

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.

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.1.tar.gz (10.2 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.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: trailing_comma-0.1.1.tar.gz
  • Upload date:
  • Size: 10.2 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.1.tar.gz
Algorithm Hash digest
SHA256 05b4780b1247bb5bf1dd7cddef14d2b402b0041751362c63d8fd002f705e0092
MD5 02146a5b75e4eadf8ca48b51233f48a2
BLAKE2b-256 edd6a28643cc43183d06f1991b3b732ea8fce4cb3854b8f44a75799c7d0bdebb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: trailing_comma-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eb5036d592821e7efa97b2e8b995c48a54eae9840aeff870ff7d34884cc21f33
MD5 14fb83281439f4ff87a2b6f6f54f9c1f
BLAKE2b-256 98ccfd0329f53ab33e86b6637498f2b5bffdcf5328498449c22133b06df4dfab

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