From 6144c46c6a6960aeaf62484d3d8cbafedf0092f3 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Fri, 21 Mar 2025 02:30:11 +0530 Subject: [PATCH] Fix parsing of walrus operator in complex with statements (#4630) --- CHANGES.md | 2 ++ src/black/linegen.py | 1 + tests/data/cases/pep_572_py310.py | 5 +++++ 3 files changed, 8 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 05c0011..4cbc3d8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ - Fix crash while formatting a long `del` statement containing tuples (#4628) +- Fix crash while formatting expressions using the walrus operator in complex + `with` statements (#4630) ### Preview style diff --git a/src/black/linegen.py b/src/black/linegen.py index ee65a7a..1ee9f6a 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -1649,6 +1649,7 @@ def maybe_make_parens_invisible_in_atom( syms.except_clause, syms.funcdef, syms.with_stmt, + syms.testlist_gexp, syms.tname, # these ones aren't useful to end users, but they do please fuzzers syms.for_stmt, diff --git a/tests/data/cases/pep_572_py310.py b/tests/data/cases/pep_572_py310.py index ba488d4..f790249 100644 --- a/tests/data/cases/pep_572_py310.py +++ b/tests/data/cases/pep_572_py310.py @@ -14,3 +14,8 @@ f((a := b + c for c in range(10)), x) f(y=(a := b + c for c in range(10))) f(x, (a := b + c for c in range(10)), y=z, **q) + + +# Don't remove parens when assignment expr is one of the exprs in a with statement +with x, (a := b): + pass