Skip to content

Commit

Permalink
fix: Don't allow unparenthesizing walruses (psf#4155)
Browse files Browse the repository at this point in the history
Signed-off-by: RedGuy12 <61329810+RedGuy12@users.noreply.github.com>
Signed-off-by: RedGuy12 <paul@reid-family.org>
  • Loading branch information
RedGuy12 committed Jan 17, 2024
1 parent b7c3a9f commit 9a331d6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- Address a missing case in the change to allow empty lines at the beginning of all
blocks, except immediately before a docstring (#4130)
- For stubs, fix logic to enforce empty line after nested classes with bodies (#4141)
- Fix crash when using a walrus in a dictionary (#4155)

### Configuration

Expand Down
6 changes: 5 additions & 1 deletion src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,11 @@ def visit_dictsetmaker(self, node: Node) -> Iterator[Line]:
if i == 0:
continue
if node.children[i - 1].type == token.COLON:
if child.type == syms.atom and child.children[0].type == token.LPAR:
if (
child.type == syms.atom
and child.children[0].type == token.LPAR
and not is_walrus_assignment(child)
):
if maybe_make_parens_invisible_in_atom(
child,
parent=node,
Expand Down
7 changes: 7 additions & 0 deletions tests/data/cases/walrus_in_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# flags: --preview
{
"is_update": (up := commit.hash in update_hashes)
}

# output
{"is_update": (up := commit.hash in update_hashes)}

0 comments on commit 9a331d6

Please sign in to comment.