Strip redundant parentheses from assignment exprs (#1906)

Fixes #1656
This commit is contained in:
Rishav Kundu 2021-02-28 06:50:23 +05:30 committed by GitHub
parent b06cd15666
commit 858225d34d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 8 deletions

View File

@ -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:

View File

@ -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

View 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))

View File

@ -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

View File

@ -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)