Skip to content

Commit

Permalink
Avoid removing whitespace for walrus operators within subscripts
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jul 29, 2023
1 parent 1a972e3 commit 67fa8d4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -66,6 +66,7 @@
(#3740)
- Fix error in AST validation when _Black_ removes trailing whitespace in a type comment
(#3773)
- Fix a bug whereby spaces were removed from walrus operators within subscript (#3823)

### Preview style

Expand Down
3 changes: 3 additions & 0 deletions src/black/nodes.py
Expand Up @@ -345,6 +345,9 @@ def whitespace(leaf: Leaf, *, complex_subscript: bool) -> str: # noqa: C901

return NO

elif t == token.COLONEQUAL or prev.type == token.COLONEQUAL:
return SPACE

elif not complex_subscript:
return NO

Expand Down
6 changes: 3 additions & 3 deletions tests/data/py_310/pep_572_py310.py
@@ -1,7 +1,7 @@
# Unparenthesized walruses are now allowed in indices since Python 3.10.
x[a:=0]
x[a:=0, b:=1]
x[5, b:=0]
x[a := 0]
x[a := 0, b := 1]
x[5, b := 0]

# Walruses are allowed inside generator expressions on function calls since 3.10.
if any(match := pattern_error.match(s) for s in buffer):
Expand Down

0 comments on commit 67fa8d4

Please sign in to comment.