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

Skip to content

Commit 9a331d6

Browse files
authored
fix: Don't allow unparenthesizing walruses (psf#4155)
Signed-off-by: RedGuy12 <[email protected]> Signed-off-by: RedGuy12 <[email protected]>
1 parent b7c3a9f commit 9a331d6

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- Address a missing case in the change to allow empty lines at the beginning of all
2323
blocks, except immediately before a docstring (#4130)
2424
- For stubs, fix logic to enforce empty line after nested classes with bodies (#4141)
25+
- Fix crash when using a walrus in a dictionary (#4155)
2526

2627
### Configuration
2728

src/black/linegen.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,11 @@ def visit_dictsetmaker(self, node: Node) -> Iterator[Line]:
242242
if i == 0:
243243
continue
244244
if node.children[i - 1].type == token.COLON:
245-
if child.type == syms.atom and child.children[0].type == token.LPAR:
245+
if (
246+
child.type == syms.atom
247+
and child.children[0].type == token.LPAR
248+
and not is_walrus_assignment(child)
249+
):
246250
if maybe_make_parens_invisible_in_atom(
247251
child,
248252
parent=node,

tests/data/cases/walrus_in_dict.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# flags: --preview
2+
{
3+
"is_update": (up := commit.hash in update_hashes)
4+
}
5+
6+
# output
7+
{"is_update": (up := commit.hash in update_hashes)}

0 commit comments

Comments
 (0)