diff --git a/CHANGES.md b/CHANGES.md index c84feb04934..502b52e89eb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -15,6 +15,7 @@ - Fix a crash in preview style with assert + parenthesized string (#3415) +- Fix a crash in preview style with a walrus operator in a return annotation (#3422) ### Configuration diff --git a/src/black/linegen.py b/src/black/linegen.py index 219495e9a5e..293d57beeba 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -1160,6 +1160,7 @@ def maybe_make_parens_invisible_in_atom( syms.expr_stmt, syms.assert_stmt, syms.return_stmt, + syms.funcdef, # these ones aren't useful to end users, but they do please fuzzers syms.for_stmt, syms.del_stmt, diff --git a/tests/data/preview_38/return_annotation_brackets_py38.py b/tests/data/preview_38/return_annotation_brackets_py38.py new file mode 100644 index 00000000000..a94d43e5d85 --- /dev/null +++ b/tests/data/preview_38/return_annotation_brackets_py38.py @@ -0,0 +1,9 @@ +# Should not remove parens around walrus operator +def walrus() -> (x := 1): + pass + + +# output +# Should not remove parens around walrus operator +def walrus() -> (x := 1): + pass diff --git a/tests/test_format.py b/tests/test_format.py index 01cd61eef63..aeb4bc149b6 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -44,6 +44,13 @@ def test_preview_format(filename: str) -> None: ) +@pytest.mark.parametrize("filename", all_data_cases("preview_38")) +def test_preview_minimum_python_38_format(filename: str) -> None: + source, expected = read_data("preview_38", filename) + mode = black.Mode(preview=True) + assert_format(source, expected, mode, minimum_version=(3, 8)) + + @pytest.mark.parametrize("filename", all_data_cases("preview_39")) def test_preview_minimum_python_39_format(filename: str) -> None: source, expected = read_data("preview_39", filename)