parent
b06cd15666
commit
858225d34d
@ -5370,10 +5370,7 @@ def normalize_invisible_parens(node: Node, parens_after: Set[str]) -> None:
|
|||||||
check_lpar = True
|
check_lpar = True
|
||||||
|
|
||||||
if check_lpar:
|
if check_lpar:
|
||||||
if is_walrus_assignment(child):
|
if child.type == syms.atom:
|
||||||
pass
|
|
||||||
|
|
||||||
elif child.type == syms.atom:
|
|
||||||
if maybe_make_parens_invisible_in_atom(child, parent=node):
|
if maybe_make_parens_invisible_in_atom(child, parent=node):
|
||||||
wrap_in_parentheses(node, child, visible=False)
|
wrap_in_parentheses(node, child, visible=False)
|
||||||
elif is_one_tuple(child):
|
elif is_one_tuple(child):
|
||||||
@ -5545,6 +5542,7 @@ def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
|
|||||||
Returns whether the node should itself be wrapped in invisible parentheses.
|
Returns whether the node should itself be wrapped in invisible parentheses.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if (
|
if (
|
||||||
node.type != syms.atom
|
node.type != syms.atom
|
||||||
or is_empty_tuple(node)
|
or is_empty_tuple(node)
|
||||||
@ -5554,6 +5552,10 @@ def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
|
|||||||
):
|
):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
if is_walrus_assignment(node):
|
||||||
|
if parent.type in [syms.annassign, syms.expr_stmt]:
|
||||||
|
return False
|
||||||
|
|
||||||
first = node.children[0]
|
first = node.children[0]
|
||||||
last = node.children[-1]
|
last = node.children[-1]
|
||||||
if first.type == token.LPAR and last.type == token.RPAR:
|
if first.type == token.LPAR and last.type == token.RPAR:
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
(a := a)
|
(a := a)
|
||||||
if (match := pattern.search(data)) is None:
|
if (match := pattern.search(data)) is None:
|
||||||
pass
|
pass
|
||||||
if (match := pattern.search(data)):
|
if match := pattern.search(data):
|
||||||
pass
|
pass
|
||||||
[y := f(x), y ** 2, y ** 3]
|
[y := f(x), y ** 2, y ** 3]
|
||||||
filtered_data = [y for x in data if (y := f(x)) is None]
|
filtered_data = [y for x in data if (y := f(x)) is None]
|
||||||
@ -43,5 +43,5 @@ def foo(answer: (p := 42) = 5):
|
|||||||
|
|
||||||
while x := f(x):
|
while x := f(x):
|
||||||
pass
|
pass
|
||||||
while (x := f(x)):
|
while x := f(x):
|
||||||
pass
|
pass
|
||||||
|
38
tests/data/pep_572_remove_parens.py
Normal file
38
tests/data/pep_572_remove_parens.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
if (foo := 0):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if (foo := 1):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if (y := 5 + 5):
|
||||||
|
pass
|
||||||
|
|
||||||
|
y = (x := 0)
|
||||||
|
|
||||||
|
y += (x := 0)
|
||||||
|
|
||||||
|
(y := 5 + 5)
|
||||||
|
|
||||||
|
test: int = (test2 := 2)
|
||||||
|
|
||||||
|
a, b = (test := (1, 2))
|
||||||
|
|
||||||
|
# output
|
||||||
|
if foo := 0:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if foo := 1:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if y := 5 + 5:
|
||||||
|
pass
|
||||||
|
|
||||||
|
y = (x := 0)
|
||||||
|
|
||||||
|
y += (x := 0)
|
||||||
|
|
||||||
|
(y := 5 + 5)
|
||||||
|
|
||||||
|
test: int = (test2 := 2)
|
||||||
|
|
||||||
|
a, b = (test := (1, 2))
|
@ -54,7 +54,6 @@ def example7():
|
|||||||
def example8():
|
def example8():
|
||||||
return (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((None)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
|
return (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((None)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
|
||||||
|
|
||||||
|
|
||||||
# output
|
# output
|
||||||
x = 1
|
x = 1
|
||||||
x = 1.2
|
x = 1.2
|
||||||
@ -141,4 +140,3 @@ def example7():
|
|||||||
|
|
||||||
def example8():
|
def example8():
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -263,6 +263,15 @@ def test_pep_572(self) -> None:
|
|||||||
if sys.version_info >= (3, 8):
|
if sys.version_info >= (3, 8):
|
||||||
black.assert_equivalent(source, actual)
|
black.assert_equivalent(source, actual)
|
||||||
|
|
||||||
|
@patch("black.dump_to_file", dump_to_stderr)
|
||||||
|
def test_pep_572_remove_parens(self) -> None:
|
||||||
|
source, expected = read_data("pep_572_remove_parens")
|
||||||
|
actual = fs(source)
|
||||||
|
self.assertFormatEqual(expected, actual)
|
||||||
|
black.assert_stable(source, actual, DEFAULT_MODE)
|
||||||
|
if sys.version_info >= (3, 8):
|
||||||
|
black.assert_equivalent(source, actual)
|
||||||
|
|
||||||
def test_pep_572_version_detection(self) -> None:
|
def test_pep_572_version_detection(self) -> None:
|
||||||
source, _ = read_data("pep_572")
|
source, _ = read_data("pep_572")
|
||||||
root = black.lib2to3_parse(source)
|
root = black.lib2to3_parse(source)
|
||||||
|
Loading…
Reference in New Issue
Block a user