black
black copied to clipboard
Comment is removed inside two pairs of parentheses
Black https://github.com/psf/black/commit/c3235e
Options
--line-length=88
--safe
Input
if ((
True
# sdf
)):
print("hw")
Output
if True:
print("hw")
Expected
Something like:
if (
True
# sdf
):
print("hw")
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 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?
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
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:
if (
a_long_condition
# a long comment about
# the condition below
and (a or b)
):
pass
I removed a_long_condition, ending up with:
if (
# a long comment about
# the condition below
(a or b)
):
pass
This is what I expected when I ran black:
if (
# a long comment about
# the condition below
a or b
):
pass
This is what I really wanted:
# a long comment about
# the condition below
if a or b:
pass
And this is what I got:
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
-if ((
+if (
# type: ignore
True
-)):
+):
print("hw")
This code results in a crash when running with --safe
if ((
True
# type: ignore
)):
print("hw")