Unverified Commit b55ea63f authored by Richard Si's avatar Richard Si Committed by GitHub
Browse files

Stop stripping parens in even more illegal spots (#2148)

We're only fixing them so fuzzers don't yell at us when we break "valid"
code. I mean "valid" because some of the examples aren't even accepted by
Python.
parent a0b4e609
Loading
Loading
Loading
Loading
+3 −0
Changes for src/black/__init__.py: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -5624,6 +5624,9 @@ def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
            syms.expr_stmt,
            syms.assert_stmt,
            syms.return_stmt,
            # these ones aren't useful to end users, but they do please fuzzers
            syms.for_stmt,
            syms.del_stmt,
        ]:
            return False

+21 −0
Changes for tests/data/pep_572_do_not_remove_parens.py: 21 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Most of the following examples are really dumb, some of them aren't even accepted by Python,
# we're fixing them only so fuzzers (which follow the grammar which actually allows these
# examples matter of fact!) don't yell at us :p

del (a := [1])

try:
    pass
except (a := 1) as (b := why_does_this_exist):
    pass

for (z := 124) in (x := -124):
    pass

with (y := [3, 2, 1]) as (funfunfun := indeed):
    pass


@(please := stop)
def sigh():
    pass
+15 −0
Changes for tests/data/pep_572_remove_parens.py: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -33,6 +33,9 @@ def foo2(answer: (p := 42) = 5):

lambda: (x := 1)

a[(x := 12)]
a[:(x := 13)]

# we don't touch expressions in f-strings but if we do one day, don't break 'em
f'{(x:=10)}'

@@ -43,6 +46,10 @@ def a():
    yield (a := 2)
    raise (c := 3)

def this_is_so_dumb() -> (please := no):
    pass


# output
if foo := 0:
    pass
@@ -79,6 +86,9 @@ def foo2(answer: (p := 42) = 5):

lambda: (x := 1)

a[(x := 12)]
a[:(x := 13)]

# we don't touch expressions in f-strings but if we do one day, don't break 'em
f"{(x:=10)}"

@@ -88,3 +98,8 @@ def a():
    await (b := 1)
    yield (a := 2)
    raise (c := 3)


def this_is_so_dumb() -> (please := no):
    pass
+8 −0
Changes for tests/test_black.py: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -290,6 +290,14 @@ def test_pep_572_remove_parens(self) -> None:
        if sys.version_info >= (3, 8):
            black.assert_equivalent(source, actual)

    @patch("black.dump_to_file", dump_to_stderr)
    def test_pep_572_do_not_remove_parens(self) -> None:
        source, expected = read_data("pep_572_do_not_remove_parens")
        # the AST safety checks will fail, but that's expected, just make sure no
        # parentheses are touched
        actual = black.format_str(source, mode=DEFAULT_MODE)
        self.assertFormatEqual(expected, actual)

    def test_pep_572_version_detection(self) -> None:
        source, _ = read_data("pep_572")
        root = black.lib2to3_parse(source)