Repository: asottile/add-trailing-comma

Issue:
Tool may change variables from type `T` to `tuple[T]`
Hey, I found this while using this as a pre-commit hook (big fan of the the project & your tools btw! :)).

It seems the tool may change turn a type into a tuple[type] in a situation like this;
```py
x = (
    object  # < comma is added here
), object
```

Discussion:

Patch:
diff --git a/add_trailing_comma/_plugins/literals.py b/add_trailing_comma/_plugins/literals.py
index c5771be..53bdcf6 100644
--- a/add_trailing_comma/_plugins/literals.py
+++ b/add_trailing_comma/_plugins/literals.py
@@ -26,12 +26,11 @@ def _fix_literal(
         *,
         one_el_tuple: bool,
 ) -> None:
-    if tokens[i].src in START_BRACES:  # pragma: no branch (<py38)
-        fix_brace(
-            tokens, find_simple(i, tokens),
-            add_comma=True,
-            remove_comma=not one_el_tuple,
-        )
+    fix_brace(
+        tokens, find_simple(i, tokens),
+        add_comma=True,
+        remove_comma=not one_el_tuple,
+    )
 
 
 @register(ast.Set)
@@ -90,6 +89,27 @@ def _fix_tuple(
     )
 
 
+def _fix_tuple_py38(
+        i: int,
+        tokens: List[Token],
+        *,
+        one_el_tuple: bool,
+) -> None:  # pragma: no cover (<py38)
+    if tokens[i].src in START_BRACES:
+        fix = find_simple(i, tokens)
+
+        # for tuples we *must* find a comma, otherwise it is not a tuple
+        if fix is None or not fix.multi_arg:
+            return
+
+        fix_brace(
+            tokens,
+            fix,
+            add_comma=True,
+            remove_comma=not one_el_tuple,
+        )
+
+
 @register(ast.Tuple)
 def visit_Tuple(
         state: State,
@@ -105,5 +125,5 @@ def visit_Tuple(
             func = functools.partial(_fix_tuple, one_el_tuple=is_one_el)
             yield ast_to_offset(node), func
         else:  # pragma: no cover (py38+)
-            func = functools.partial(_fix_literal, one_el_tuple=is_one_el)
+            func = functools.partial(_fix_tuple_py38, one_el_tuple=is_one_el)
             yield ast_to_offset(node), func


Test patch:
diff --git a/tests/features/literals_test.py b/tests/features/literals_test.py
index 74f543e..f5dda44 100644
--- a/tests/features/literals_test.py
+++ b/tests/features/literals_test.py
@@ -15,6 +15,12 @@ from add_trailing_comma._main import _fix_src
         '    pass\n'
         '[x] = {y}',
         pytest.param('x[1, 2, 3, 4]', id='multi-slice'),
+        pytest.param(
+            'x = (\n'
+            '    object\n'
+            '), object\n',
+            id='regression test for #153',
+        ),
     ),
 )
 def test_noop_literals(src):