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

Skip to content

Commit 995e4ad

Browse files
authored
Fix unnecessary nesting when wrapping long dict (psf#4135)
Fixes psf#4129
1 parent 7f60f3d commit 995e4ad

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
blocks, except immediately before a docstring (#4130)
2424
- For stubs, fix logic to enforce empty line after nested classes with bodies (#4141)
2525
- Fix crash when using a walrus in a dictionary (#4155)
26+
- Fix unnecessary parentheses when wrapping long dicts (#4135)
2627

2728
### Configuration
2829

src/black/linegen.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,14 @@ def visit_dictsetmaker(self, node: Node) -> Iterator[Line]:
244244
if node.children[i - 1].type == token.COLON:
245245
if (
246246
child.type == syms.atom
247-
and child.children[0].type == token.LPAR
247+
and child.children[0].type in OPENING_BRACKETS
248248
and not is_walrus_assignment(child)
249249
):
250-
if maybe_make_parens_invisible_in_atom(
250+
maybe_make_parens_invisible_in_atom(
251251
child,
252252
parent=node,
253253
remove_brackets_around_comma=False,
254-
):
255-
wrap_in_parentheses(node, child, visible=False)
254+
)
256255
else:
257256
wrap_in_parentheses(node, child, visible=False)
258257
yield from self.visit_default(node)

tests/data/cases/preview_long_dict_values.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@
3737
}
3838

3939

40+
class Random:
41+
def func():
42+
random_service.status.active_states.inactive = (
43+
make_new_top_level_state_from_dict(
44+
{
45+
"topLevelBase": {
46+
"secondaryBase": {
47+
"timestamp": 1234,
48+
"latitude": 1,
49+
"longitude": 2,
50+
"actionTimestamp": Timestamp(
51+
seconds=1530584000, nanos=0
52+
).ToJsonString(),
53+
}
54+
},
55+
}
56+
)
57+
)
58+
59+
4060
# output
4161

4262

@@ -89,3 +109,21 @@
89109
}
90110
),
91111
}
112+
113+
114+
class Random:
115+
def func():
116+
random_service.status.active_states.inactive = (
117+
make_new_top_level_state_from_dict({
118+
"topLevelBase": {
119+
"secondaryBase": {
120+
"timestamp": 1234,
121+
"latitude": 1,
122+
"longitude": 2,
123+
"actionTimestamp": (
124+
Timestamp(seconds=1530584000, nanos=0).ToJsonString()
125+
),
126+
}
127+
},
128+
})
129+
)

0 commit comments

Comments
 (0)