From 578f33e282479aa5bb77ee3a4f49122cef2b0a59 Mon Sep 17 00:00:00 2001 From: RedGuy12 <61329810+RedGuy12@users.noreply.github.com> Date: Wed, 13 Dec 2023 11:20:49 -0600 Subject: [PATCH] fix: In subscripts, treat walruses just like other binary operators Signed-off-by: RedGuy12 <61329810+RedGuy12@users.noreply.github.com> --- CHANGES.md | 3 +++ src/black/lines.py | 6 +++++- tests/data/cases/preview_pep_572.py | 4 ++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 9d79b0fb61a..eeb6bb1fd0b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,9 @@ +- Fix bug where spaces were not added around parenthesized walruses in subscripts, + unlike other binary operators (#4109) + ### Configuration diff --git a/src/black/lines.py b/src/black/lines.py index 2a41db173d4..0d09a6d6507 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -446,8 +446,12 @@ def is_complex_subscript(self, leaf: Leaf) -> bool: if subscript_start.type == syms.subscriptlist: subscript_start = child_towards(subscript_start, leaf) + + test_decendants = TEST_DESCENDANTS + if Preview.walrus_subscript in self.mode: + test_decendants.add(syms.namedexpr_test) 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( diff --git a/tests/data/cases/preview_pep_572.py b/tests/data/cases/preview_pep_572.py index 8e801ff6cdc..75ad0cc4176 100644 --- a/tests/data/cases/preview_pep_572.py +++ b/tests/data/cases/preview_pep_572.py @@ -3,5 +3,5 @@ x[:(a:=0)] # output -x[(a := 0):] -x[:(a := 0)] +x[(a := 0) :] +x[: (a := 0)]