Skip to content

Commit 48afd8f

Browse files
JelleZijlstraamyreese
authored andcommitted
Back out psf#850 (psf#1079)
Fixes psf#1042 (and probably psf#1044 which looks like the same thing). The issue with the "obviously unnecessary" parentheses that psf#850 removed is that sometimes they're necessary to help Black fit something in one line. I didn't see an obvious solution that still removes the parens psf#850 was intended to remove, so let's back out this change for now in the interest of unblocking a release. This PR also adds a test adapted from the failing example in psf#1042, so that if we try to reapply the psf#850 change we don't break the same case again.
1 parent db6ed34 commit 48afd8f

File tree

4 files changed

+30
-27
lines changed

4 files changed

+30
-27
lines changed

black.py

-20
Original file line numberDiff line numberDiff line change
@@ -1655,26 +1655,6 @@ def visit_default(self, node: LN) -> Iterator[Line]:
16551655
self.current_line.append(node)
16561656
yield from super().visit_default(node)
16571657

1658-
def visit_atom(self, node: Node) -> Iterator[Line]:
1659-
# Always make parentheses invisible around a single node, because it should
1660-
# not be needed (except in the case of yield, where removing the parentheses
1661-
# produces a SyntaxError).
1662-
if (
1663-
len(node.children) == 3
1664-
and isinstance(node.children[0], Leaf)
1665-
and node.children[0].type == token.LPAR
1666-
and isinstance(node.children[2], Leaf)
1667-
and node.children[2].type == token.RPAR
1668-
and isinstance(node.children[1], Leaf)
1669-
and not (
1670-
node.children[1].type == token.NAME
1671-
and node.children[1].value == "yield"
1672-
)
1673-
):
1674-
node.children[0].value = ""
1675-
node.children[2].value = ""
1676-
yield from super().visit_default(node)
1677-
16781658
def visit_factor(self, node: Node) -> Iterator[Line]:
16791659
"""Force parentheses between a unary op and a binary power:
16801660

tests/data/expression.diff

+1-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@
160160
+{"2.7": dead, "3.7": long_live or die_hard}
161161
+{"2.7", "3.6", "3.7", "3.8", "3.9", "4.0" if gilectomy else "3.10"}
162162
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or A, 11 or B, 12 or C]
163-
-(SomeName)
164-
+SomeName
163+
(SomeName)
165164
SomeName
166165
(Good, Bad, Ugly)
167166
(i for i in (1, 2, 3))

tests/data/expression.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ async def f():
412412
{"2.7": dead, "3.7": long_live or die_hard}
413413
{"2.7", "3.6", "3.7", "3.8", "3.9", "4.0" if gilectomy else "3.10"}
414414
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or A, 11 or B, 12 or C]
415-
SomeName
415+
(SomeName)
416416
SomeName
417417
(Good, Bad, Ugly)
418418
(i for i in (1, 2, 3))

tests/data/remove_parens.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
print((1))
21
x = (1)
32
x = (1.2)
4-
(x) = (3)
53

4+
data = (
5+
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
6+
).encode()
7+
8+
async def show_status():
9+
while True:
10+
try:
11+
if report_host:
12+
data = (
13+
f"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
14+
).encode()
15+
except Exception as e:
16+
pass
617

718
def example():
819
return (("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"))
@@ -45,10 +56,23 @@ def example8():
4556

4657

4758
# output
48-
print(1)
4959
x = 1
5060
x = 1.2
51-
x = 3
61+
62+
data = (
63+
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
64+
).encode()
65+
66+
67+
async def show_status():
68+
while True:
69+
try:
70+
if report_host:
71+
data = (
72+
f"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
73+
).encode()
74+
except Exception as e:
75+
pass
5276

5377

5478
def example():

0 commit comments

Comments
 (0)