This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
trailing-comma is a minimal Python CLI tool that adds/removes trailing commas in Python source files. It replaces add-trailing-comma to fix one issue: a Windows stdin double-newline bug. It gives us an excuse to redesign the project and carve out features we don't want to support:
- it's 2026, we use python 3.14
- the hard part is about modifying strings, we need to isolate this part
- we use
pythonso we don't have an obsessive focus on performance - i want to control the workflow/hooks
Token-based approach using tokenize-rt (the only runtime dependency). No AST walk — the tool walks the token stream directly, matching open/close braces and checking line numbers.
Core logic is a pure function fix_trailing_commas(source: str) -> str that:
- Tokenizes with tokenize-rt
- Walks tokens for
([{, finds matching close (tracks nesting) - Same line → removes trailing comma if present
- Different lines → adds comma after last non-whitespace token
- Fixes closing brace indentation (dedent to opening line indent for function defs)
Edge cases: empty braces (skip), generators (don't add comma — detect via for keyword in tokens), nested braces (each level independent).
trailing-comma file1.py file2.py— rewrite files in placetrailing-comma -— read stdin, write stdout (usessys.stdout.buffer.writeto avoid Windows newline bug)- Exit code 1 if any file was rewritten
One pain point which is clearly intentional in the initial design, is that there is not what to feed a string. We should fix that: the hard part is about converting a bad string to a clean string, all the work around files and bytes is an overhead that should be clearly carved out - this is one reason for the rewrite of the project.
pip install -e .
It should be deployed on PyPy as well.
package name is trailing-comma - not sure if it's taken yet.
Unit tests cover fix_trailing_commas directly (no subprocess). Key test categories:
- All constructs: calls, defs, imports, lists, dicts, sets, tuples
- yapf cycle test (idempotence: yapf → tc → yapf → tc)
- Windows newline test (no
\r\r\n) - Edge cases: empty braces, generators, nested, single-element tuples
The initial repo uses pytest, can we move to unittest? Hopefully it's not using the pytest features too heavily.
Constructs handled: function calls, function defs, imports, lists, dicts, sets, tuples.
Explicitly skipped: class bases, match/case patterns, PEP 695 type aliases, with statement commas.