Repository: asottile/add-trailing-comma

Issue:
autopep8 and add-trailing-comma fight over indentation
Here's an (admittedly poorly-formatted) reproduction. FWIW I prefer add-trailing-comma's approach, but curious to hear your thoughts. This is obviously weird indentation but ideally they wouldn't fight. Do we need to make autopep8 smarter?

## what add-trailing-comma wants
```python
[a()
    for b in c
    if (
        d
    )
]
```

## what autopep8 wants
```python
[a()
    for b in c
    if (
        d
)
]
```

Discussion:

Patch:
diff --git a/add_trailing_comma.py b/add_trailing_comma.py
index 006dfe8..846894c 100644
--- a/add_trailing_comma.py
+++ b/add_trailing_comma.py
@@ -341,11 +341,11 @@ def _fix_src(contents_text, py35_plus, py36_plus):
             add_comma = not func.star_args or py36_plus
             # functions can be treated as calls
             fixes.append((add_comma, _find_call(func, i, tokens)))
-        # Handle parenthesized things
-        elif token.src == '(':
-            fixes.append((False, _find_simple(i, tokens)))
         elif key in visitor.literals:
             fixes.append((True, _find_simple(i, tokens)))
+        # Handle parenthesized things, unhug of tuples, and comprehensions
+        elif token.src in START_BRACES:
+            fixes.append((False, _find_simple(i, tokens)))
 
         for add_comma, fix_data in fixes:
             if fix_data is not None:


Test patch:
diff --git a/tests/add_trailing_comma_test.py b/tests/add_trailing_comma_test.py
index a663f99..ff4a4a8 100644
--- a/tests/add_trailing_comma_test.py
+++ b/tests/add_trailing_comma_test.py
@@ -577,6 +577,23 @@ def test_noop_unhugs(src):
             '    ], None,\n'
             ')',
         ),
+        # Regression test for #32
+        (
+            '[a()\n'
+            '    for b in c\n'
+            '    if (\n'
+            '        d\n'
+            '    )\n'
+            ']',
+
+            '[\n'
+            '    a()\n'
+            '    for b in c\n'
+            '    if (\n'
+            '        d\n'
+            '    )\n'
+            ']',
+        ),
     ),
 )
 def test_fix_unhugs(src, expected):