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: In subscripts, treat walruses just like other binary operators #4109

Merged
merged 4 commits into from
Dec 28, 2023
Merged
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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

<!-- Changes that affect Black's preview style -->

- Fix bug where spaces were not added around parenthesized walruses in subscripts,
unlike other binary operators (#4109)

### Configuration

<!-- Changes to how Black can be configured -->
Expand Down
9 changes: 8 additions & 1 deletion src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,15 @@ def is_complex_subscript(self, leaf: Leaf) -> bool:

if subscript_start.type == syms.subscriptlist:
subscript_start = child_towards(subscript_start, leaf)

# When this is moved out of preview, add syms.namedexpr_test directly to
# TEST_DESCENDANTS in nodes.py
if Preview.walrus_subscript in self.mode:
test_decendants = TEST_DESCENDANTS | {syms.namedexpr_test}
else:
test_decendants = TEST_DESCENDANTS
return subscript_start is not None and any(
n.type in TEST_DESCENDANTS for n in subscript_start.pre_order()
n.type in test_decendants for n in subscript_start.pre_order()
)

def enumerate_with_length(
Expand Down
4 changes: 2 additions & 2 deletions tests/data/cases/preview_pep_572.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
x[:(a:=0)]

# output
x[(a := 0):]
x[:(a := 0)]
x[(a := 0) :]
x[: (a := 0)]