Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash in return annotation with walrus #3422

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -15,6 +15,7 @@
<!-- Changes that affect Black's preview style -->

- 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

Expand Down
1 change: 1 addition & 0 deletions src/black/linegen.py
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions 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
7 changes: 7 additions & 0 deletions tests/test_format.py
Expand Up @@ -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)
Expand Down