Repository: psf/black

Issue:
Comment is removed inside two pairs of parentheses
**Black https://github.com/psf/black/commit/c3235e**

[Playground link](https://black.vercel.app/?version=main&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4ACZAGVdAD2IimZxl1N_Wlws4SSfhvbGwuP6jsfmgnb8ARMAR2V_UJHHOyeiwNcjQiu0GhYjdcO4liGW2J0TeBgncyKtJ_H87d1jKJ2pyaCwrgBj1Mlq6vnOvm0jbchemI3s6xyDPUy5PJcAAAAAADxDb0Shhvi6AAGBAZoBAADnUeb4scRn-wIAAAAABFla)

## Options

`--line-length=88`
`--safe`

## Input

```python
if ((
    True
    # sdf
)):
    print("hw")

```

## Output

```python
if True:
    print("hw")

```

## Expected
Something like:
```python
if (
    True
    # sdf
):
    print("hw")
```


Discussion:
Apologies if I missed some rules/etiquettes when posting this issue.  I actually faced this issue in real-life when I took the suggestion of [this SO answer](https://stackoverflow.com/a/68911882) to combat `E129 visually indented line with same indent as next logical line` (before using black).  I had added a big comment inside describing the logic of the `if` condition, which got removed by black.  Please let me know if I should add more information.
Wouldn't it be better to do something like this?

```py
if True: # sdf
    print("hw")
```
We should definitely not remove the comment. I would welcome a proposed fix.
Well, if you blacken the following code, it stays unchanged.  [playground link](https://black.vercel.app/?version=stable&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4ACVAGFdAD2IimZxl1N_Wlws4SR2T8ZZR20deiE0nAGdx6Seyg-QeXSak9N53dGa4exKNRz_FhYC01c5J_XWwgJRXNgKs3tDJ1t-KRlCUaV7wS4Fni6Urt87IUL0gGma_qK4tgtMp2QAAAAAHfAfaVLPikcAAX2WAQAAABd_E5KxxGf7AgAAAAAEWVo=)

```python
if (
    True
    # sdf
):
    print("hw")
```
Please also consider the case of multi-line comments. I hit this bug today as well. In my case, I had something like this:
```python
if (
    a_long_condition
    # a long comment about
    # the condition below
    and (a or b)
):
    pass
```
I removed `a_long_condition`, ending up with:
```python
if (
    # a long comment about
    # the condition below
    (a or b)
):
    pass
```

This is what I expected when I ran `black`:
```python
if (
    # a long comment about
    # the condition below
    a or b
):
    pass
```
This is what I really wanted:
```python
# a long comment about
# the condition below
if a or b:
    pass
```
And this is what I got:
```python
if a or b:
    pass
```
Changed the title, since I think this is the same issue reported in #4199
```
def return_true():
    return (
        (
            True  # this comment gets removed accidentally
        )
    )
```
Interesting, it looks like there's some special logic here for `type: ignore` comments

```diff
-if ((
+if (
     # type: ignore
     True
-)):
+):
     print("hw")
```


This code results in a crash when running with `--safe`
```py
if ((
    True
    # type: ignore
)):
    print("hw")
```

Resolving patch:
diff --git a/src/black/linegen.py b/src/black/linegen.py
--- a/src/black/linegen.py
+++ b/src/black/linegen.py
@@ -1553,6 +1553,9 @@ def maybe_make_parens_invisible_in_atom(
             not is_type_ignore_comment_string(middle.prefix.strip())
         ):
             first.value = ""
+            if first.prefix.strip():
+                # Preserve comments before first paren
+                middle.prefix = first.prefix + middle.prefix
             last.value = ""
         maybe_make_parens_invisible_in_atom(
             middle,
@@ -1564,6 +1567,9 @@ def maybe_make_parens_invisible_in_atom(
             # Strip the invisible parens from `middle` by replacing
             # it with the child in-between the invisible parens
             middle.replace(middle.children[1])
+            if middle.children[-1].prefix.strip():
+                # Preserve comments before last paren
+                last.prefix = middle.children[-1].prefix + last.prefix
 
         return False
 


Test patch:
diff --git a/tests/data/cases/comments_in_double_parens.py b/tests/data/cases/comments_in_double_parens.py
new file mode 100644
--- /dev/null
+++ b/tests/data/cases/comments_in_double_parens.py
@@ -0,0 +1,113 @@
+if (
+    True
+    # sdf
+):
+    print("hw")
+
+if ((
+    True
+    # sdf
+)):
+    print("hw")
+
+if ((
+    # type: ignore
+    True
+)):
+    print("hw")
+
+if ((
+    True
+    # type: ignore
+)):
+    print("hw")
+
+if (
+    # a long comment about
+    # the condition below
+    (a or b)
+):
+    pass
+
+def return_true():
+    return (
+        (
+            True  # this comment gets removed accidentally
+        )
+    )
+
+def return_true():
+    return (True)  # this comment gets removed accidentally
+
+
+if (
+    # huh comment
+    (True)
+):
+    ...
+
+if (
+    # huh
+    (
+        # comment
+        True
+    )
+):
+    ...
+
+
+# output
+
+if (
+    True
+    # sdf
+):
+    print("hw")
+
+if (
+    True
+    # sdf
+):
+    print("hw")
+
+if (
+    # type: ignore
+    True
+):
+    print("hw")
+
+if (
+    True
+    # type: ignore
+):
+    print("hw")
+
+if (
+    # a long comment about
+    # the condition below
+    a
+    or b
+):
+    pass
+
+
+def return_true():
+    return True  # this comment gets removed accidentally
+
+
+def return_true():
+    return True  # this comment gets removed accidentally
+
+
+if (
+    # huh comment
+    True
+):
+    ...
+
+if (
+    # huh
+    # comment
+    True
+):
+    ...