Skip to main content

Automatically add or remove trailing commas from calls and literals

Project description

build status

remove-trailing-comma

A tool (and pre-commit hook) to automatically remove optional trailing commas from calls and literals while preserving commas required for Python semantics. It also keeps the original add-trailing-comma command for adding commas.

Installation

Python 3.12 or newer is required.

pip install remove-trailing-comma

As a pre-commit hook

See pre-commit for instructions

Sample .pre-commit-config.yaml:

-   repo: https://github.com/basnijholt/remove-trailing-comma
    rev: main
    hooks:
    -   id: remove-trailing-comma

From the command line:

remove-trailing-comma path/to/file.py

The remove mode preserves commas that are required for Python semantics, such as one-element tuples:

 x = (1,)
-y = [1, 2,]
+y = [1, 2]

Adding trailing commas

To add trailing commas instead, use the add-trailing-comma hook:

-   repo: https://github.com/basnijholt/remove-trailing-comma
    rev: main
    hooks:
    -   id: add-trailing-comma

From the command line:

add-trailing-comma path/to/file.py

The legacy add-trailing-comma --remove-comma form is still supported.

multi-line method invocation style -- why?

# Sample of *ideal* syntax
function_call(
    argument,
    5 ** 5,
    kwarg=foo,
)
  • the initial paren is at the end of the line
  • each argument is indented one level further than the function name
  • the last parameter (unless the call contains an unpacking (*args / **kwargs)) has a trailing comma

This has the following benefits:

  • arbitrary indentation is avoided:

    # I hear you like 15 space indents
    # oh your function name changed? guess you get to reindent :)
    very_long_call(arg,
                   arg,
                   arg)
    
  • adding / removing a parameter preserves git blame and is a minimal diff:

     # with no trailing commas
     x(
    -    arg
    +    arg,
    +    arg2
     )
    
     # with trailing commas
     x(
         arg,
    +    arg2,
     )
    

Implemented features

trailing commas for function calls

 x(
     arg,
-    arg
+    arg,
 )

trailing commas for tuple / list / dict / set literals

 x = [
-    1, 2, 3
+    1, 2, 3,
 ]

trailing commas for function definitions

 def func(
         arg1,
-        arg2
+        arg2,
 ):
 async def func(
         arg1,
-        arg2
+        arg2,
 ):

trailing commas for from imports

 from os import (
     path,
-    makedirs
+    makedirs,
 )

trailing comma for class definitions

 class C(
     Base1,
-    Base2
+    Base2,
 ):
     pass

trailing comma for with statement

 with (
         open('f1', 'r') as f1,
-        open('f2', 'w') as f2
+        open('f2', 'w') as f2,
 ):
     pass

trailing comma for match statement

 match x:
     case A(
        1,
-       2
+       2,
     ):
        pass
     case (
        1,
-       2
+       2,
     ):
        pass
     case [
        1,
-       2
+       2,
     ]:
        pass
     case {
        'x': 1,
-       'y': 2
+       'y': 2,
     }:
        pass

trailing comma for PEP-695 type aliases

 def f[
-    T
+    T,
 ](x: T) -> T:
     return x
 class A[
-    K
+    K,
 ]:
     def __init__(self, x: T) -> None:
         self.x = x
 type ListOrSet[
-     T
+     T,
] = list[T] | set[T]

unhug trailing paren

 x(
     arg1,
-    arg2)
+    arg2,
+)

unhug leading paren

-function_name(arg1,
-              arg2)
+function_name(
+    arg1,
+    arg2,
+)

match closing brace indentation

 x = [
     1,
     2,
     3,
-    ]
+]

remove unnecessary commas

-[1, 2, 3,]
-[1, 2, 3, ]
+[1, 2, 3]
+[1, 2, 3]

Development

This project uses uv for dependency management and CI.

uv sync --dev
uv run coverage run -m pytest tests
uv run coverage report
uv run pre-commit run --all-files

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

remove_trailing_comma-3.2.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

remove_trailing_comma-3.2.0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file remove_trailing_comma-3.2.0.tar.gz.

File metadata

  • Download URL: remove_trailing_comma-3.2.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for remove_trailing_comma-3.2.0.tar.gz
Algorithm Hash digest
SHA256 c29915153d818723c79a5d861609b569e02e39a06f3058012bb8c901ff2aec90
MD5 8b845cb81882177d9e24231cef2f80cb
BLAKE2b-256 fc2f74acc72ed40a83814d2088bfc633c67d9ddd35b99fc748f82ab5c08179bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for remove_trailing_comma-3.2.0.tar.gz:

Publisher: release.yml on basnijholt/remove-trailing-comma

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file remove_trailing_comma-3.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for remove_trailing_comma-3.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c83c98832173bfa45a140b4d18138a7d4351c588b1671d770f4fa8f4ffbcaabf
MD5 f923709af419dbd5e56590428118da90
BLAKE2b-256 ba1ada2ec5058e080357c653bcbd2e44fceb20505d7de31e7a8fb170beaf71e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for remove_trailing_comma-3.2.0-py3-none-any.whl:

Publisher: release.yml on basnijholt/remove-trailing-comma

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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