Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c80685f

Browse files
authored
Treat walruses like other binary operators in subscripts (psf#4109)
Fixes psf#4078
1 parent 5178614 commit c80685f

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

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

17+
- Fix bug where spaces were not added around parenthesized walruses in subscripts,
18+
unlike other binary operators (#4109)
19+
1720
### Configuration
1821

1922
<!-- Changes to how Black can be configured -->

src/black/lines.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,15 @@ def is_complex_subscript(self, leaf: Leaf) -> bool:
446446

447447
if subscript_start.type == syms.subscriptlist:
448448
subscript_start = child_towards(subscript_start, leaf)
449+
450+
# When this is moved out of preview, add syms.namedexpr_test directly to
451+
# TEST_DESCENDANTS in nodes.py
452+
if Preview.walrus_subscript in self.mode:
453+
test_decendants = TEST_DESCENDANTS | {syms.namedexpr_test}
454+
else:
455+
test_decendants = TEST_DESCENDANTS
449456
return subscript_start is not None and any(
450-
n.type in TEST_DESCENDANTS for n in subscript_start.pre_order()
457+
n.type in test_decendants for n in subscript_start.pre_order()
451458
)
452459

453460
def enumerate_with_length(

tests/data/cases/preview_pep_572.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
x[:(a:=0)]
44

55
# output
6-
x[(a := 0):]
7-
x[:(a := 0)]
6+
x[(a := 0) :]
7+
x[: (a := 0)]

0 commit comments

Comments
 (0)